# sequential model with two dense layers, # A Sequential model in TensorFlow is a linear stack of neural network # layers, where each layer is added one at a time. # Different types of layers, such as Dense layers (also called fully connected # layers), Convolutional layers, Pooling layers, and Recurrent layers can be # added. Also the activation function and other parameters for each layer can # be configured. import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt import numpy as np # Load the MNIST Fashion dataset (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() # Normalize pixel values to between 0 and 1 x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 # Define the sequential model model = keras.Sequential([ keras.layers.Flatten(input_shape=(28, 28)), keras.layers.Dense(128, activation='relu'), keras.layers.Dense(64, activation='relu'), keras.layers.Dense(10) ]) # Compile the model model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # Train the model history = model.fit(x_train, y_train, epochs=15, validation_split=0.2) # Evaluate the model on the test set test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2) print("Test accuracy:", test_acc) # Plot the training and validation accuracy and loss over time plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.plot(history.history["accuracy"]) plt.plot(history.history["val_accuracy"]) plt.title("Model accuracy") plt.ylabel("Accuracy") plt.xlabel("Epoch") plt.legend(["Train", "Validation"], loc="lower right") plt.subplot(1, 2, 2) plt.plot(history.history["loss"]) plt.plot(history.history["val_loss"]) plt.title("Model loss") plt.ylabel("Loss") plt.xlabel("Epoch") plt.legend(["Train", "Validation"], loc="upper right") plt.show() # Plot a random sample of test set images and their predicted labels random_indices = np.random.choice(x_test.shape[0], 16, replace=False) test_preds = np.argmax(model.predict(x_test), axis=1) plt.figure(figsize=(10, 10)) for i, index in enumerate(random_indices): plt.subplot(4, 4, i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(x_test[index], cmap=plt.cm.binary) plt.xlabel(f"Predicted: {test_preds[index]}\nTrue: {y_test[index]}") plt.show()