欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytorch笔记——pytorch基础

程序员文章站 2022-06-26 12:02:50
...
import torch
import torchvision
print(torch)

随机生成


x=torch.rand(5,3)
print(x)
'''
tensor([[0.8781, 0.6810, 0.4982],
        [0.7308, 0.6844, 0.8016],
        [0.9965, 0.7614, 0.0788],
        [0.6502, 0.6808, 0.0835],
        [0.0102, 0.9330, 0.2545]])
'''

构造一个矩阵,不初始化

x=torch.empty(5,3)
print(x)
'''
tensor([[1.0286e-38, 1.0194e-38, 9.6429e-39],
        [9.2755e-39, 9.1837e-39, 9.3674e-39],
        [1.0745e-38, 1.0653e-38, 9.5510e-39],
        [1.0561e-38, 1.0194e-38, 1.1112e-38],
        [1.0561e-38, 9.9184e-39, 1.0653e-38]])
'''

生成0

x=torch.zeros(5,3,)
print(x)
'''
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
'''

构造张量

x=torch.tensor([5,3],dtype=torch.float)
print(x)
x=torch.tensor([5.56,3],dtype=torch.int32)
print(x)
x=torch.tensor([5.56,3],dtype=torch.float)
print(x)
'''
tensor([5., 3.])
tensor([5, 3], dtype=torch.int32)
tensor([5.5600, 3.0000])
'''

创建一个tensor,基于已经存在的tensor


x=x.new_ones(5,3)
x=torch.randn_like(x,dtype=torch.float)
print(x)
print(x)
'''
tensor([[-1.3639,  0.8326, -0.4899],
        [ 1.6127, -2.0743,  1.5063],
        [-1.1452, -0.9633, -0.0357],
        [-2.1144, -1.9547,  1.2708],
        [-1.2065,  0.7668,  0.1674]])
tensor([[-1.3639,  0.8326, -0.4899],
        [ 1.6127, -2.0743,  1.5063],
        [-1.1452, -0.9633, -0.0357],
        [-2.1144, -1.9547,  1.2708],
        [-1.2065,  0.7668,  0.1674]])
'''
print(x.size())
'''
torch.Size([1])
'''

加法


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)

y.add_(x)
print(y)
'''
tensor([[-0.4606,  1.4884,  0.1468],
        [ 2.0469, -1.7316,  2.3033],
        [-0.3407, -0.9139,  0.9012],
        [-1.8327, -1.5334,  2.1550],
        [-0.5702,  1.7230,  1.0674]])
tensor([[-0.4606,  1.4884,  0.1468],
        [ 2.0469, -1.7316,  2.3033],
        [-0.3407, -0.9139,  0.9012],
        [-1.8327, -1.5334,  2.1550],
        [-0.5702,  1.7230,  1.0674]])
tensor([[-0.4606,  1.4884,  0.1468],
        [ 2.0469, -1.7316,  2.3033],
        [-0.3407, -0.9139,  0.9012],
        [-1.8327, -1.5334,  2.1550],
        [-0.5702,  1.7230,  1.0674]])
tensor([[-0.4606,  1.4884,  0.1468],
        [ 2.0469, -1.7316,  2.3033],
        [-0.3407, -0.9139,  0.9012],
        [-1.8327, -1.5334,  2.1550],
        [-0.5702,  1.7230,  1.0674]])
'''

类似列表的索引

print(x[:,1])
'''
tensor([ 0.8326, -2.0743, -0.9633, -1.9547,  0.7668])
'''

改变大小

x=torch.randn(4,4)
y=x.view(16)
z=x.view(-1,8)#-1表示根据另一列推断
print(x.size(),y.size(),z.size())
'''
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
'''

只有1个元素的时候可以用.item()获得值

x=torch.rand(1)
print(x)
print(x.item())
y=torch.rand(5,5)
print(y)
print(y.item())
'''
tensor([0.6056])
0.6056373119354248
tensor([[0.5475, 0.7503, 0.6943, 0.1225, 0.1180],
        [0.1781, 0.6898, 0.0210, 0.5957, 0.1206],
        [0.7138, 0.4520, 0.5671, 0.5965, 0.1314],
        [0.5944, 0.8185, 0.4200, 0.9304, 0.8873],
        [0.0336, 0.6716, 0.7224, 0.3542, 0.9614]])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-04dec2814a89> in <module>
      4 y=torch.rand(5,5)
      5 print(y)
----> 6 print(y.item())

ValueError: only one element tensors can be converted to Python scalars



'''

自动广播

x=torch.arange(1,3).view(1,2)
print(x)
y=torch.arange(1,4).view(3,1)
print(y)
print(x+y)
'''tensor([[1, 2]])
tensor([[1],
        [2],
        [3]])
tensor([[2, 3],
        [3, 4],
        [4, 5]])
'''

相关标签: pytorch文档笔记