9951 explained code solutions for 126 technologies


python-pytorchHow can I use Python with PyTorch?


Python is a powerful, high-level, general-purpose programming language. PyTorch is a deep learning framework for Python that provides a set of powerful tools for building and training neural networks. With PyTorch, you can use Python to build, train, and deploy deep learning models.

To use Python with PyTorch, you need to install PyTorch on your system. Once PyTorch is installed, you can import the PyTorch module in your Python code and use it to build and train neural networks.

Here is an example of how to use Python and PyTorch to build and train a simple neural network:

import torch

# define the neural network
model = torch.nn.Sequential(torch.nn.Linear(4, 5),
                            torch.nn.ReLU(),
                            torch.nn.Linear(5, 3))

# define the loss function
loss_fn = torch.nn.MSELoss()

# define the optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# define the input data
x = torch.tensor([[1, 2, 3, 4]])

# define the expected output
y = torch.tensor([[0, 1, 0]])

# train the network
for epoch in range(1000):
    # forward pass
    y_pred = model(x)

    # compute the loss
    loss = loss_fn(y_pred, y)

    # backward pass
    loss.backward()

    # update the parameters
    optimizer.step()

# print the output
print(model(x))

Output example

tensor([[-0.0460,  0.9868, -0.0495]], grad_fn=<AddmmBackward>)

The code above:

  1. Imports the torch module.
  2. Defines a neural network with two linear layers and a ReLU activation function.
  3. Defines a loss function (MSE).
  4. Defines an optimizer (SGD).
  5. Defines the input data.
  6. Defines the expected output.
  7. Trains the network for 1000 epochs.
  8. Prints the output of the network.

Helpful links

Edit this code on GitHub