Colab/PyTorch - 001 PyTorch Basics-3. 张量介绍

时间:2024-05-09 12:19:38

张量简单来说就是对矩阵的一种称呼。如果熟悉NumPy数组,理解和使用PyTorch张量将会非常容易。标量值由一个零维张量表示。类似地,列/行矩阵使用一维张量表示,以此类推。下面给出了一些不同维度的张量示例,供理解:

在这里插入图片描述
测试代码:PyTorch_for_Beginners

3.1 构建张量

import torch
 
# Create a Tensor with just ones in a column
a = torch.ones(5)
 
# Print the tensor we created
print(a)
 
# tensor([1., 1., 1., 1., 1.])
 
# Create a Tensor with just zeros in a column
b = torch.zeros(5)
print(b)
 
# tensor([0., 0., 0., 0., 0.])

c = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
print(c)
 
# tensor([1., 2., 3., 4., 5.])

d = torch.zeros(3,2)
print(d)
 
# tensor([[0., 0.],
#        [0., 0.],
#        [0., 0.]])
 
e = torch.ones(3,2)
print(e)
 
# tensor([[1., 1.],
#        [1., 1.],
#        [1., 1.]])
 
f = torch.tensor([[1.0, 2.0],[3.0, 4.0]])
print(f)
 
# tensor([[1., 2.],
#        [3., 4.]])
 
# 3D Tensor
g = torch.tensor([[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]])
print(g)
 
# tensor([[[1., 2.],
#         [3., 4.]],
#
#        [[5., 6.],
#         [7., 8.]]])

print(f.shape)
# torch.Size([2, 2])
 
print(e.shape)
# torch.Size([3, 2])
 
print(g.shape)
# torch.Size([2, 2, 2])

3.2 访问张量元素

  • 1D
# Get element at index 2
print(c[2])
 
# tensor(3.)
  • 2D/3D
# All indices starting from 0
 
# Get element at row 1, column 0
print(f[1,0])
# We can also use the following
print(f[1][0])
 
# tensor(3.)
 
# Similarly for 3D Tensor
print(g[1,0,0])
print(g[1][0][0])
 
# tensor(5.)
  • 访问部分张量
# All elements
print(f[:])
 
# All elements from index 1 to 2 (inclusive)
print(c[1:3])
 
# All elements till index 4 (exclusive)
print(c[:4])
 
# First row
print(f[0,:])
 
# Second column
print(f[:,1])

3.3 张量元素类型

int_tensor = torch.tensor([[1,2,3],[4,5,6]])
print(int_tensor.dtype)
 
# torch.int64
 
# What if we changed any one element to floating point number?
int_tensor = torch.tensor([[1,2,3],[4.,5,6]])
print(int_tensor.dtype)
 
# torch.float32
 
print(int_tensor)
 
# tensor([[1., 2., 3.],
#        [4., 5., 6.]])
 
 
# This can be overridden as follows
int_tensor = torch.tensor([[1,2,3],[4.,5,6]], dtype=torch.int32)
print(int_tensor.dtype)
 
# torch.int32
print(int_tensor)
 
# tensor([[1, 2, 3],
#        [4, 5, 6]], dtype=torch.int32)

3.4 张量转换(NumPy Array)

# Import NumPy
import numpy as np
 
# Tensor to Array
f_numpy = f.numpy()
print(f_numpy)
 
# array([[1., 2.],
#       [3., 4.]], dtype=float32)
 
# Array to Tensor
h = np.array([[8,7,6,5],[4,3,2,1]])
h_tensor = torch.from_numpy(h)
print(h_tensor)
 
# tensor([[8, 7, 6, 5],
#        [4, 3, 2, 1]])

3.5 张量运算

# Create tensor
tensor1 = torch.tensor([[1,2,3],[4,5,6]])
tensor2 = torch.tensor([[-1,2,-3],[4,-5,6]])
 
# Addition
print(tensor1+tensor2)
# We can also use
print(torch.add(tensor1,tensor2))
 
# tensor([[ 0,  4,  0],
#        [ 8,  0, 12]])
 
# Subtraction
print(tensor1-tensor2)
# We can also use
print(torch.sub(tensor1,tensor2))
 
# tensor([[ 2,  0,  6],
#        [ 0, 10,  0]])
 
# Multiplication
# Tensor with Scalar
print(tensor1 * 2)
# tensor([[ 2,  4,  6],
#        [ 8, 10, 12]])
 
# Tensor with another tensor
# Elementwise Multiplication
print(tensor1 * tensor2)
# tensor([[ -1,   4,  -9],
#        [ 16, -25,  36]])
 
# Matrix multiplication
tensor3 = torch.tensor([[1,2],[3,4],[5,6]])
print(torch.mm(tensor1,tensor3))
# tensor([[22, 28],
#        [49, 64]])
 
# Division
# Tensor with scalar
print(tensor1/2)
# tensor([[0, 1, 1],
#        [2, 2, 3]])
 
# Tensor with another tensor
# Elementwise division
print(tensor1/tensor2)
# tensor([[-1,  1, -1],
#        [ 1, -1,  1]])

3.6 CPU v/s GPU 张量

PyTorch针对CPU和GPU有不同的Tensor实现。可以将每个张量转换为GPU,以执行大规模并行、快速的计算。所有对张量执行的操作都将使用PyTorch提供的专用于GPU的例程进行。

# Create a tensor for CPU
# This will occupy CPU RAM
tensor_cpu = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], device='cpu')
 
# Create a tensor for GPU
# This will occupy GPU RAM
tensor_gpu = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], device='cuda')

CPU v/s GPU张量转换

# Move GPU tensor to CPU
tensor_gpu_cpu = tensor_gpu.to(device='cpu')
 
# Move CPU tensor to GPU
tensor_cpu_gpu = tensor_cpu.to(device='cuda')

测试代码:001 PyTorch for Beginners