Skip to content

Instantly share code, notes, and snippets.

@jacobeturpin
Created January 18, 2020 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobeturpin/0fc94b001a0d15501502ced1c11a3a23 to your computer and use it in GitHub Desktop.
Save jacobeturpin/0fc94b001a0d15501502ced1c11a3a23 to your computer and use it in GitHub Desktop.
XOR Gate in Keras
"""Implementation of XOR Gate Using Keras"""
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
inputs = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
targets = np.array([
[1, 0],
[0, 1],
[0, 1],
[1, 0]
])
model = Sequential()
model.add(Dense(2, activation='relu', input_shape=(2,)))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(inputs, targets,
epochs=1000,
batch_size=1)
predictions = model.predict(inputs)
print('Expected Output: ', targets.argmax(axis=-1))
print('Predicted Output: ', predictions.argmax(axis=-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment