import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers # Load the MNIST dataset (a collection of handwritten digits) (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # Normalize the pixel values between 0 and 1 x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 # Reshape the input data x_train = x_train.reshape(-1, 28 * 28) x_test = x_test.reshape(-1, 28 * 28) model = keras.Sequential([ layers.Dense(64, activation="relu", input_shape=(28 * 28,)), layers.Dense(10), ]) model.compile(optimizer="adam", loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=["accuracy"]) model.fit(x_train, y_train, batch_size=32, epochs=5, verbose=2) test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2) print("Test accuracy:", test_acc)