|
| 1 | +import tensorflow as tf |
| 2 | +from tensorflow.keras import layers, models |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +# Load the MNIST dataset from Keras |
| 6 | +mnist = tf.keras.datasets.mnist |
| 7 | +(X_train, y_train), (X_test, y_test) = mnist.load_data() |
| 8 | + |
| 9 | +# Normalize the images from 0-255 to 0-1 by dividing by 255 |
| 10 | +X_train, X_test = X_train / 255.0, X_test / 255.0 |
| 11 | + |
| 12 | +# Print TensorFlow and Keras information |
| 13 | +print(f"TensorFlow Version: {tf.__version__}") |
| 14 | +print(f"Keras Layers Module: {layers.__name__}") |
| 15 | +print(f"Keras Models Module: {models.__name__}") |
| 16 | + |
| 17 | +# Build a simple Sequential model |
| 18 | +model = models.Sequential() |
| 19 | + |
| 20 | +# Flatten the 28x28 images into vectors of length 784 |
| 21 | +model.add(layers.Flatten(input_shape=(28, 28))) |
| 22 | + |
| 23 | +# First hidden layer with 128 neurons and ReLU activation |
| 24 | +model.add(layers.Dense(128, activation='relu')) |
| 25 | + |
| 26 | +# Dropout layer to prevent overfitting (randomly drops 20% of neurons) |
| 27 | +model.add(layers.Dropout(0.2)) |
| 28 | + |
| 29 | +# Second hidden layer with 64 neurons and ReLU activation |
| 30 | +model.add(layers.Dense(64, activation='relu')) |
| 31 | + |
| 32 | +# Output layer with 10 neurons (one for each digit class 0-9), softmax for probabilities |
| 33 | +model.add(layers.Dense(10, activation='softmax')) |
| 34 | + |
| 35 | +# Compile the model |
| 36 | +model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) |
| 37 | + |
| 38 | +# Train the model on the MNIST training data |
| 39 | +model.fit(X_train, y_train, epochs=5, batch_size=32) |
| 40 | + |
| 41 | +# Evaluate the model on the test set |
| 42 | +test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2) |
| 43 | +print(f'\nTest accuracy: {test_acc}') |
| 44 | + |
| 45 | +# Make a prediction on a random test image |
| 46 | +random_index = np.random.randint(0, len(X_test)) |
| 47 | +random_image = np.expand_dims(X_test[random_index], axis=0) |
| 48 | +prediction = model.predict(random_image) |
| 49 | +predicted_digit = np.argmax(prediction) |
| 50 | + |
| 51 | +# Print the predicted result and actual label |
| 52 | +print(f'Predicted digit: {predicted_digit}, Actual digit: {y_test[random_index]}') |
| 53 | + |
0 commit comments