import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense
import time
nb_epoch = 500
for tiles in [1, 10, 50, 500]:
# the four different states of the XOR gate
training_data = np.tile(
np.array([[0,0],[0,1],[1,0],[1,1]], "float32"),
(tiles, 1)
)
# the four expected results in the same order
target_data = np.tile(
np.array([[0],[1],[1],[0]], "float32"),
(tiles, 1)
)
model = Sequential()
model.add(Dense(16, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(
loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy']
)
start = time.time()
model.fit(training_data, target_data, nb_epoch=nb_epoch/tiles, verbose=0)
print '%d * %d -> %.3f' % (nb_epoch/tiles, tiles, time.time() - start)