Created
February 24, 2017 01:05
-
-
Save codekansas/6e249a76c7bd023484218962d5444435 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
"""Example of building a model to solve an XOR problem in Keras.""" | |
import keras | |
import numpy as np | |
# XOR data. | |
x = np.array([ | |
[0, 1], | |
[1, 0], | |
[0, 0], | |
[1, 1], | |
]) | |
y = np.array([ | |
[1], | |
[1], | |
[0], | |
[0], | |
]) | |
# Builds the model. | |
input_var = keras.layers.Input(shape=(2,), dtype='float32') | |
hidden = keras.layers.Dense(5, activation='tanh')(input_var) | |
hidden = keras.layers.Dense(5, activation='tanh')(hidden) | |
output_var = keras.layers.Dense(1, activation='sigmoid')(hidden) | |
model = keras.models.Model([input_var], [output_var]) | |
model.compile(loss='mean_squared_error', optimizer='sgd') | |
# Train the model. | |
model.fit([x], [y], nb_epoch=10000) | |
# Show the predictions. | |
preds = model.predict([x]) | |
print preds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment