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]]
"""
@baj12
Copy link

baj12 commented Dec 30, 2024

reattacking this problem (with the guidance of o1/chatGPT):
https://github.com/baj12/XOR

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