我使用Python 2.7的neurolab包建立了一个神经网络,如下所示(松散地基于https://pythonhosted.org/neurolab/ex_newff.html处的样本)。通常,当训练发生时(带有net.train()
的行),信息将被打印到控制台,例如“达到最大列车时期数”。训练此网络通常需要> 15秒但是,看似随机且没有稍微改变代码,训练失败:没有输出消息打印到控制台并且分类不正确。
import neurolab as nl
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def f(v):
if np.linalg.norm(v) < 0.35: return 1
elif np.linalg.norm(v) < 0.75: return 0
else: return -1
# Create training set
print 'sampling'
x = (np.random.rand(500, 3)*2)-1
y = np.asarray([f(i) for i in x])
size = len(x)
inp = x.reshape(size,3)
tar = y.reshape(size,1)
# Create network
print 'creating net'
net = nl.net.newff([[-1.5, 1.5]]*3, [10, 1])
# Train network
print 'training net'
net.init()
net.train(inp, tar, epochs=500, show=100, goal=0.02)
# Simulate network
print 'simulating net'
out = net.sim(inp)
out = np.asarray([int(round(i)) for i in out])
# Plot network results
print 'plotting'
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in xrange(len(out)):
cls = 'g'
if out[i] == 1: cls = 'b'
elif out[i] == -1: cls = 'r'
ax.scatter(x[i][0], x[i][1], x[i][2], c=cls)
#plt.scatter(x[i][0], x[i][1], c=cls)
plt.show()
为什么会发生这种情况,我该如何解决?