PyTorch_基础

第三方库:

from __future__ import print_function   # 使print函数在python2.x版本中正常运行
import torch
import numpy as np

(1) print_function 使print函数在python2.x版本中正常运行。
(2) torch 包包含了多维张量的数据结构, 以及基于其上的多种数学操作. 此外, 它还提供了许多用于高效序列化 Tensor 和任意类型的实用工具包, 以及一起其它有用的实用工具包。
(3)numpy的数组类(numpy.array)提供很多数组计算的便捷方法。

1.张量
(1)未初始化矩阵,其在使用之前不包含明确的已知值。

x = torch.empty(5, 3)
print(x)

输出:

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

(2)随机初始化矩阵

x = torch.rand(5, 3)
print(x)

输出:

tensor([[0.6259, 0.0797, 0.8297],
        [0.6732, 0.7944, 0.2363],
        [0.6775, 0.2497, 0.3846],
        [0.8515, 0.5171, 0.6957],
        [0.7759, 0.6000, 0.1323]])

(3)构造一个填充零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)在现有张量的基础上创建张量

x = x.new_ones(5, 3, dtype=torch.double)      #构建一个新的x张量
print(x)

x = torch.randn_like(x, dtype=torch.float)    
# 构建一个与原x类似大小的随机张量,并改变数据类型
print(x) 

输出:

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.5955, -0.2528, -0.2648],
        [ 0.7689,  0.2396, -0.0121],
        [ 1.3478,  0.0460,  0.0255],
        [ 0.1266, -1.1526, -0.5546],
        [-0.2001, -0.0542, -0.6439]])

(6)得到张量的大小

print(x.size())

输出:

torch.Size([5, 3])

(7)加法

y = torch.rand(5, 3)
print(x + y)

print(torch.add(x, y))

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

# adds x to y
y.add_(x)
# 任何变异张量的操作都是用_
# 例如:x.copy_(y), x.t_(),将会改变x
print(y)

输出:

tensor([[ 1.1550,  0.5950, -0.0519],
        [ 1.3954,  0.9232,  0.8904],
        [ 1.7020,  0.8187,  0.0265],
        [ 0.3831, -0.6057, -0.2829],
        [ 0.5647,  0.5976,  0.1128]])
tensor([[ 1.1550,  0.5950, -0.0519],
        [ 1.3954,  0.9232,  0.8904],
        [ 1.7020,  0.8187,  0.0265],
        [ 0.3831, -0.6057, -0.2829],
        [ 0.5647,  0.5976,  0.1128]])
tensor([[ 1.1550,  0.5950, -0.0519],
        [ 1.3954,  0.9232,  0.8904],
        [ 1.7020,  0.8187,  0.0265],
        [ 0.3831, -0.6057, -0.2829],
        [ 0.5647,  0.5976,  0.1128]])
tensor([[ 1.1550,  0.5950, -0.0519],
        [ 1.3954,  0.9232,  0.8904],
        [ 1.7020,  0.8187,  0.0265],
        [ 0.3831, -0.6057, -0.2829],
        [ 0.5647,  0.5976,  0.1128]])

(8)索引

print(x[:, 1])

输出:

# 输出为x的第二列
tensor([-0.2528,  0.2396,  0.0460, -1.1526, -0.0542])

(9)调整大小;使用torch.view调整张量的大小/形状

x = torch.randn(4, 4)
y = x.view(16)  # y是x的1维情况
z = x.view(2, 8)  # z是x的2行8列情况
print(x)
print(y)
print(z)
print(x.size(), y.size(), z.size())

输出:

tensor([[-0.4242,  0.5593, -0.4173, -0.0495],
        [ 0.1649, -0.3614,  1.6300,  0.3331],
        [-0.3961,  0.1895, -0.2653, -0.1173],
        [ 0.4377,  0.4657,  0.3130,  0.4540]])
tensor([-0.4242,  0.5593, -0.4173, -0.0495,  0.1649, -0.3614,  1.6300,  0.3331,
        -0.3961,  0.1895, -0.2653, -0.1173,  0.4377,  0.4657,  0.3130,  0.4540])
tensor([[-0.4242,  0.5593, -0.4173, -0.0495,  0.1649, -0.3614,  1.6300,  0.3331],
        [-0.3961,  0.1895, -0.2653, -0.1173,  0.4377,  0.4657,  0.3130,  0.4540]])
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

(10)Python数字获取;当只有一个元素张量,可以使用.item()获取

x = torch.randn(1)
print(x)
print(x.item())

输出:

tensor([-0.8748])
-0.8748161792755127

2.张量与numpy
(1)张量转换为数组

a = torch.ones(5)
print(a)
b = a.numpy()   # 张量转换为数组
print(b)

a.add_(1)
print(a)
print(b)    # a变b也跟着变

输出:

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

(2)数组转换为张量

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)  # 数组转换为张量 
np.add(a, 1, out=a)
print(a)
print(b)

输出:

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

3.CUDA张量
目的是使张量计算在GPU上运行,对于重复性的矩阵计算工作效率有明显的提升。采用.to方法可以将张量移动到任何设备上。

# 只有CUDA可用时,我们才能运行此单元
# 我们将使用“torch.device”对象将张量移入和移出GPU
x = torch.rand(3, 5)
if torch.cuda.is_available():
    device = torch.device("cuda")          # CUDA设备对象
    y = torch.ones_like(x, device=device)  # 直接在gpu上创建张量
    x = x.to(device)                       # 或者只使用字符串“to”(“cuda”)。``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to``也可以一起更改数据类型!

输出:

[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值