ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

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 NumPy: From Basics to Advanced

    25/09/2024 · Python

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 · Python

  • Mastering Computer Vision with OpenCV

    06/12/2024 · Python

  • Python Basics: Comprehensive Guide

    21/09/2024 · Python

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 · Python

Related articles

  • Enhancing LlamaIndex

    05/11/2024 · Python

  • Introduction to Streamlit

    15/11/2024 · Python

  • Understanding Core Concepts of Scikit-learn

    15/11/2024 · Python

  • Maximizing Efficiency

    05/11/2024 · Python

  • LangChain and Large Language Models

    26/10/2024 · Python

  • Mastering Chains

    26/10/2024 · Python

  • Managing Model Outputs and Predictions in Hugging Face Transformers

    14/11/2024 · Python

Popular category

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