9951 explained code solutions for 126 technologies


python-pytorchHow can I create a neural network using Python and PyTorch?


To create a neural network using Python and PyTorch, you will need to import the PyTorch library and define the model architecture. The following code block is an example of a basic neural network:

import torch
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(2, 4)
        self.fc2 = nn.Linear(4, 1)

    def forward(self, x):
        x = self.fc1(x)
        x = torch.sigmoid(x)
        x = self.fc2(x)
        x = torch.sigmoid(x)
        return x

net = Net()

The code above consists of the following parts:

  1. import torch - imports the PyTorch library
  2. import torch.nn as nn - imports the neural network module from PyTorch
  3. class Net(nn.Module): - defines a class for the neural network
  4. def __init__(self): - initializes the neural network
  5. self.fc1 = nn.Linear(2, 4) - creates a linear layer with 2 input nodes and 4 output nodes
  6. self.fc2 = nn.Linear(4, 1) - creates a linear layer with 4 input nodes and 1 output node
  7. def forward(self, x): - defines the forward pass of the network
  8. x = self.fc1(x) - applies the linear layer to the input
  9. x = torch.sigmoid(x) - applies the sigmoid activation function to the output of the linear layer
  10. x = self.fc2(x) - applies the second linear layer to the output of the first
  11. x = torch.sigmoid(x) - applies the sigmoid activation function to the output of the second linear layer
  12. return x - returns the output of the network
  13. net = Net() - creates an instance of the neural network

For more information, please refer to the PyTorch documentation.

Edit this code on GitHub