Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Created February 19, 2018 23:29
Show Gist options
  • Save omaraflak/9833bd54a98093b0ade2fc11e2c08cfc to your computer and use it in GitHub Desktop.
Save omaraflak/9833bd54a98093b0ade2fc11e2c08cfc to your computer and use it in GitHub Desktop.
Simple XOR with Keras
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import SGD
import numpy as np
def main():
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, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.1))
model.fit(X, Y, batch_size=1, nb_epoch=1000)
model.summary()
print(model.predict(X))
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment