Example Analysis of basic Operation of PyTorch
This article mainly introduces PyTorch basic operation example analysis, the article is very detailed, has a certain reference value, interested friends must read!
create data
torch.empty()
Create an empty tensor matrix.
Format:
torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor
Parameters:
size: The shape of the generator matrix, required
dtype: Data type, default is None
Examples:
#Create a matrix a = torch.empty(2,2)print(a)#Create a matrix b = torch.empty(3,3)print(b) of shape [3,3]
Output:
tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
torch.zeros()
Create a matrix of all zeros.
Format:
torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Parameters:
size: The shape of the generator matrix, required
dtype: Data type, default is None
Examples:
#Create an array of all zeros of shape [2,2] a = torch.zeros([2,2], dtype= torch. float32)print(a)#Create an array of all zeros of shape [3,3] b = torch.zeros([3,3], dtype= torch. float32)print(b)
Output:
tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
torch.ones()
Create an all-one matrix.
Format:
torch.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Parameters:
size: The shape of the generator matrix, required
dtype: Data type, default is None
Examples:
#Create an all-one array of shape [2,2] a = torch.ones([2,2], dtype= torch. float32)print(a)#Create an all-one array of shape [3,3] b = torch.ones([3,3], dtype= torch. float32)print(b)
Output:
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
torch.tensor()
Create tensors from data.
Format:
torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor
Parameters:
data: data (array, tuple, ndarray, scalar)
dtype: Data type, default is None
Examples:
#Create tensor from data array = np.arange(1, 10).reshape(3, 3)print(array)print(type(array))tensor = torch.tensor(array)print(tensor) print(type(tensor))
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.int32)
torch.rand()
Create a tensor matrix of random numbers 0~1.
Format:
torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Parameters:
size: The shape of the generator matrix, required
dtype: data type, default is None
Examples:
#Create a random number matrix of shape [2, 2] rand = torch.rand(2, 2)print(rand)
Output:
tensor([[0.6209, 0.3424],
[0.3506, 0.7986]])
mathematical operations
torch.add()
Returns the added tensor.
Format:
torch.add(input, other, *, out=None) → Tensor
Examples:
#tensor addition input1 = torch.tensor([[1, 2], [3, 4]])print(input1) input2 = torch.tensor([[4, 3], [2, 1])print(input2)output = torch.add(input1, input2)print(output)
Output:
tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[5, 5],
[5, 5]])
Note: The tensor shapes must be consistent, otherwise an error will be reported.
torch.sub()
Returns the subtracted tensor.
Examples:
#tensor subtract input1 = torch.tensor([[1, 2], [3, 4]])print(input1) input2 = torch.tensor([[4, 3], [2, 1])print(input2)output = torch.sub(input1, input2)print(output)
Output:
tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[-3, -1],
[ 1, 3]])
torch.matmul()
Examples:
#tensor matrix multiplication input1 = torch.tensor([[1, 1, 1])print(input1) input2 = torch.tensor([[3], [3], [3])print(input2)output = torch.matmul(input1, input2)print(output)
Output:
tensor([[1, 1, 1]])
tensor([[3],
[3],
[3]])
tensor([[9]])
index operations
An index can help us quickly find specific information in a tensor.
Examples:
#simple index operations ones = torch.ones([3, 3])print(ones[: 2])print(ones[:, : 2])
Debug output:
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 1.],
[1., 1.],
[1., 1.]])
That's all for "PyTorch Basic Operation Sample Analysis". Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!