- Published on
PyTorch 基本操作
PyTorch 基本操作
广播机制
PyTorch 中一个特别强大的机制是广播机制,这一功能使该框架在进行算术运算时能够处理不同形状的数组。它会扩展较小的数组以匹配较大的数组,从而使逐元素操作能够顺利进行。这一特性显著增强了数组操作的灵活性与便捷性。
Rank: This simply tells us the number of dimensions in a tensor. A scalar has rank 0, a vector has rank 1, a matrix has rank 2, and a tensor has rank 3 or more. Shape: The shape of a tensor is the number of elements in each dimension. Size: The total number of items in the tensor, which can be computed as a product of the elements of the shape.
秩:这仅仅告知我们张量中维度的数量。标量的秩为0,向量的秩为1,矩阵的秩为2,而张量的秩为3或更高。 形状:张量的形状指的是每个维度中的元素数量。 大小:张量中元素的总数,可通过将形状中的各元素相乘得出。
Empty Tensor: torch.empty(size): Returns a tensor of given size filled with uninitialized data. Here, size is a tuple defining the dimension of the tensor. Zero Tensor: torch.zeros(size): Returns a tensor filled with zeroes. Ones Tensor: torch.ones(size): Returns a tensor filled with ones. Random Tensor: torch.rand(size): Returns a tensor filled with random numbers from a uniform distribution in the range [0, 1).
import torch
zero_tensor = torch.zeros(3,2)
ones_tensor = torch.ones(3,2)
random_tensor = torch.rand(3,2)
ones_tensor = torch.to(torch.float64)
print(ones_tensor.dtype)
tensor1 = torch.tensor([1,2,3,4], dtype=torch.float32)
tensor2 = torch.tensor([5,6,7,8], dtype=torch.float32)
result = tensor1 + tensor2
result = tensor1 - tensor2
result = tensor1 * tensor2
result = tensor1 / tensor2
Tensor Manipulation
Reshaping
is a common operation, which allows us to restructure our data to have different numbers of dimensions or different sizes for each dimension.
tensor = torch.arange(9) # tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
reshaped_tensor = tensor.reshape(3, 3)
reshaped_tensor = tensor.view(3, 3) # Another way to reshape
Slicing
allows us to extract a portion of the tensor. The slicing syntax in PyTorch is quite similar to that in Python and NumPy.
sliced_tensor = reshaped_tensor[0:2, 0:2]
Joining
# use torch.cat() to concatenate two tensors along a given dimension
tensor1 = torch.tensor([1, 2, 3])
tensor2 = torch.tensor([4, 5, 6])
concatenated_tensor = torch.cat((tensor1, tensor2))
Transposing
Transposing a tensor swaps its dimensions. This is particularly useful for changing the orientation of matrices or higher-dimensional tensors.
import torch
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])
transposed_matrix = torch.transpose(matrix, 0, 1) # Transpose dimensions 0 and 1
# or simply
transposed_matrix = matrix.T
print("Original Matrix:")
print(matrix)
print("Transposed Matrix:")
print(transposed_matrix)
Matrix Operations
matrix1 = torch.tensor([[1, 2], [3, 4]])
matrix2 = torch.tensor([[5, 6], [7, 8]])
result = torch.matmul(matrix1, matrix2)
result = matrix1 @ matrix2 #Matrix multiplication using @ operator
Aggregation Operations
Aggregation operations are used to compute a single value from a tensor, such as the sum, mean, or maximum.
import torch
# Create a tensor
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
# Sum of all elements
total_sum = torch.sum(tensor) # Returns 21
# Mean of all elements
mean_value = torch.mean(tensor.float()) # Returns 3.5
# Maximum value in the tensor
max_value = torch.max(tensor) # Returns 6
# Minimum value in the tensor
min_value = torch.min(tensor) # Returns 1
# Sum along a specific dimension
sum_dim0 = torch.sum(tensor, dim=0) # Sum along rows
sum_dim1 = torch.sum(tensor, dim=1) # Sum along columns
# Mean along a specific dimension
mean_dim0 = torch.mean(tensor.float(), dim=0) # Mean along rows
mean_dim1 = torch.mean(tensor.float(), dim=1) # Mean along columns
# Maximum along a specific dimension
max_dim0 = torch.max(tensor, dim=0) # Max along rows
max_dim1 = torch.max(tensor, dim=1) # Max along columns
# Minimum along a specific dimension
min_dim0 = torch.min(tensor, dim=0) # Min along rows
min_dim1 = torch.min(tensor, dim=1) # Min along columns
THE END