2023-05-15 22:42:04 +00:00
|
|
|
import numpy as np
|
|
|
|
from tensorflow import keras
|
|
|
|
|
|
|
|
import keras_core
|
|
|
|
|
|
|
|
NUM_CLASSES = 10
|
|
|
|
|
|
|
|
|
|
|
|
def build_mnist_data(num_classes):
|
|
|
|
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
|
|
|
|
|
|
|
|
# Scale images to the [0, 1] range
|
|
|
|
x_train = x_train.astype("float32") / 255
|
|
|
|
x_test = x_test.astype("float32") / 255
|
|
|
|
# Make sure images have shape (28, 28, 1)
|
|
|
|
x_train = np.expand_dims(x_train, -1)
|
|
|
|
x_test = np.expand_dims(x_test, -1)
|
|
|
|
|
|
|
|
# convert class vectors to binary class matrices
|
|
|
|
y_train = keras.utils.to_categorical(y_train, num_classes)
|
|
|
|
y_test = keras.utils.to_categorical(y_test, num_classes)
|
|
|
|
|
|
|
|
print("x_train shape:", x_train.shape)
|
|
|
|
print(x_train.shape[0], "train samples")
|
|
|
|
print(x_test.shape[0], "test samples")
|
2023-05-15 23:19:58 +00:00
|
|
|
|
2023-05-15 22:42:04 +00:00
|
|
|
return x_train, y_train, x_test, y_test
|
|
|
|
|
|
|
|
|
|
|
|
def build_keras_model(keras_module, num_classes):
|
|
|
|
input_shape = (28, 28, 1)
|
|
|
|
|
|
|
|
model = keras_module.Sequential(
|
|
|
|
[
|
|
|
|
keras_module.Input(shape=input_shape),
|
2023-05-15 23:19:58 +00:00
|
|
|
keras_module.layers.Conv2D(
|
|
|
|
32, kernel_size=(3, 3), activation="relu"
|
|
|
|
),
|
2023-05-15 22:42:04 +00:00
|
|
|
keras_module.layers.MaxPooling2D(pool_size=(2, 2)),
|
2023-05-15 23:19:58 +00:00
|
|
|
keras_module.layers.Conv2D(
|
|
|
|
64, kernel_size=(3, 3), activation="relu"
|
|
|
|
),
|
2023-05-15 22:42:04 +00:00
|
|
|
keras_module.layers.MaxPooling2D(pool_size=(2, 2)),
|
|
|
|
keras_module.layers.Flatten(),
|
|
|
|
keras_module.layers.Dense(num_classes, activation="softmax"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
model.summary()
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def train_model(model, x, y):
|
2023-05-15 23:33:35 +00:00
|
|
|
batch_size = 256
|
2023-05-15 22:42:04 +00:00
|
|
|
epochs = 1
|
|
|
|
|
2023-05-15 23:19:58 +00:00
|
|
|
model.compile(
|
|
|
|
loss="mse", optimizer="adam", metrics=["accuracy"], jit_compile=False
|
|
|
|
)
|
2023-05-15 22:42:04 +00:00
|
|
|
|
2023-05-15 23:19:58 +00:00
|
|
|
return model.fit(
|
2023-05-15 23:33:35 +00:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
batch_size=batch_size,
|
|
|
|
epochs=epochs,
|
|
|
|
validation_split=0.1,
|
|
|
|
shuffle=False,
|
2023-05-15 23:19:58 +00:00
|
|
|
)
|
2023-05-15 22:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def eval_model(model, x, y):
|
|
|
|
score = model.evaluate(x, y, verbose=0)
|
|
|
|
print("Test loss:", score[0])
|
|
|
|
print("Test accuracy:", score[1])
|
|
|
|
return score
|
|
|
|
|
|
|
|
|
|
|
|
def numerical_test():
|
|
|
|
x_train, y_train, x_test, y_test = build_mnist_data(NUM_CLASSES)
|
|
|
|
keras_model = build_keras_model(keras, NUM_CLASSES)
|
|
|
|
keras_core_model = build_keras_model(keras_core, NUM_CLASSES)
|
|
|
|
|
|
|
|
# Make sure both model have same weights before training
|
|
|
|
keras_core_model.set_weights(keras_model.weights)
|
|
|
|
|
|
|
|
for kw, kcw in zip(keras_model.weights, keras_core_model.weights):
|
|
|
|
np.testing.assert_allclose(kw, kcw)
|
|
|
|
|
|
|
|
keras_history = train_model(keras_model, x_train, y_train)
|
|
|
|
keras_core_history = train_model(keras_core_model, x_train, y_train)
|
|
|
|
|
2023-05-15 23:33:35 +00:00
|
|
|
for key in keras_history.history.keys():
|
|
|
|
np.testing.assert_allclose(
|
|
|
|
keras_history.history[key],
|
|
|
|
keras_core_history.history[key],
|
|
|
|
atol=1e-3,
|
|
|
|
)
|
2023-05-15 22:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-05-15 23:19:58 +00:00
|
|
|
keras.utils.set_random_seed(1337)
|
|
|
|
keras_core.utils.set_random_seed(1337)
|
2023-05-15 22:42:04 +00:00
|
|
|
numerical_test()
|