Introduction

Gal Curious

I've heard about neural networks in programming. How can I create a simple one? 🧠

Geek Smiling

Neural networks are the foundation of deep learning. Let's create a simple one using Python and PyTorch!

Gal Happy

Sounds fun! Let's get started! 🚀

Step 1: Import Libraries

Gal Eager

What libraries do we need?

Geek Nodding

We'll use PyTorch and torchvision for this example.

import torch
import torch.nn as nn
import torchvision

Step 2: Define the Neural Network

Gal Excited

Now, let's create the neural network!

Geek Smiling

We'll create a simple feedforward neural network with one hidden layer.

class SimpleNeuralNetwork(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleNeuralNetwork, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

Step 3: Create an Instance of the Neural Network

Gal Wondering

How do we use our neural network?

Geek Happy

First, we create an instance with the desired input, hidden, and output sizes.

input_size = 784
hidden_size = 100
output_size = 10

model = SimpleNeuralNetwork(input_size, hidden_size, output_size)

Step 4: Define Loss and Optimizer

Gal Curious

What's next?

Geek Smiling

We need to define a loss function and an optimizer to train the network.

loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

Step 5: Train the Neural Network

Gal Excited

Time for training! 🏋️‍♀️

Geek Nodding

We'll use a dataset, like MNIST, to train our neural network. For simplicity, we won't include the dataset loading code here.

num_epochs = 5

for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.view(-1, 28*28)

        # Forward pass
        outputs = model(images)
        loss = loss_function(outputs, labels)

        # Backward pass
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

Conclusion

Great job! You’ve created a simple neural network using Python and PyTorch. We defined the network, created an instance, defined loss and optimizer, and trained the network. Keep exploring and have fun with neural networks! 🎉