Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple XOR learning with keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
import numpy as np
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])
model = Sequential()
model.add(Dense(8, input_dim=2))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
sgd = SGD(lr=0.1)
model.compile(loss='binary_crossentropy', optimizer=sgd)
model.fit(X, y, show_accuracy=True, batch_size=1, nb_epoch=1000)
print(model.predict_proba(X))
"""
[[ 0.0033028 ]
[ 0.99581173]
[ 0.99530098]
[ 0.00564186]]
"""

Great simple example. I would get it to work with only two neurons in the dense layer by running for more epochs. I tested this and it gets to similar accuracy with 5000 epochs. With rounding, only ~2000 epochs are needed.

Try this code. Best result I was able to get from it is 22 epochs with batchsize = 4. So only 86 network evaluations !!!

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras.callbacks import Callback
from keras.initializers import VarianceScaling 
import numpy as np 


lastEpoch = 0


class EarlyStoppingByLossVal(Callback):
    def __init__(self, monitor='val_loss', value=0.008, verbose=0):
        super(Callback, self).__init__()
        self.monitor = monitor
        self.value = value
        self.verbose = verbose
    def on_epoch_end(self, epoch, logs={}):
        global lastEpoch
        current = logs.get("loss")         
        if current != None and current < self.value:
            self.model.stop_training = True
            lastEpoch = epoch + 1


x = np.array([
    [0,0], [0,1],
    [1,0], [1,1]
])
y = np.array([
    [0], [1], 
    [1], [0]
])

model = Sequential()
model.add(Dense(8, 
                input_dim = 2, 
                use_bias = False, 
                kernel_initializer = VarianceScaling()))
model.add(Activation('tanh'))
model.add(Dense(1, 
                use_bias = False, 
                kernel_initializer = VarianceScaling()))
model.add(Activation('tanh'))
model.compile(loss = "mean_squared_error", 
              optimizer = SGD(lr = 0.6, 
                              momentum = 0.6))

model.fit(x, y, 
          verbose = 1, 
          batch_size = 4, 
          epochs = 10000, 
          callbacks = [
            EarlyStoppingByLossVal()
          ])

print(model.predict_proba(x))
print("Last epoch: " + str(lastEpoch))

baj12 commented Oct 14, 2017

how would i solve this xor problem:

n=300
x1 = np.random.rand(n,2) * (-1)
x2 = np.random.rand(n,2)
x2[:,1] *= (-1)

x3 = np.random.rand(n,2)
x3[:,0] *= (-1)
x4 = np.random.rand(n,2)
x = np.concatenate((x1, x2, x3, x4))
x = (x + 1 ) /2
y1 = np.ones(n)
y4 = np.ones(n)
y2 = np.zeros(n)
y3 = np.zeros(n)
y = np.concatenate((y1,y2,y3,y4))
print (x1[[1,2],:])
print (x2[[1,2],:])
print (x3[[1,2],:])
print (x4[[1,2],:])

import matplotlib.pyplot as plt

plt.plot(x1[:,0], x1[:,1], 'ro')
plt.plot(x2[:,0], x2[:,1], 'bo')
plt.plot(x3[:,0], x3[:,1], 'bo')
plt.plot(x4[:,0], x4[:,1], 'ro')

plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment