Tensors介绍

Tensors可以简单地看成数组或矩阵(类似Numpy中的ndarrays),在Pytorch中可用tensors来表示一个模型的输入、输出和参数.
可用tensor.shape tensor.dtype tensor.device来查看tensor的维度、数据类型和设备.

1
2
3
%matplotlib inline
import torch
import numpy as np

Tensor的初始化 - 1

Pytorch中的tensor结构有多种初始化的方法, 可以直接由List或者Numpy数组初始化, 但要注意由numpy arraytensor是一个原地操作, 两者共享存储地址, 只改变解释方式,改变tensor的同时会改变numpy array, 例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# From Data
data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)

# From Numpy array
np_array = np.array(data)
x_np = torch.from_numpy(np_array)

print(f"Numpy np_array value: \n {np_array} \n")
print(f"Tensor x_np value: \n {x_np} \n")

np.multiply(np_array, 2, out=np_array)

print(f"Numpy np_array after * 2 operation: \n {np_array} \n")
print(f"Tensor x_np value after modifying numpy array: \n {x_np} \n")

运行结果为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Numpy np_array value: 
[[1 2]
[3 4]]

Tensor x_np value:
tensor([[1, 2],
[3, 4]])

Numpy np_array after * 2 operation:
[[2 4]
[6 8]]

Tensor x_np value after modifying numpy array:
tensor([[2, 4],
[6, 8]])

python中的tensor初始化 - 2

可以利用元组shape参数来初始化tensor,例如:

1
2
3
4
5
6
7
8
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}")

运行结果为:

1
2
3
4
5
6
7
8
9
10
11
Random Tensor: 
tensor([[0.4424, 0.4927, 0.5646],
[0.7742, 0.0868, 0.3927]])

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

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

Tensor操作

从CPU转移到GPU

Tensor默认创建在CPU上,如果需要在GPU上运行,可以使用torch.to来移动tensor,也可以预先指定设备为GPU。例如:

1
2
3
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')

剪切与赋值操作

和Numpy类似,不再赘述:

1
2
3
4
5
6
tensor = torch.ones(4, 4)
print('First row: ',tensor[0])
print('First column: ', tensor[:, 0])
print('Last column:', tensor[..., -1])
tensor[:,1] = 0
print(tensor)

连接操作

torch.cat来连接tensor,默认为行方向连接,用dim=1参数指定列方向,例如:

1
2
t1 = torch.cat([tensor, tensor, tensor],dim=1)
print(t1)

算术运算

@tensor.matmultorch.matmul均可进行矩阵乘法,用*tensor.multorch.mul可实现矩阵对应位置相乘

1
2
3
4
5
6
7
8
9
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)

z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)

y123的结果是相同的,z123的结果也是相同的

tensor转数值

.item()可将tensor转为python数值,例如:

1
2
3
agg = tensor.sum()
agg_item = agg.item()
print(agg, type(agg), agg_item, type(agg_item))

结果为:tensor(12.) <class 'torch.Tensor'> 12.0 <class 'float'>

Notice - tensor的原地操作

除了numpy arraytensor之外,还有一些原地操作,例如:x.copy_(y), x.t_(), x.add_(), x.numpy()都会改变x的值。
为保证程序完美运行,在内存开销足够的情况下,应尽量避免原地操作