From the material in lesson 2 I wanted to understand if we could build a keras.Sequential
to predict if a number is even or odd. The following is what I tried.
# Number of samples in the training set.
>>> samples = 100
# Numpy array of arrays with one number.
>>> x = np.reshape(np.arange(samples), (samples, 1))
# A even number has a 0 and an odd number has a 1.
>>> y = np.array([temp[0] % 2 for temp in x])
# Sample values of x
>>> x[:5]
array([[0],
[1],
[2],
[3],
[4]])
# Sample values of y
>>> y[:5]
array([0, 1, 0, 1, 0])
>>> lm = Sequential([Dense(1, input_shape=(1, )), ])
>>> lm.compile(optimizer=SGD(lr=0.1), loss='mse')
# Computes the loss on some input data, batch by batch.
>>> lm.evaluate(x, y, verbose=1)
# Computes the loss on some input data, batch by batch.
>>> lm.evaluate(x, y, verbose=1)
32/100 [========>.....................] - ETA: 0s
4156.3061328125004
>>> lm.fit(x, y, nb_epoch=5, batch_size=1)
Epoch 1/5
100/100 [==============================] - 0s - loss: nan
Epoch 2/5
100/100 [==============================] - 0s - loss: nan
Epoch 3/5
100/100 [==============================] - 0s - loss: nan
Epoch 4/5
100/100 [==============================] - 0s - loss: nan
Epoch 5/5
100/100 [==============================] - 0s - loss: nan
<keras.callbacks.History at 0x7f847acf16a0>
>>> lm.evaluate(x, y)
32/100 [========>.....................] - ETA: 0s
nan
# Let's look at the weights.
>>> lm.get_weights()
[array([[ nan]], dtype=float32), array([ nan], dtype=float32)]