我使用 Python 2.7 的神经实验室包设置了一个神经网络,如下所示(大致基于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()
为什么会发生这种情况,我该如何解决?