Created
January 18, 2020 22:50
-
-
Save jacobeturpin/0fc94b001a0d15501502ced1c11a3a23 to your computer and use it in GitHub Desktop.
XOR Gate in Keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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