logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Building a Simple Neural Network in PyTorch

author
Generated by
ProCodebase AI

14/11/2024

pytorch

Sign in to read full article

Introduction

PyTorch has become one of the most popular deep learning frameworks due to its flexibility and ease of use. In this tutorial, we'll walk through the process of building a simple neural network using PyTorch. By the end, you'll have a solid understanding of how to create, train, and evaluate a basic neural network.

Setting Up the Environment

Before we dive in, make sure you have PyTorch installed. You can install it using pip:

pip install torch torchvision

Now, let's import the necessary libraries:

import torch import torch.nn as nn import torch.optim as optim

Defining the Neural Network

We'll create a simple feedforward neural network with one hidden layer. Here's how we define it:

class SimpleNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(SimpleNN, self).__init__() self.hidden = nn.Linear(input_size, hidden_size) self.relu = nn.ReLU() self.output = nn.Linear(hidden_size, output_size) def forward(self, x): x = self.hidden(x) x = self.relu(x) x = self.output(x) return x

Let's break down what's happening here:

  1. We define a class SimpleNN that inherits from nn.Module.
  2. In the __init__ method, we create two linear layers and a ReLU activation function.
  3. The forward method defines how data flows through our network.

Preparing the Data

For this example, we'll create some dummy data:

# Generate random input data X = torch.randn(100, 10) # Generate random target data y = torch.randint(0, 2, (100,)).float()

Here, we've created 100 samples with 10 features each, and corresponding binary target values.

Initializing the Model

Now, let's create an instance of our neural network:

input_size = 10 hidden_size = 5 output_size = 1 model = SimpleNN(input_size, hidden_size, output_size)

Defining Loss Function and Optimizer

We'll use Binary Cross Entropy as our loss function and Adam as our optimizer:

criterion = nn.BCEWithLogitsLoss() optimizer = optim.Adam(model.parameters(), lr=0.01)

Training the Model

Let's train our model for 100 epochs:

num_epochs = 100 for epoch in range(num_epochs): # Forward pass outputs = model(X) loss = criterion(outputs.squeeze(), y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() if (epoch + 1) % 10 == 0: print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

This loop does the following:

  1. Performs a forward pass through the network.
  2. Calculates the loss.
  3. Performs backpropagation and updates the weights.
  4. Prints the loss every 10 epochs.

Evaluating the Model

After training, we can evaluate our model:

model.eval() with torch.no_grad(): test_output = model(X) predicted = (test_output.squeeze() > 0.5).float() accuracy = (predicted == y).float().mean() print(f'Accuracy: {accuracy.item():.4f}')

This code:

  1. Sets the model to evaluation mode.
  2. Disables gradient calculation.
  3. Makes predictions on our data.
  4. Calculates and prints the accuracy.

Conclusion

Congratulations! You've just built and trained a simple neural network using PyTorch. This example covers the basics, but PyTorch offers much more for creating complex models and working with large datasets.

As you continue your journey with PyTorch, explore more advanced topics like convolutional neural networks, recurrent neural networks, and transfer learning. Happy coding!

Popular Tags

pytorchneural networksdeep learning

Share now!

Like & Bookmark!

Related Collections

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 | Python

  • Matplotlib Mastery: From Plots to Pro Visualizations

    05/10/2024 | Python

  • Streamlit Mastery: From Basics to Advanced

    15/11/2024 | Python

  • Mastering Computer Vision with OpenCV

    06/12/2024 | Python

  • PyTorch Mastery: From Basics to Advanced

    14/11/2024 | Python

Related Articles

  • Data Manipulation with Pandas

    15/01/2025 | Python

  • Seaborn in Real-world Data Science Projects

    06/10/2024 | Python

  • Mastering Tensor Operations and Manipulation in PyTorch

    14/11/2024 | Python

  • Mastering PyTorch Datasets and DataLoaders

    14/11/2024 | Python

  • Advanced Error Handling and Logging in Python

    15/01/2025 | Python

  • Training Transformers from Scratch

    14/11/2024 | Python

  • Setting Up Your Python and LangChain Development Environment

    26/10/2024 | Python

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design