Keras and the Last Number Problem

Keras and the Last Number Problem#

Let’s see if we can do better than our simple hidden layer NN with the last number problem.

import numpy as np
import keras
from keras.utils import np_utils
2024-11-20 14:15:26.442108: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-20 14:15:26.445333: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-20 14:15:26.454128: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1732112126.468563    3678 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1732112126.472806    3678 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-11-20 14:15:26.488811: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 3
      1 import numpy as np
      2 import keras
----> 3 from keras.utils import np_utils

ImportError: cannot import name 'np_utils' from 'keras.utils' (/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/keras/api/utils/__init__.py)

We’ll use the same data class

class ModelDataCategorical:
    """this is the model data for our "last number" training set.  We
    produce input of length N, consisting of numbers 0-9 and store
    the result in a 10-element array as categorical data.

    """
    def __init__(self, N=10):
        self.N = N
        
        # our model input data
        self.x = np.random.randint(0, high=10, size=N)
        self.x_scaled = self.x / 10 + 0.05
        
        # our scaled model output data
        self.y = np.array([self.x[-1]])
        self.y_scaled = np.zeros(10) + 0.01
        self.y_scaled[self.x[-1]] = 0.99
        
    def interpret_result(self, out):
        """take the network output and return the number we predict"""
        return np.argmax(out)

For Keras, we need to pack the scaled data (both input and output) into arrays. We’ll use the Keras np_utils.to_categorical() to make the data categorical.

Let’s make both a training set and a test set

x_train = []
y_train = []
for _ in range(10000):
    m = ModelDataCategorical()
    x_train.append(m.x_scaled)
    y_train.append(m.y)

x_train = np.asarray(x_train)
y_train = np_utils.to_categorical(y_train, 10)
x_test = []
y_test = []
for _ in range(1000):
    m = ModelDataCategorical()
    x_test.append(m.x_scaled)
    y_test.append(m.y)

x_test = np.asarray(x_test)
y_test = np_utils.to_categorical(y_test, 10)

Check to make sure the data looks like we expect:

x_train[0]
array([0.15, 0.35, 0.35, 0.25, 0.25, 0.55, 0.75, 0.75, 0.95, 0.15])
y_train[0]
array([0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)

Now let’s build our network. We’ll use just a single hidden layer, but instead of the sigmoid used before, we’ll use RELU and the softmax activations.

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from tensorflow.keras.optimizers import RMSprop
model = Sequential()
model.add(Dense(100, input_dim=10, activation="relu"))
model.add(Dropout(0.1))
model.add(Dense(10, activation="softmax"))
rms = RMSprop()
model.compile(loss='categorical_crossentropy',
              optimizer=rms, metrics=['accuracy'])

Now we can train and test each epoch to see how we do

epochs = 100
batch_size = 256
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size,
          validation_data=(x_test, y_test), verbose=2)
