XOR example

92 views
Skip to first unread message

David Cabanillas

unread,
Jun 3, 2015, 4:29:40 AM6/3/15
to py-ne...@googlegroups.com
I have tried to develop the xor example with neurolab with HardLim. The main problem is that the o = net.sim(i) variable returns 
array([[ 0.],
       [ 0.],
       [ 0.],
       [ 0.]]

My code is:

import neurolab as nl
import numpy as np  
import pylab as pl

# Create train samples
#data
input = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

#class
target = np.array([[0], [1],[1], [0]])

print ('nl.tool.minmax(input) %s',nl.tool.minmax(input))
# Create network with 2 layers:4 neurons in input layer
# and 2 neurons in output layer(liner)
net = nl.net.newff([[0, 1], [0, 1]], [2, 1],transf=[nl.trans.HardLim()] *2)

print ('net_layers %s',len(net.layers))

# Train network
error = net.train(input, target, epochs=100, goal=0.1)
print ('error %s',error)
# Plot result
import pylab as pl
i = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
o = net.sim(i)
print ('o  %s',o )

grid1 = i[o[:, 0]>0.5]#!!!
grid2 = i[o[:, 0]<0.5]#!!!

class1 = input[target[:, 0]==1]
class2 = input[target[:, 0]==0]

pl.plot(class1[:,0], class1[:,1], 'bx', class2[:,0], class2[:,1], 'rx')
pl.plot(grid1[:,0], grid1[:,1], 'b+', grid2[:,0], grid2[:,1], 'r+')

pl.axis([-0.5, 1.5, -0.5, 1.5])
pl.xlabel('Input[:, 0]')
pl.ylabel('Input[:, 1]')
pl.legend(['class 1', 'class 2', 'detected class 1', 'detected class 2'])
pl.show()

Reply all
Reply to author
Forward
0 new messages