Skip to content

Instantly share code, notes, and snippets.

@stewartpark
Created October 12, 2015 08:17
Show Gist options
  • Save stewartpark/187895beb89f0a1b3a54 to your computer and use it in GitHub Desktop.
Save stewartpark/187895beb89f0a1b3a54 to your computer and use it in GitHub Desktop.
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]]
"""
@homerobse
Copy link

homerobse commented Aug 1, 2018

I wanted to solve this with only two hidden units. So I used this code which worked fine (for most executions, depending on the random initial condition):

 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(2, input_dim=2))
 model.add(Activation('tanh'))
 model.add(Dense(1))
 model.add(Activation('sigmoid'))
 
 sgd = SGD(lr=0.1)
 model.compile(loss='mean_squared_error', optimizer=sgd)
 
 model.fit(X, y, batch_size=1, epochs=1000)
 print(model.predict_proba(X))

I think to solve it for any initial condition we need to have scattered inputs like @baj12 proposed. But I didn't test it.

@jollyblade
Copy link

I added some biases and random initialization as well, however I have no better result as consciencia

rndU = RandomUniform(minval=-1, maxval=1, seed=None)
model = Sequential()
model.add(Dense(9, activation='sigmoid', input_dim=2, use_bias = True, kernel_initializer=rndU, bias_initializer=rndU))
model.add(Dense(1, activation='sigmoid', use_bias = True, kernel_initializer=rndU, bias_initializer=rndU))

@gauravkr0071
Copy link

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.initializers import RandomUniform
import numpy as np
x=np.array([[0.1,0.1,1],
[0.1,0.9,1],
[0.9,0.1,1],
[0.9,0.9,1]])
y=np.array([[0.1],[0.9],[0.9],[0.1]])
model= Sequential()
model.add(Dense(4,input_dim=3,activation="sigmoid",
bias_initializer=RandomUniform(minval=-1.0, maxval=1, seed=None)))
model.add(Dense(1,activation="sigmoid",bias_initializer=RandomUniform(minval=-1.0, maxval=1, seed=None)))
sgd=SGD(lr=0.01)
model.compile(loss='mean_squared_error',optimizer='sgd')
model.fit(x,y,epochs=5000,batch_size=1,verbose=1)

i am not geeting good result what i am doing wrong any idea

@belabedmohammed
Copy link

@gauravkr0071 replace
model.compile(loss='mean_squared_error',optimizer='sgd')
by this
model.compile(loss='mean_squared_error',optimizer=sgd)

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