Getting started with PyTorch
Quickstart - PyTorch
Install PyTorch on your machine. If you have Anaconda installed, you can install PyTorch by running the following command:
conda install pytorch torchvision -c pytorch
Import PyTorch in your Python script by adding the following line at the top:
import torch
Check if PyTorch is working by running the following code:
x = torch.Tensor([5, 3])
y = torch.Tensor([2, 1])
print(x*y)
This should print tensor([10., 3.])
PyTorch uses tensors, which are similar to NumPy's ndarrays, to store and manipulate data. You can create a tensor by passing a list or array to the torch.Tensor
function. For example:
x = torch.Tensor([[1, 2, 3], [4, 5, 6]])
print(x)
This should print ```tensor([[1., 2., 3.], [4., 5., 6.]])``
You can also create tensors with random values using the torch.rand
function. For example:
x = torch.rand(3, 3)
print(x)
This will create a 3x3 tensor with random values between 0 and 1.
You can perform mathematical operations on tensors just like you would with NumPy arrays. For example:
x = torch.Tensor([[1, 2, 3], [4, 5, 6]])
y = torch.Tensor([[1, 1, 1], [2, 2, 2]])
z = x + y
print(z)
This should print ```tensor([[2., 3., 4.], [6., 7., 8.]])``
You can also perform operations on tensors using PyTorch's built-in functions. For example:
x = torch.Tensor([[1, 2, 3], [4, 5, 6]])
y = torch.mean(x, dim=1)
print(y)
This should print tensor([2., 5.])
, which is the mean of each row of x.
I hope this tutorial helps you get started with PyTorch. For more details visit PyTorch github Documentation