# 在PyTorch中测试深层神经网络（图解）

def plot_decision_boundary(x, y):

x_span=np.linspace(min(x[:, 0]), max(x[:, 0]))
y_span=np.linspace(min(y[:, 1]), max(y[:, 1]))

xx, yy=np.meshgrid(x_span, y_span)

meshgrid()函数同时使用向量x_span和y_span作为参数。两个向量都包含50个元素, 此函数将返回二维的50 * 50矩阵。新添加的行将是x_span向量中原始行的重复副本, 并将返回到xx变量。 y_span的过程相同；它将返回二维的50 * 50矩阵, 其中新添加的列将是y_span向量中原始列的重复副本。该矩阵将返回yy变量。

print(xx.ravel(), yy.ravel())

grid=np.c_[xx.ravel(), yy.ravel()]

grid=torch.Tensor(np.c_[xx.ravel(), yy.ravel()])

model.forward(grid)

pred_func=model.forward(grid)

z=pred_func.view(xx.shape).numpy()

z=pred_func.view(xx.shape).detach().numpy()

plt.contourf(xx, yy, z)

plot_decision_boundary(x, y)
scatter_plot()

p1=torch.Tensor(0.25, 0.25])

plt.plot(p1[0], p1[1], marker='o', markersize=5, color='red')

plt.plot(p1.numpy()[0], p1.numpy()[1], marker='o', markersize=5, color='red')

print("Red point positive probability={}".format(model.forward(p1).item()))

def predict(self, x):
pred=self.forward(x)
if pred>=0.5:
return 1
else:
return 0

print("Red point in calss={}".format(model.predict(p1)))

## 完整的代码

import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn as nn
from sklearn import datasets
no_of_points=500
x, y=datasets.make_circles(n_samples=no_of_points, random_state=123, noise=0.1, factor=0.2)
xdata=torch.Tensor(x)
ydata=torch.Tensor(y)
def scatter_plot():
plt.scatter(x[y==0, 0], x[y==0, 1])
plt.scatter(x[y==1, 0], x[y==1, 1])
plt.show()

class Deep_neural_network(nn.Module):
def __init__(self, input_size, h1, output_size):
super().__init__()
self.linear=nn.Linear(input_size, h1)   # input layer connect with hidden layer
self.linear1=nn.Linear(h1, output_size)   # hidden layer connect with output layer
def forward(self, x):
x=torch.sigmoid(self.linear(x))	# Return the prediction x
x=torch.sigmoid(self.linear1(x))	# Prediction will go through the next layer.
return x        # Returning final outputs
def predict(self, z):
pred=self.forward(z)
if pred>=0.5:
return 1
else:
return 0
torch.manual_seed(2)
model= Deep_neural_network(2, 4, 1)	# 2 input nodes, 4 hidden nodes and 1 output node
print(list(model.parameters()))
criterion=nn.BCELoss()
epochs=1000
losses=[]
for i in range(epochs):
ypred=model.forward(xdata)
loss=criterion(ypred, ydata)
print("epoch:", i, "loss:", loss.item())
losses.append(loss)
loss.backward()
optimizer.step()
def plot_decision_boundary(x, y):
x_span=np.linspace(min(x[:, 0]), max(x[:, 0]))
y_span=np.linspace(min(x[:, 1]), max(x[:, 1]))
xx, yy=np.meshgrid(x_span, y_span)
grid=torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
pred_func=model.forward(grid)
z=pred_func.view(xx.shape).detach().numpy()
plt.contourf(xx, yy, z)
z1=0.25
z2=0.25
p1=torch.Tensor([z1, z2])
plt.plot(p1.numpy()[0], p1.numpy()[1], marker='o', markersize=5, color='red')
print("Red point positive probability={}".format(model.forward(p1).item()))
print("Red point in calss={}".format(model.predict(p1)))
plot_decision_boundary(x, y)
scatter_plot()