Skip to content

Instantly share code, notes, and snippets.

@deevis
Last active August 16, 2017 07:12
Show Gist options
  • Select an option

  • Save deevis/f115ec4776bd5d0e6148c4c1a291b69f to your computer and use it in GitHub Desktop.

Select an option

Save deevis/f115ec4776bd5d0e6148c4c1a291b69f to your computer and use it in GitHub Desktop.
Using Tensorflow and Keras to approximate an XOR
import numpy as np
from tensorflow.contrib.keras.api.keras.models import Sequential, Model
from tensorflow.contrib.keras.api.keras.layers import Dense, Activation, Dropout
from tensorflow.contrib.keras.api.keras.optimizers import SGD
model = Sequential()
# tanh activation allows [-1,1]
model.add(Dense(6, input_dim=2, activation='tanh'))
# hard_sigmoid will converge much faster than sigmoid with this example
model.add(Dense(1, activation='hard_sigmoid'))
sgd = SGD(lr=0.25)
model.compile(optimizer=sgd, loss='binary_crossentropy')
X = np.array([[0,0], [1,0], [0,1], [1,1]])
y = np.array([[0], [1], [1], [0]])
# Only need 100 epochs using hard_sigmoid and it converges to an exact solution!
model.fit(X, y, batch_size=1, epochs=100, verbose=1)
outputs = model.predict(X)
for input, output in zip(X, outputs):
print("{} => {}".format(input, output))
# [0 0] => [ 0.]
# [1 0] => [ 1.]
# [0 1] => [ 1.]
# [1 1] => [ 0.]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment