I set up a neural network using the neurolab package for Python 2.7 as follows (loosely based on the sample at https://pythonhosted.org/neurolab/ex_newff.html). Normally, when training occurs (the line with net.train()
), information is printed to the console, like 'The maximum number of train epochs is reached.' Training this network usually takes > 15 seconds. However, seemingly randomly and without changing the code in the slightest, the training fails: no output messages are printed to the console and classification is incorrect.
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()
Why is this happening and how do I fix it?