pytorch入门——tensor预备知识看这篇就够了

查看pytorch能否识别我的显卡

import torch
print(torch.cuda.is_available())
print(torch.rand(2,2))    #随机生成秩为2的tensor

True

tensor([[0.6070, 0.0479],

[0.4637, 0.4207]])

tensor(张量):数字容器、多维数组

#列表创建
x=torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
x

tensor([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

#改变元素
x[0][0]=100
x

tensor([[100, 2, 3],

[ 4, 5, 6],

[ 7, 8, 9]])

#特定类型tensor
print(torch.zeros(2,2))
print(torch.ones(1,2)+torch.ones(1,2))
tensor([[0., 0.],    
        [0., 0.]])
tensor([[2., 2.]])
#秩为0的tensor,用item取值
torch.rand(1).item()

0.2878343462944031

#tensor可以存在于CPU和GPU,使用to()在设备间复制
cpu_tensor = torch.rand(2)
print(cpu_tensor.device)
gpu_tensor = cpu_tensor.to('cuda')
print(gpu_tensor.device)

cpu

cuda:0

张量操作

#max
T=torch.rand(2,2)
T
tensor([[0.5627, 0.4383],     
        [0.9184, 0.7431]])
print(T.max())
print(T.max().item())

tensor(0.9184)

0.9183632135391235

#使用to()改变tensor的类型
T1=torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
print(T1.type())
T2=T1.to(dtype=torch.float32)
print(T2.type())

torch.LongTensor

torch.FloatTensor

#in-place函数(在pytorch中是指改变一个tensor的值的时候,不经过复制操作,而是直接在原来的内存上改变它的值。)
T=torch.rand(2)
print(T)
print(T.log2())
print(T.log2_())   
T
# x 和 y 不可以广播
x=torch.empty(1,3,1)
print(x)
y=torch.ones(1,3,1)
print((x.add_(y)).size())
x
tensor([0.3138, 0.7331])
tensor([-1.6720, -0.4479])
tensor([-1.6720, -0.4479])
tensor([[[-5.0002e-01],
   [ 2.0420e+00],
         [ 9.3940e+21]]])
torch.Size([1, 3, 1])


tensor([[[4.9998e-01],
         [3.0420e+00],
         [9.3940e+21]]])

张量变形无脑用reshape()

#张量变形
flat_tensor=torch.rand(784)
viewed_tensor=flat_tensor.view(1,28,28)    #view作变形
print(viewed_tensor.shape)

reshaped_tensor=flat_tensor.reshape(1,28,28)  #reshape做变形
print(reshaped_tensor.shape)

torch.Size([1, 28, 28])

torch.Size([1, 28, 28])

Tensor.unfold(dimension, size, step)的用法

# Tensor.unfold(dimension, size, step)
# dimension (int) – dimension in which unfolding happens
# size (int) – the size of each slice that is unfolded
# step (int) – the step between each slice
x = torch.Tensor([[  1,  2,  3,  4,  5],
                  [  6,  7,  8,  9, 10],
                  [ 11, 12, 13, 14, 15],
                  [ 16, 17, 18, 19, 20]])
x = x.unfold(0, 3, 1)
print(x.shape)
x
torch.Size([2, 5, 3])
tensor([[[ 1.,  6., 11.],
         [ 2.,  7., 12.],
         [ 3.,  8., 13.],
         [ 4.,  9., 14.],
         [ 5., 10., 15.]],

        [[ 6., 11., 16.],
         [ 7., 12., 17.],
         [ 8., 13., 18.],
         [ 9., 14., 19.],
         [10., 15., 20.]]])
#pytorch官网例子
x=torch.arange(1.,8)
print(x.shape)
x

torch.Size([7])

tensor([1., 2., 3., 4., 5., 6., 7.])

x.unfold(0,2,1)

tensor([[1., 2.],

        [2., 3.],
        [3., 4.],
        [4., 5.],
        [5., 6.],
        [6., 7.]])
x.unfold(0,2,2)

tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])
# 重排维度
hwc_tensor=torch.rand(640,480,3)
chw_tensor=hwc_tensor.permute(2,0,1)
chw_tensor.shape

torch.Size([3, 640, 480])

张量的广播与运算

·每个tensor至少有一个维度;

·遍历tensor所有维度时,从末尾随开始遍历,两个tensor存在下列情况:

·tensor维度相等。

·tensor维度不等且其中一个维度为1。

·tensor维度不等且其中一个维度不存在。

实例1

#张量x.shape = (3, 4),张量y.shape = (1, 4),其计算过程如下,将y复制为shape=(3, 4),然后对应位置相加计算。
x=torch.zeros(3,4)
y=torch.tensor([[1,2,3,4]])
x+y
  tensor([[1., 2., 3., 4.],
          [1., 2., 3., 4.],
          [1., 2., 3., 4.]])

实例2

t1=torch.zeros(3,4,5)
t1
tensor([[[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]]])
t2=torch.ones(1,1,5)
t2

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

t1+t2
tensor([[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])

实例三

t1=torch.zeros(1,4,5)
t1
tensor([[[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]]])
t2=torch.ones(3,1,1)
t2
tensor([[[1.]],
        [[1.]],
        [[1.]]])
t1+t2
tensor([[[1., 1., 1., 1., 1.],  
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值