文章目录

  • 1. 张量的初始化
  • (1)直接生成张量
  • (2)通过Numpy数组来生成张量
  • (3)通过已有的张量来生成新的张量
  • (4)通过指定数据维度来生成张量
  • 2. 张量属性
  • 3. 张量运算
  • (1)将运算移植到GPU中
  • (2)张量的索引和切片
  • (3)张量的拼接
  • (4)张量的乘积和矩阵乘法
  • (5)自动赋值运算
  • 4. Tensor与Numpy的转化


张量是一种特殊的

数据结构,在pytorch中,

神经网络的输入、输出和网络的参数等数据,都是通过张量的形式进行描述的。张量和Numpy中的ndarrays很像,只不过张量可以运行在GPU或者其它专用硬件上,从而达到加速的效果。

1. 张量的初始化

下面介绍4种初始化方法。

(1)直接生成张量

由原始数据直接生成张量,张量类型由原始数据类型决定。

import torch

data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
print(x_data)

输出:

tensor([[1, 2],
        [3, 4]])

(2)通过Numpy数组来生成张量

Numpy类型和张量类型可以互相转化

import torch
import numpy as np

data = [[1, 2], [3, 4]]
np_array = np.array(data) #转化为numpy类型
x_np = torch.from_numpy(np_array) # 生成张量
print(x_np)

输出:

tensor([[1, 2],
        [3, 4]])

(3)通过已有的张量来生成新的张量

新的张量将继承已有张量的数据属性(结构、类型), 也可以重新指定新的数据类型。下列x_ones为继承类型,x_rand为重写类型。

import torch

data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
x_ones = torch.ones_like(x_data)   # 保继承x_data 的属性
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float)   # 重写 x_data 的数据类型,int -> float
print(f"Random Tensor: \n {x_rand} \n")

输出:

Ones Tensor: 
 tensor([[1, 1],
        [1, 1]]) 

Random Tensor: 
 tensor([[0.8947, 0.7094],
        [0.8378, 0.1072]])

(4)通过指定数据维度来生成张量

可以通过元组类型描述张量的维度,并使用下列三种方式生成张量。

import torch

shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")

输出:

Random Tensor: 
 tensor([[0.1817, 0.6367, 0.2425],
        [0.6479, 0.2495, 0.6829]]) 

Ones Tensor: 
 tensor([[1., 1., 1.],
        [1., 1., 1.]]) 

Zeros Tensor: 
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

2. 张量属性

张量属性有维数、数据类型以及它们所存储的设备(CPU或GPU)。下面举例获取相应的属性。

import torch

tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")               # 维数
print(f"Datatype of tensor: {tensor.dtype}")            # 数据类型
print(f"Device tensor is stored on: {tensor.device}")   # 存储设备

输出:

Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu

3. 张量运算

张量的运算操作有很多种,例如转置、索引、切片、数学运算、线性代数、随机采样等。如果想要查看完成的运算操作,可以查看官网链接

(1)将运算移植到GPU中

所有的运算操作都可以在GPU中运行,例如:

import torch

# 判断当前环境GPU是否可用, 然后将tensor导入GPU内运行
tensor = torch.rand(3, 4)
if torch.cuda.is_available():
  tensor = tensor.to('cuda')

print(f"Device tensor is stored on: {tensor.device}")

输出:

Device tensor is stored on: cuda:0

可以看到tensor张量已经移到GPU中。

(2)张量的索引和切片

import torch

tensor = torch.ones(4, 4)
tensor[:,1] = 0            # 将第1列数据全部赋值为0
print(tensor)

输出:

tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

(3)张量的拼接

可以通过torch.cat或者torch.stack将一组张量按照指定的维度进行拼接。

import torch

tensor = torch.ones(4, 4)
tensor[:,1] = 0            # 将第1列(从0开始)的数据全部赋值为0
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)

输出:

tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])

(4)张量的乘积和矩阵乘法

  • 张量矩阵元素逐个相乘
import torch

tensor = torch.ones(4, 4)
tensor[:,1] = 0            # 将第1列(从0开始)的数据全部赋值为0

# 逐个元素相乘结果
print(f"tensor.mul(tensor): \n {tensor.mul(tensor)} \n")
# 等价写法:
print(f"tensor * tensor: \n {tensor * tensor}")

输出:

tensor.mul(tensor): 
 tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]]) 

tensor * tensor: 
 tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])
  • 张量的矩阵乘法
import torch

tensor = torch.ones(4, 4)
tensor[:,1] = 0            # 将第1列(从0开始)的数据全部赋值为0

print(f"tensor.matmul(tensor.T): \n {tensor.matmul(tensor.T)} \n")
# 等价写法:
print(f"tensor @ tensor.T: \n {tensor @ tensor.T}")

输出:

tensor.matmul(tensor.T): 
 tensor([[3., 3., 3., 3.],
        [3., 3., 3., 3.],
        [3., 3., 3., 3.],
        [3., 3., 3., 3.]]) 

tensor @ tensor.T: 
 tensor([[3., 3., 3., 3.],
        [3., 3., 3., 3.],
        [3., 3., 3., 3.],
        [3., 3., 3., 3.]])

(5)自动赋值运算

自动赋值运算通常在方法后有 _ 作为后缀, 例如: x.copy_(y), x.t_()操作会改变 x 的取值。

自动赋值运算虽然可以节省内存, 但在求导时会因为丢失了中间过程而导致一些问题, 所以我们并不鼓励使用它

import torch

tensor = torch.ones(4, 4)
tensor[:,1] = 0            # 将第1列(从0开始)的数据全部赋值为0

print(tensor, "\n")
tensor.add_(5)
print(tensor)

输出:

tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]]) 

tensor([[6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.]])

4. Tensor与Numpy的转化

张量和Numpy array数组在CPU上可以共用一块内存区域, 改变其中一个另一个也会随之改变。

  • 将Tensor转换为Numpy array数组
import torch

t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")

输出:

t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]
  • 由Numpy array数组转为张量
import torch
import numpy as np

n = np.ones(5)
t = torch.from_numpy(n)

# 修改其中一个值,另一个也随之改变
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

输出:

t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]