PyTorch中文笔记(1)

PyTorch 基本使用(1)

Tensors (张量)

Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算。

from __future__ import print_function
import torch
# 构造一个5x3矩阵,不初始化
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) 
# new_* methods take in sizes
print(x)
x_ = torch.randn_like(x, dtype=torch.float) 
# override dtype!
print(x) 
# result has the same size
# 获取它的维度信息
print(x.size())
print(x_.size())
## torch.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)
# 这里的result相当于上面的c
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

# adds x to y
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])

改变大小

# 改变大小:如果你想改变一个 tensor 的大小或者形状,你可以使用 torch.view
x = torch.randn(4, 4)
print('x is',x)
y = x.view([16])    # y=x.view(16)返回1行16列的矩阵相当于把原矩阵拉平
print('y is',y)
z = x.view(-1, 8) # 要修改成具有m行8列的tensor,m值会自动计算,只需要输入参数-1即可
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值