Skip to content

Instantly share code, notes, and snippets.

@codekansas
Created February 24, 2017 01:05
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 codekansas/6e249a76c7bd023484218962d5444435 to your computer and use it in GitHub Desktop.
Save codekansas/6e249a76c7bd023484218962d5444435 to your computer and use it in GitHub Desktop.
#!/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