Here's an example of building a simple neural network using Python and Pandas to classify images of handwritten digits from the MNIST dataset.

This example solves the problem of classifying images of handwritten digits from the MNIST dataset. The goal is to build a neural network that can accurately predict the digit shown in the input image. The problem is a classic machine learning problem and is often used as a benchmark for evaluating different algorithms and models. The example uses a simple neural network with two fully connected layers to achieve high accuracy in classifying the digits.

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

# Load the MNIST dataset
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Flatten the input images
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

# Normalize the data
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255

# Convert the target variable to one-hot encoded vectors
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)

# Define the model architecture
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))

# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)
2023-03-18 11:21:39.682503: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 1s 0us/step
Epoch 1/10
2023-03-18 11:22:01.472441: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2629 - accuracy: 0.9255 - val_loss: 0.1331 - val_accuracy: 0.9611
Epoch 2/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1132 - accuracy: 0.9669 - val_loss: 0.1036 - val_accuracy: 0.9666
Epoch 3/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0782 - accuracy: 0.9762 - val_loss: 0.0863 - val_accuracy: 0.9743
Epoch 4/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0581 - accuracy: 0.9826 - val_loss: 0.0750 - val_accuracy: 0.9770
Epoch 5/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0469 - accuracy: 0.9851 - val_loss: 0.0760 - val_accuracy: 0.9770
Epoch 6/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0357 - accuracy: 0.9883 - val_loss: 0.0723 - val_accuracy: 0.9783
Epoch 7/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0282 - accuracy: 0.9910 - val_loss: 0.0798 - val_accuracy: 0.9766
Epoch 8/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0231 - accuracy: 0.9930 - val_loss: 0.0737 - val_accuracy: 0.9784
Epoch 9/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0188 - accuracy: 0.9941 - val_loss: 0.0880 - val_accuracy: 0.9760
Epoch 10/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0156 - accuracy: 0.9953 - val_loss: 0.0764 - val_accuracy: 0.9785
313/313 [==============================] - 0s 1ms/step - loss: 0.0764 - accuracy: 0.9785
Test accuracy: 0.9785000085830688

Visualize how this neural network works.

Use the matplotlib library to display some of the input images and the corresponding predicted labels. This code will create a 5x5 grid of subplots, each containing an input image from the test set and its predicted label. The np.argmax function is used to find the index of the predicted label with the highest probability.

import matplotlib.pyplot as plt

# Make predictions on the test set
y_pred = model.predict(X_test)

# Plot some of the input images and their predicted labels
fig, axs = plt.subplots(5, 5, figsize=(10, 10))
axs = axs.ravel()
for i in range(25):
    axs[i].imshow(X_test[i].reshape(28, 28), cmap=plt.cm.gray_r)
    axs[i].set_title('Predicted label = {}'.format(np.argmax(y_pred[i])))
    axs[i].axis('off')
plt.show()
313/313 [==============================] - 0s 942us/step

Resources

Lesson to expand on MNIST.

TensorFlow's official MNIST tutorial:TensorFlow is a popular open-source machine learning framework, and their MNIST tutorial provides a great introduction to building neural networks with TensorFlow. You can find the tutorial here: https://www.tensorflow.org/tutorials/keras/classification. Coursera's Deep Learning Specialization: This is a series of courses taught by Andrew Ng that provides a comprehensive introduction to deep learning. The second course in the series, "Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization," covers the MNIST example in detail. You can find the course here: https://www.coursera.org/specializations/deep-learning.

Neural Networks and Deep Learning book: This is a free online book by Michael Nielsen that provides an introduction to neural networks and deep learning. The book includes a chapter on the MNIST example, which provides a detailed explanation of how the neural network works. You can find the book here: http://neuralnetworksanddeeplearning.com/chap1.html.

Fast.ai: This is a set of online courses and resources that provide an introduction to deep learning. The courses include hands-on coding assignments, including an assignment on the MNIST example. You can find the courses here: https://www.fast.ai/.