Epoch 1/100
40/40 - 0s - loss: 1.0717 - accuracy: 0.6682 - val_loss: 1.0455 - val_accuracy: 0.7210 - 72ms/epoch - 2ms/step
Epoch 2/100
40/40 - 0s - loss: 1.0422 - accuracy: 0.6832 - val_loss: 1.0176 - val_accuracy: 0.7890 - 50ms/epoch - 1ms/step
Epoch 3/100
40/40 - 0s - loss: 1.0140 - accuracy: 0.7036 - val_loss: 0.9844 - val_accuracy: 0.7540 - 51ms/epoch - 1ms/step
Epoch 4/100
40/40 - 0s - loss: 0.9882 - accuracy: 0.7129 - val_loss: 0.9576 - val_accuracy: 0.8150 - 50ms/epoch - 1ms/step
Epoch 5/100
40/40 - 0s - loss: 0.9619 - accuracy: 0.7325 - val_loss: 0.9358 - val_accuracy: 0.7930 - 50ms/epoch - 1ms/step
Epoch 6/100
40/40 - 0s - loss: 0.9370 - accuracy: 0.7417 - val_loss: 0.9178 - val_accuracy: 0.7960 - 51ms/epoch - 1ms/step
Epoch 7/100
40/40 - 0s - loss: 0.9147 - accuracy: 0.7533 - val_loss: 0.8856 - val_accuracy: 0.8390 - 52ms/epoch - 1ms/step
Epoch 8/100
40/40 - 0s - loss: 0.8883 - accuracy: 0.7750 - val_loss: 0.8759 - val_accuracy: 0.8050 - 50ms/epoch - 1ms/step
Epoch 9/100
40/40 - 0s - loss: 0.8677 - accuracy: 0.7869 - val_loss: 0.8385 - val_accuracy: 0.8620 - 50ms/epoch - 1ms/step
Epoch 10/100
40/40 - 0s - loss: 0.8447 - accuracy: 0.7985 - val_loss: 0.8250 - val_accuracy: 0.8740 - 51ms/epoch - 1ms/step
Epoch 11/100
40/40 - 0s - loss: 0.8223 - accuracy: 0.8187 - val_loss: 0.7962 - val_accuracy: 0.8900 - 50ms/epoch - 1ms/step
Epoch 12/100
40/40 - 0s - loss: 0.8005 - accuracy: 0.8307 - val_loss: 0.7702 - val_accuracy: 0.8770 - 51ms/epoch - 1ms/step
Epoch 13/100
40/40 - 0s - loss: 0.7819 - accuracy: 0.8451 - val_loss: 0.7630 - val_accuracy: 0.9050 - 51ms/epoch - 1ms/step
Epoch 14/100
40/40 - 0s - loss: 0.7603 - accuracy: 0.8545 - val_loss: 0.7396 - val_accuracy: 0.9430 - 52ms/epoch - 1ms/step
Epoch 15/100
40/40 - 0s - loss: 0.7433 - accuracy: 0.8655 - val_loss: 0.7216 - val_accuracy: 0.9160 - 49ms/epoch - 1ms/step
Epoch 16/100
40/40 - 0s - loss: 0.7211 - accuracy: 0.8733 - val_loss: 0.7044 - val_accuracy: 0.9310 - 49ms/epoch - 1ms/step
Epoch 17/100
40/40 - 0s - loss: 0.7021 - accuracy: 0.8857 - val_loss: 0.6917 - val_accuracy: 0.9140 - 48ms/epoch - 1ms/step
Epoch 18/100
40/40 - 0s - loss: 0.6813 - accuracy: 0.8992 - val_loss: 0.6558 - val_accuracy: 0.9610 - 48ms/epoch - 1ms/step
Epoch 19/100
40/40 - 0s - loss: 0.6619 - accuracy: 0.9069 - val_loss: 0.6314 - val_accuracy: 0.9690 - 50ms/epoch - 1ms/step
Epoch 20/100
40/40 - 0s - loss: 0.6459 - accuracy: 0.9136 - val_loss: 0.6320 - val_accuracy: 0.9470 - 49ms/epoch - 1ms/step
Epoch 21/100
40/40 - 0s - loss: 0.6268 - accuracy: 0.9189 - val_loss: 0.6050 - val_accuracy: 0.9510 - 50ms/epoch - 1ms/step
Epoch 22/100
40/40 - 0s - loss: 0.6128 - accuracy: 0.9240 - val_loss: 0.5894 - val_accuracy: 0.9640 - 50ms/epoch - 1ms/step
Epoch 23/100
40/40 - 0s - loss: 0.5957 - accuracy: 0.9316 - val_loss: 0.5768 - val_accuracy: 0.9460 - 51ms/epoch - 1ms/step
Epoch 24/100
40/40 - 0s - loss: 0.5779 - accuracy: 0.9373 - val_loss: 0.5567 - val_accuracy: 0.9800 - 52ms/epoch - 1ms/step
Epoch 25/100
40/40 - 0s - loss: 0.5627 - accuracy: 0.9482 - val_loss: 0.5417 - val_accuracy: 0.9810 - 51ms/epoch - 1ms/step
Epoch 26/100
40/40 - 0s - loss: 0.5472 - accuracy: 0.9531 - val_loss: 0.5256 - val_accuracy: 0.9850 - 51ms/epoch - 1ms/step
Epoch 27/100
40/40 - 0s - loss: 0.5310 - accuracy: 0.9565 - val_loss: 0.5142 - val_accuracy: 0.9930 - 48ms/epoch - 1ms/step
Epoch 28/100
40/40 - 0s - loss: 0.5165 - accuracy: 0.9615 - val_loss: 0.4979 - val_accuracy: 0.9950 - 49ms/epoch - 1ms/step
Epoch 29/100
40/40 - 0s - loss: 0.5027 - accuracy: 0.9607 - val_loss: 0.4729 - val_accuracy: 0.9950 - 48ms/epoch - 1ms/step
Epoch 30/100
40/40 - 0s - loss: 0.4878 - accuracy: 0.9678 - val_loss: 0.4705 - val_accuracy: 0.9820 - 50ms/epoch - 1ms/step
Epoch 31/100
40/40 - 0s - loss: 0.4735 - accuracy: 0.9729 - val_loss: 0.4506 - val_accuracy: 0.9980 - 50ms/epoch - 1ms/step
Epoch 32/100
40/40 - 0s - loss: 0.4590 - accuracy: 0.9755 - val_loss: 0.4437 - val_accuracy: 0.9950 - 50ms/epoch - 1ms/step
Epoch 33/100
40/40 - 0s - loss: 0.4456 - accuracy: 0.9788 - val_loss: 0.4208 - val_accuracy: 0.9990 - 49ms/epoch - 1ms/step
Epoch 34/100
40/40 - 0s - loss: 0.4350 - accuracy: 0.9787 - val_loss: 0.4078 - val_accuracy: 0.9990 - 52ms/epoch - 1ms/step
Epoch 35/100
40/40 - 0s - loss: 0.4229 - accuracy: 0.9805 - val_loss: 0.3946 - val_accuracy: 1.0000 - 53ms/epoch - 1ms/step
Epoch 36/100
40/40 - 0s - loss: 0.4086 - accuracy: 0.9849 - val_loss: 0.3866 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 37/100
40/40 - 0s - loss: 0.3979 - accuracy: 0.9851 - val_loss: 0.3743 - val_accuracy: 0.9990 - 50ms/epoch - 1ms/step
Epoch 38/100
40/40 - 0s - loss: 0.3857 - accuracy: 0.9862 - val_loss: 0.3649 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 39/100
40/40 - 0s - loss: 0.3732 - accuracy: 0.9867 - val_loss: 0.3580 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 40/100
40/40 - 0s - loss: 0.3646 - accuracy: 0.9885 - val_loss: 0.3477 - val_accuracy: 0.9990 - 51ms/epoch - 1ms/step
Epoch 41/100
40/40 - 0s - loss: 0.3535 - accuracy: 0.9896 - val_loss: 0.3347 - val_accuracy: 0.9990 - 50ms/epoch - 1ms/step
Epoch 42/100
40/40 - 0s - loss: 0.3409 - accuracy: 0.9926 - val_loss: 0.3221 - val_accuracy: 1.0000 - 53ms/epoch - 1ms/step
Epoch 43/100
40/40 - 0s - loss: 0.3319 - accuracy: 0.9928 - val_loss: 0.3220 - val_accuracy: 0.9990 - 52ms/epoch - 1ms/step
Epoch 44/100
40/40 - 0s - loss: 0.3236 - accuracy: 0.9898 - val_loss: 0.2948 - val_accuracy: 1.0000 - 49ms/epoch - 1ms/step
Epoch 45/100
40/40 - 0s - loss: 0.3122 - accuracy: 0.9935 - val_loss: 0.2882 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 46/100
40/40 - 0s - loss: 0.3022 - accuracy: 0.9947 - val_loss: 0.2867 - val_accuracy: 1.0000 - 48ms/epoch - 1ms/step
Epoch 47/100
40/40 - 0s - loss: 0.2935 - accuracy: 0.9926 - val_loss: 0.2726 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 48/100
40/40 - 0s - loss: 0.2831 - accuracy: 0.9947 - val_loss: 0.2601 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 49/100
40/40 - 0s - loss: 0.2762 - accuracy: 0.9936 - val_loss: 0.2500 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 50/100
40/40 - 0s - loss: 0.2667 - accuracy: 0.9941 - val_loss: 0.2416 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 51/100
40/40 - 0s - loss: 0.2588 - accuracy: 0.9950 - val_loss: 0.2482 - val_accuracy: 1.0000 - 53ms/epoch - 1ms/step
Epoch 52/100
40/40 - 0s - loss: 0.2500 - accuracy: 0.9958 - val_loss: 0.2460 - val_accuracy: 0.9990 - 55ms/epoch - 1ms/step
Epoch 53/100
40/40 - 0s - loss: 0.2414 - accuracy: 0.9968 - val_loss: 0.2223 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 54/100
40/40 - 0s - loss: 0.2345 - accuracy: 0.9963 - val_loss: 0.2159 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 55/100
40/40 - 0s - loss: 0.2258 - accuracy: 0.9973 - val_loss: 0.2031 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 56/100
40/40 - 0s - loss: 0.2187 - accuracy: 0.9968 - val_loss: 0.1984 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 57/100
40/40 - 0s - loss: 0.2113 - accuracy: 0.9968 - val_loss: 0.1961 - val_accuracy: 1.0000 - 53ms/epoch - 1ms/step
Epoch 58/100
40/40 - 0s - loss: 0.2039 - accuracy: 0.9982 - val_loss: 0.1915 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 59/100
40/40 - 0s - loss: 0.1965 - accuracy: 0.9977 - val_loss: 0.1761 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 60/100
40/40 - 0s - loss: 0.1896 - accuracy: 0.9978 - val_loss: 0.1704 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 61/100
40/40 - 0s - loss: 0.1828 - accuracy: 0.9988 - val_loss: 0.1594 - val_accuracy: 1.0000 - 55ms/epoch - 1ms/step
Epoch 62/100
40/40 - 0s - loss: 0.1759 - accuracy: 0.9974 - val_loss: 0.1668 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 63/100
40/40 - 0s - loss: 0.1702 - accuracy: 0.9983 - val_loss: 0.1489 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 64/100
40/40 - 0s - loss: 0.1633 - accuracy: 0.9987 - val_loss: 0.1475 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 65/100
40/40 - 0s - loss: 0.1591 - accuracy: 0.9983 - val_loss: 0.1417 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 66/100
40/40 - 0s - loss: 0.1530 - accuracy: 0.9988 - val_loss: 0.1387 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 67/100
40/40 - 0s - loss: 0.1482 - accuracy: 0.9989 - val_loss: 0.1258 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 68/100
40/40 - 0s - loss: 0.1431 - accuracy: 0.9985 - val_loss: 0.1264 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 69/100
40/40 - 0s - loss: 0.1376 - accuracy: 0.9982 - val_loss: 0.1148 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 70/100
40/40 - 0s - loss: 0.1319 - accuracy: 0.9985 - val_loss: 0.1185 - val_accuracy: 1.0000 - 49ms/epoch - 1ms/step
Epoch 71/100
40/40 - 0s - loss: 0.1271 - accuracy: 0.9994 - val_loss: 0.1088 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 72/100
40/40 - 0s - loss: 0.1228 - accuracy: 0.9989 - val_loss: 0.1002 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 73/100
40/40 - 0s - loss: 0.1181 - accuracy: 0.9992 - val_loss: 0.1029 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 74/100
40/40 - 0s - loss: 0.1151 - accuracy: 0.9992 - val_loss: 0.0953 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 75/100
40/40 - 0s - loss: 0.1100 - accuracy: 0.9990 - val_loss: 0.0917 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 76/100
40/40 - 0s - loss: 0.1061 - accuracy: 0.9990 - val_loss: 0.0865 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 77/100
40/40 - 0s - loss: 0.1017 - accuracy: 0.9990 - val_loss: 0.0839 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 78/100
40/40 - 0s - loss: 0.0986 - accuracy: 0.9990 - val_loss: 0.0805 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 79/100
40/40 - 0s - loss: 0.0954 - accuracy: 0.9990 - val_loss: 0.0748 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 80/100
40/40 - 0s - loss: 0.0910 - accuracy: 0.9992 - val_loss: 0.0814 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 81/100
40/40 - 0s - loss: 0.0882 - accuracy: 0.9995 - val_loss: 0.0712 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 82/100
40/40 - 0s - loss: 0.0844 - accuracy: 0.9997 - val_loss: 0.0658 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 83/100
40/40 - 0s - loss: 0.0810 - accuracy: 0.9996 - val_loss: 0.0640 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 84/100
40/40 - 0s - loss: 0.0772 - accuracy: 0.9999 - val_loss: 0.0601 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 85/100
40/40 - 0s - loss: 0.0764 - accuracy: 0.9994 - val_loss: 0.0625 - val_accuracy: 1.0000 - 49ms/epoch - 1ms/step
Epoch 86/100
40/40 - 0s - loss: 0.0723 - accuracy: 0.9997 - val_loss: 0.0556 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 87/100
40/40 - 0s - loss: 0.0701 - accuracy: 0.9995 - val_loss: 0.0521 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 88/100
40/40 - 0s - loss: 0.0666 - accuracy: 0.9997 - val_loss: 0.0527 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 89/100
40/40 - 0s - loss: 0.0644 - accuracy: 0.9995 - val_loss: 0.0487 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 90/100
40/40 - 0s - loss: 0.0627 - accuracy: 0.9991 - val_loss: 0.0448 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 91/100
40/40 - 0s - loss: 0.0594 - accuracy: 0.9995 - val_loss: 0.0438 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 92/100
40/40 - 0s - loss: 0.0574 - accuracy: 0.9997 - val_loss: 0.0405 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 93/100
40/40 - 0s - loss: 0.0554 - accuracy: 0.9996 - val_loss: 0.0431 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 94/100
40/40 - 0s - loss: 0.0532 - accuracy: 0.9996 - val_loss: 0.0399 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 95/100
40/40 - 0s - loss: 0.0519 - accuracy: 0.9998 - val_loss: 0.0372 - val_accuracy: 1.0000 - 50ms/epoch - 1ms/step
Epoch 96/100
40/40 - 0s - loss: 0.0498 - accuracy: 0.9997 - val_loss: 0.0361 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 97/100
40/40 - 0s - loss: 0.0478 - accuracy: 0.9999 - val_loss: 0.0355 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
Epoch 98/100
40/40 - 0s - loss: 0.0465 - accuracy: 0.9997 - val_loss: 0.0339 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 99/100
40/40 - 0s - loss: 0.0446 - accuracy: 0.9999 - val_loss: 0.0304 - val_accuracy: 1.0000 - 52ms/epoch - 1ms/step
Epoch 100/100
40/40 - 0s - loss: 0.0434 - accuracy: 0.9997 - val_loss: 0.0341 - val_accuracy: 1.0000 - 51ms/epoch - 1ms/step
<keras.callbacks.History at 0x7f63906cd790>

As we see, the network is essentially perfect now.