Udacity Deep Learning Nanodegree, Lesson 7, Keras 퀴즈
The Keras example of a Multi-Layer Perceptron network is similar to what you need to do here.
import numpy as np
from keras.utils import np_utils
import tensorflow as tf
# Using TensorFlow 1.0.0; use tf.python_io in later versions
tf.python.control_flow_ops = tf
# Set random seed
np.random.seed(42)
# Our data
X = np.array([[0,0],[0,1],[1,0],[1,1]]).astype('float32')
y = np.array([[0],[1],[1],[0]]).astype('float32')
# Initial Setup for Keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
# One-hot encoding the output
# y = np_utils.to_categorical(y)
# Building the model
# xor = Sequential()
xor = Sequential()
xor.add(Dense(8, input_dim=2))
xor.add(Activation('tanh'))
xor.add(Dense(1))
xor.add(Activation('sigmoid'))
sgd = SGD(lr=0.1)
xor.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
history = xor.fit(X, y, show_accuracy=False, batch_size=10, nb_epoch=1000)
# Uncomment this line to print the model architecture
xor.summary()
# Scoring the model
loss, acc = xor.evaluate(X, y)
print("\nAccuracy: ", acc)
# Checking the predictions
print("\nPredictions:")
print(xor.predict_proba(X))