版权提示: 参考 PyTorch中文文档
1、 构建5*3的矩阵,不进行初始化
import torch
x = torch.empty(5, 3)
print(x)
输出
tensor([[1.0682e-11, 4.5757e-41, 1.6970e-36],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00]])
2、构建5*3的随机初始化的矩阵
x = torch.rand(5, 3)
print(x)
输出
tensor([[0.8665, 0.2635, 0.6159],
[0.6626, 0.2808, 0.7473],
[0.1187, 0.0978, 0.3235],
[0.4837, 0.7860, 0.4497],
[0.0208, 0.6852, 0.1181]])
3、构造一个矩阵全为0, 而且数据类型是long
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
4、 构造一个张量,直接使用数据
x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])
5、 创建一个tensor,基于已存在的tensor
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype=torch.float)
print(x)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-0.0781, 1.2222, 0.4896],
[ 0.9935, -0.2359, 2.1339],
[-1.5652, -2.9325, -1.4721],
[-0.6683, 1.3144, 0.2216],
[-1.3604, -0.7052, 1.0775]])
6、 获取维度信息
torch.size 是一个元祖,所以支持左右的元祖操作
print(x.size())
print(x.size()[0])
torch.Size([5, 3])
5
7、 加法操作 + 和 add
y = torch.rand(5, 3)
print(x+y)
print(torch.add(x, y))
完全一样
tensor([[ 0.3728, 1.6224, 1.3346],
[ 1.4414, 0.3598, 2.5921],
[-1.4853, -1.9702, -1.0021],
[ 0.1606, 1.3632, 0.7417],
[-1.2048, 0.0495, 1.3192]])
tensor([[ 0.3728, 1.6224, 1.3346],
[ 1.4414, 0.3598, 2.5921],
[-1.4853, -1.9702, -1.0021],
[ 0.1606, 1.3632, 0.7417],
[-1.2048, 0.0495, 1.3192]])
8、 提供一个输出Tensor作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 0.3728, 1.6224, 1.3346],
[ 1.4414, 0.3598, 2.5921],
[-1.4853, -1.9702, -1.0021],
[ 0.1606, 1.3632, 0.7417],
[-1.2048, 0.0495, 1.3192]])
9、 加法 in-place y.add(x) 和 y.add_(x)
y.add(x) 只减了一次, y.add_(x) 则一直在减
for i in range(4):
y.add(x)
print(y)
for i in range(4):
y.add_(x)
print(y)
tensor([[0.4509, 0.4002, 0.8449],
[0.4478, 0.5957, 0.4582],
[0.0800, 0.9623, 0.4701],
[0.8289, 0.0489, 0.5201],
[0.1557, 0.7547, 0.2417]])
tensor([[0.4509, 0.4002, 0.8449],
[0.4478, 0.5957, 0.4582],
[0.0800, 0.9623, 0.4701],
[0.8289, 0.0489, 0.5201],
[0.1557, 0.7547, 0.2417]])
tensor([[0.4509, 0.4002, 0.8449],
[0.4478, 0.5957, 0.4582],
[0.0800, 0.9623, 0.4701],
[0.8289, 0.0489, 0.5201],
[0.1557, 0.7547, 0.2417]])
tensor([[0.4509, 0.4002, 0.8449],
[0.4478, 0.5957, 0.4582],
[0.0800, 0.9623, 0.4701],
[0.8289, 0.0489, 0.5201],
[0.1557, 0.7547, 0.2417]])
tensor([[ 0.3728, 1.6224, 1.3346],
[ 1.4414, 0.3598, 2.5921],
[-1.4853, -1.9702, -1.0021],
[ 0.1606, 1.3632, 0.7417],
[-1.2048, 0.0495, 1.3192]])
tensor([[ 0.2947, 2.8446, 1.8242],
[ 2.4349, 0.1239, 4.7260],
[-3.0505, -4.9027, -2.4742],
[-0.5076, 2.6776, 0.9633],
[-2.5652, -0.6556, 2.3966]])
tensor([[ 0.2166, 4.0667, 2.3138],
[ 3.4285, -0.1120, 6.8600],
[-4.6157, -7.8352, -3.9463],
[-1.1759, 3.9919, 1.1849],
[-3.9257, -1.3608, 3.4741]])
tensor([[ 0.1385, 5.2889, 2.8035],
[ 4.4220, -0.3479, 8.9939],
[ -6.1810, -10.7677, -5.4184],
[ -1.8442, 5.3063, 1.4066],
[ -5.2861, -2.0660, 4.5516]])
10、 使用numpy类似的索引操作
print(x[:, 1])
tensor([ 1.2222, -0.2359, -2.9325, 1.3144, -0.7052])
11、改变Tensor大小的形状
如果你想改变一个tensor的大小或者形状, 可以使用torch.view
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
12、如果一个tensor只有一个元素,获取元素值
可以用 item()来获取这个value.注意只能有一个元素,否则会报错
x = torch.randn(1)
print(x)
print(x.item())
x = torch.tensor([3.14])
print(x.item())
tensor([-0.6945])
-0.6944621801376343
3.140000104904175