torch operation
Basic operations
Create a tensor:
import torch # Create tensors directly from lists or arraysx = ([1, 2, 3])
Create a tensor of a specific value:
# All zero tensorzeros = (3, 3) # All one quantityones = (3, 3) # Unit tensor (diagonal is 1, the rest is 0)eye = (3) # Random Tensorrand = (3, 3) # Random tensor extracted from a normal distribution with mean of 0 and standard deviation of 1randn = (3, 3)
Create an arithmetic sequence tensor:
# Arithmetic sequence from 0 to 10 (excluding 10), step size of 2arange = (0, 10, 2)
Create sequence tensors for specific intervals:
# 5 numbers evenly distributed from 0 to 10linspace = (0, 10, 5)
2. Addition and subtraction:
# Additionz = x + y # (x, y) print(z) # Output: tensor([5, 7, 9])# Subtractionz = x - y # (x, y) print(z) # Output: tensor([-3, -3, -3])
3. Multiplication and division:
# Element multiplicationz = x * y # (x, y) print(z) # Output: tensor([ 4, 10, 18])# Element divisionz = x / y # (x, y) print(z) # Output: tensor([0.2500, 0.4000, 0.5000])
4. Matrix multiplication:
a = ([[1, 2], [3, 4]]) b = ([[5, 6], [7, 8]]) #Matrix Multiplicationz = (a, b) # or a @ bprint(z) # Output: tensor([[19, 22], [43, 50]])
Shape operation
1. Change the shape:view()
a = ([[1, 2, 3], [4, 5, 6]]) # Change shapeb = (3, 2) print(b) # Output:# tensor([[1, 2], # [3, 4], # [5, 6]])
Automatic size adjustment (parameter-1)
A parameter specified in the view is -1, which means that the number of elements in this dimension is automatically adjusted to ensure that the total number of elements remains unchanged.
import torch x1 = (0,16) print(x1) #a1: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) ------------------------------------------------------------------------------------------------------ x2 = (-1, 16) x3 = (-1, 8) x4 = (-1, 4) x5 = (-1, 2) print(x2) print(x3) print(x4) print(x5) x2: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]) x3: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15]]) x4: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) x5: tensor([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15]])
2. Splicing:
x = ([[1, 2], [3, 4]]) y = ([[5, 6], [7, 8]]) # Splicing along the 0 axis (vertical splicing)z = ((x, y), dim=0) print(z) # Output:# tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]]) # Splice along the 1 axis (horizontal splice)z = ((x, y), dim=1) print(z) # Output:# tensor([[1, 2, 5, 6], # [3, 4, 7, 8]])
3. Slice:
a = ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Sliceb = a[:, 1] # Column 2print(b) # Output: tensor([2, 5, 8])
Mathematical operations
Ask for sum:sum()
x = ([1, 2, 3, 4]) #Sumsum_x = (x) print(sum_x) # Output: tensor(10)
Mean:mean()
# Averagemean_x = (()) # Convert to floating point number typeprint(mean_x) # Output: tensor(2.5000)
Maximum and minimum values:max(),min()
# Maximum valuemax_x = (x) print(max_x) # Output: tensor(4)# Minimum valuemin_x = (x) print(min_x) # Output: tensor(1)
Logical operations
Comparative operations:
x = ([1, 2, 3]) y = ([2, 2, 2]) # Greater thanprint(x > y) # Output: tensor([False, False, True])# Smallprint(x < y) # Output: tensor([ True, False, False])
Logical and or:
a = ([True, False, True]) b = ([False, False, True]) #Logistics andc = torch.logical_and(a, b) print(c) # Output: tensor([False, False, True])# Logical orc = torch.logical_or(a, b) print(c) # Output: tensor([ True, False, True])
Common advanced operations
Broadcasting mechanism:
a = ([1, 2, 3]) b = ([[1], [2], [3]]) # Broadcasting mechanismc = a + b print(c) # Output:# tensor([[2, 3, 4], # [3, 4, 5], # [4, 5, 6]])
Automatic gradient calculation:
x = ([1.0, 2.0, 3.0], requires_grad=True) # Forward communicationy = x + 2 z = y * y * 2 out = () # Backpropagation() print() # Output: tensor([4.6667, 6.0000, 7.3333])
Random number generation
Generate random tensors from standard normal distribution:
randn_tensor = (3, 3) # Generate a random tensor of shape (3, 3) to obey the standard normal distributionprint(randn_tensor) #tensor([[ 1.2335, -0.3941, 0.8990], # [ 0.0470, -1.2671, 0.3248], # [-0.4062, -0.6862, 0.1314]])
Generate random arrangements:
randperm_tensor = (10) # Generate a random permutation from 0 to 9print(randperm_tensor) #tensor([2, 0, 5, 1, 8, 6, 3, 4, 7, 9])
Generate arithmetic sequence tensor:
arange_tensor = (0, 10, 2) # Generate an arithmetic sequence from 0 to 10 (excluding 10), with a step size of 2print(arange_tensor) #tensor([0, 2, 4, 6, 8])
This is the end of this article about the detailed explanation of pytorch torch operation examples. For more related pytorch torch operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!