Skip to content

Instantly share code, notes, and snippets.

@codekansas
Created September 11, 2016 06:18
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/1936a02d9ce6475ea58cd73cbddcc98b to your computer and use it in GitHub Desktop.
Save codekansas/1936a02d9ce6475ea58cd73cbddcc98b to your computer and use it in GitHub Desktop.
Two-layer XOR in Keras
from __future__ import print_function
import numpy as np
from keras.engine import Input, Model
from keras.layers import Dense
X = np.asarray([[0, 1], [1, 0], [0, 0], [1, 1]])
y = np.asarray([[0], [0], [1], [1]])
input = Input(shape=(2,))
hidden = Dense(5, activation='relu')(input)
output = Dense(1, activation='sigmoid')(hidden)
model = Model(input=input, output=output)
model.compile(optimizer='sgd', loss='mse')
model.fit([X], [y], nb_epoch=10000, verbose=0)
error = model.evaluate([X], [y])
print('Error: {}'.format(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment