### AI Tutorial: Building a Simple Neural Network with TensorFlow and Keras

Equip yourself with the right tools and resources by exploring this forum. Share tutorials, software recommendations, and educational materials that can help beginners and experts alike expand their AI knowledge. Collaborate with others to discover the best practices and resources available.
Post Reply
Mark
Site Admin
Posts: 14
Joined: Thu Mar 20, 2025 3:02 pm

### AI Tutorial: Building a Simple Neural Network with TensorFlow and Keras

Post by Mark »

#### Prerequisites

Before you begin, ensure you have:

1. **Python Installed**: You can download Python from [python.org](https://www.python.org/).
2. **Basic Understanding of Python**: Familiarity with Python programming concepts.
3. **An IDE or Text Editor**: You can use Jupyter Notebook, VSCode, or any editor of your choice.

#### Step 1: Installing Required Libraries

Open your terminal or command prompt and install TensorFlow:

```bash
pip install tensorflow matplotlib
```

- **TensorFlow**: An open-source library for machine learning and deep learning.
- **Matplotlib**: A library for creating static, animated, and interactive visualizations in Python.

#### Step 2: Import Libraries

In your Python script or Jupyter Notebook, start by importing the required libraries:

```python
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
```

#### Step 3: Load the MNIST Dataset

The MNIST dataset is included with Keras, making it easy to load. The dataset consists of 70,000 images of handwritten digits (0 through 9).

```python
# Load the MNIST dataset
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
```

#### Step 4: Preprocess the Data

The images in the dataset are in the format of 28x28 pixels. We'll normalize the images to values between 0 and 1 for better network performance.

```python
# Normalize the pixel values (0-255) to a range of 0-1
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# Check the shape of the data
print(f"x_train shape: {x_train.shape}, y_train shape: {y_train.shape}")
print(f"x_test shape: {x_test.shape}, y_test shape: {y_test.shape}")
```

#### Step 5: Define the Neural Network Model

Now, we will define a simple feedforward neural network using Keras. This network will have:

- An input layer
- Two hidden layers (with ReLU activation)
- An output layer (with softmax activation for multi-class classification)

```python
model = keras.Sequential([
layers.Flatten(input_shape=(28, 28)), # Flatten the input images
layers.Dense(128, activation='relu'), # First hidden layer with ReLU activation
layers.Dense(10, activation='softmax') # Output layer with softmax activation
])
```

#### Step 6: Compile the Model

Before training the model, we need to compile it. We will specify the optimizer, loss function, and metrics we are interested in.

```python
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
```

#### Step 7: Train the Model

Now, it's time to train the model using the training dataset. We'll also validate the performance on the test dataset.

```python
# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model on the test set
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'\nTest accuracy: {test_accuracy}')
```

#### Step 8: Make Predictions

After training, we can use the model to make predictions on the test dataset.

```python
# Make predictions on the test dataset
predictions = model.predict(x_test)

# Output the prediction for the first test image
predicted_label = np.argmax(predictions[0]) # Get the class with the highest predicted probability
print(f'Predicted label for the first image: {predicted_label}')

# Display the first image and its predicted label
plt.imshow(x_test[0], cmap='gray')
plt.title(f'Predicted Label: {predicted_label}')
plt.axis('off')
plt.show()
```
Post Reply