PyTorch 基本使用(1)
Tensors (张量)
Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算。
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
tensor([[1.0286e-38, 9.0919e-39, 8.9082e-39],
[9.2755e-39, 8.4490e-39, 1.0194e-38],
[9.0919e-39, 8.4490e-39, 1.0745e-38],
[1.0653e-38, 1.0286e-38, 1.0194e-38],
[9.2755e-39, 1.0561e-38, 1.0102e-38]])
tensor([[0.1387, 0.4353, 0.6198],
[0.9733, 0.6104, 0.7069],
[0.7583, 0.9802, 0.0425],
[0.6065, 0.1982, 0.7491],
[0.5445, 0.8969, 0.0568]])
构造一个随机初始化的矩阵:
x = torch.rand(5, 3)
print(x)
tensor([[0.7264, 0.5414, 0.0320],
[0.9985, 0.9970, 0.5261],
[0.8209, 0.6232, 0.2760],
[0.8616, 0.4027, 0.1228],
[0.9084, 0.3834, 0.5738]])
构造一个矩阵全为 0,而且数据类型是 long
x = torch.zeros(5, 3, dtype=torch.long)a
print(x)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
构造一个张量,直接使用数据
x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])
创建一个 tensor 基于已经存在的 tensor
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x_ = torch.randn_like(x, dtype=torch.float)
print(x)
print(x.size())
print(x_.size())
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
torch.Size([5, 3])
torch.Size([5, 3])
接下来的例子中,我们将会看到加法操作
加法: 方式1
y = torch.rand(5, 3)
print(x)
print(y)
print(x + y)
print((x+y).size())
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[0.2351, 0.0342, 0.3862],
[0.3746, 0.8989, 0.4538],
[0.4272, 0.6829, 0.9977],
[0.1624, 0.4268, 0.7985],
[0.1371, 0.4286, 0.6957]])
tensor([[1.2351, 1.0342, 1.3862],
[1.3746, 1.8989, 1.4538],
[1.4272, 1.6829, 1.9977],
[1.1624, 1.4268, 1.7985],
[1.1371, 1.4286, 1.6957]], dtype=torch.float64)
torch.Size([5, 3])
加法: 方式2
c=torch.add(x, y)
print(c)
print(c.size())
tensor([[1.2351, 1.0342, 1.3862],
[1.3746, 1.8989, 1.4538],
[1.4272, 1.6829, 1.9977],
[1.1624, 1.4268, 1.7985],
[1.1371, 1.4286, 1.6957]], dtype=torch.float64)
torch.Size([5, 3])
加法: 提供一个输出 tensor 作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[1.2351, 1.0342, 1.3862],
[1.3746, 1.8989, 1.4538],
[1.4272, 1.6829, 1.9977],
[1.1624, 1.4268, 1.7985],
[1.1371, 1.4286, 1.6957]])
加法: in-place
y.add_(x)
print(y)
tensor([[1.2351, 1.0342, 1.3862],
[1.3746, 1.8989, 1.4538],
[1.4272, 1.6829, 1.9977],
[1.1624, 1.4268, 1.7985],
[1.1371, 1.4286, 1.6957]])
注意
任何使张量会发生变化的操作都有一个前缀 ‘’。例如:x.copy(y), x.t_(), 将会改变 x
使用标准的 NumPy 类似的索引操作
print(y)
print(y[:, 1])
print(y[1,:])
tensor([[1.2351, 1.0342, 1.3862],
[1.3746, 1.8989, 1.4538],
[1.4272, 1.6829, 1.9977],
[1.1624, 1.4268, 1.7985],
[1.1371, 1.4286, 1.6957]])
tensor([1.0342, 1.8989, 1.6829, 1.4268, 1.4286])
tensor([1.3746, 1.8989, 1.4538])
改变大小
x = torch.randn(4, 4)
print('x is',x)
y = x.view([16])
print('y is',y)
z = x.view(-1, 8)
print('z is',z)
print(x.size(), y.size(), z.size())
tensor([[ 2.3366, -0.2340, 1.8353, -0.8254],
[ 0.0038, -0.5315, 0.7534, -0.2029],
[-0.8902, -0.4629, 0.4296, 0.1333],
[-1.4612, 1.0908, -2.0607, -1.5879]])
tensor([ 2.3366, -0.2340, 1.8353, -0.8254, 0.0038, -0.5315, 0.7534, -0.2029,
-0.8902, -0.4629, 0.4296, 0.1333, -1.4612, 1.0908, -2.0607, -1.5879])
z is tensor([[ 2.3366, -0.2340, 1.8353, -0.8254, 0.0038, -0.5315, 0.7534, -0.2029],
[-0.8902, -0.4629, 0.4296, 0.1333, -1.4612, 1.0908, -2.0607, -1.5879]])
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一个元素 tensor ,使用 .item() 来获得这个 value
x = torch.randn(1)
print(x)
print(x.item())
tensor([0.4578])
0.4578220844268799
|