多个神经元的实现——回归问题:
%matplotlib inline
#IPython的魔法函数,可以在IPython编译器里直接使用,作用是内嵌画图,省略掉plt.show()这一步,直接显示图像
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(-1.0,1.0,0.2)
Y = np.arange(-1.0,1.0,0.2)
Z = np.zeros((10,10))
#权重矩阵 m n 值和神经元数量一致
w_im = np.array([[4.0,4.0],
[4.0,4.0]])
w_mo = np.array([[1.0],
[-1.0]])
#偏置矩阵 偏置的数量和下层网络的神经元数量一致,为n
b_im = np.array([3.0,-3.0])
b_mo = np.array([0.1])
def middle_layer(x,w,b):
u = np.dot(x,w) + b
return 1/(1+np.exp(-u))
def output_layer(x,w,b):
u = np.dot(x,w) + b
return u
for i in range(10):
for j in range(10):
inp = np.array([X[i],Y[j]])
mid = middle_layer(inp,w_im,b_im)
out = output_layer(mid,w_mo,b_mo)
Z[j][i] = out[0]
plt.imshow(Z,"gray",vmin=0.0,vmax=1.0)
plt.colorbar()
plt.show()
多个神经网络实现——分类问题: 分类问题的特点:输出元和类别数一致
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(-1.0,1.0,0.1)
Y = np.arange(-1.0,1.0,0.1)
Z = np.zeros((10,10))
#权重矩阵 m n 值和神经元数量一致
w_im = np.array([[1.0,2.0],
[2.0,3.0]])
w_mo = np.array([[-1.0,1.0],
[1.0,-1.0]])
#偏置矩阵
b_im = np.array([0.3,-0.3])
b_mo = np.array([0.4,0.1])
def middle_layer(x,w,b):
u = np.dot(x,w) + b
return 1/(1+np.exp(-u))
def output_layer(x,w,b):
u = np.dot(x,w) + b
return np.exp(u)/np.sum(np.exp(u))
x_1 = []
y_1 = []
x_2 = []
y_2 = []
for i in range(20):
for j in range(20):
inp = np.array([X[i],Y[j]])
mid = middle_layer(inp,w_im,b_im)
out = output_layer(mid,w_mo,b_mo)
if out[0] > out[1]:
x_1.append(X[i])
y_1.append(Y[j])
else:
x_2.append(X[i])
y_2.append(Y[j])
plt.scatter(x_1,y_1,marker = "o")
plt.scatter(x_2,y_2,marker = "+")
plt.show()
标签:layer,plt,1.0,神经网络,im,np,相关,array From: https://www.cnblogs.com/bbnltxdy/p/17612949.html