记录学习pytorch常用的API(1)

记录学习pytorch常用的API(1)


pytorch

不同于python,pytorch只有一个数据类型,就是张量

维度

(1)维度为0时,维度为0,一般在模型里是loss函数

import torch
a = torch.tensor(1)
print(a)
#查看数据类型
print(a.type())
print(type(a))
#查看设备
print(a.device)
#查看形状
print(a.shape)
print(a.size())
#loss一般是一个标量,维度一般为0
#判断括号里的是不是同一个东西
print(isinstance(a,torch.IntTensor))
print(isinstance(a,torch.LongTensor))
#查看维度
print(a.dim())
print(len(a.shape))

输出:
tensor(1)
torch.LongTensor
<class ‘torch.Tensor’>
cpu
torch.Size([])
torch.Size([])
False
True
0
0
(2)维度为1时,模型里面一般指线性层的输入

b=torch.tensor([1,2,3,4])#有一个中括号就是一个维度,当前维度下有四个元素罢了
print(b)
print(b.dim())
print(b.type())
print(b.device)
print(b.shape)

输出:
tensor([1, 2, 3, 4])
1
torch.LongTensor
cpu
torch.Size([4])
(3)维度为2时,在模型里面一般是batch(每次给模型训练送多少张图片)

c = torch.tensor([[1,2,3,4],[5,6,7,8]])
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.shape)

输出:
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
2
torch.LongTensor
cpu
torch.Size([2, 4])
(4)维度为3时,比如说图片[3,224,224]维度为3,三个通道,224是高度宽度

c = torch.tensor([[[1,2,3,4],[5,6,7,8]]])
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.shape)
print(torch.numel(c))#全部元素的数量

输出:
tensor([[[1, 2, 3, 4],
[5, 6, 7, 8]]])
3
torch.LongTensor
cpu
torch.Size([1, 2, 4])
8
(5)维度为4时,四个维度,对于图片来说是[batch,3,224,224]几乎数据集都是四个维度的

创建张量、初始化

#初始化
a = torch.tensor(5)
b = torch.FloatTensor(4)#转化为浮点型的tensor
print(a.type())
print(b.type())

输出:
torch.LongTensor
torch.FloatTensor

c = torch.ones(3,3)#三行三列的tensor,全1
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.size())

输出:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
2
torch.FloatTensor
cpu
torch.Size([3, 3])

c=torch.zeros(4,4)#全0
print(c)
d = torch.eye(4,4)#对角矩阵
print(d)

输出:
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

a = torch.arange(0,10,2)#左开右闭,第三个参数为步长,默认为1
print(a)
print(a.dim())

输出:
tensor([0, 2, 4, 6, 8])
1

a = torch.linspace(0,10,5)#也是一个数列,5代表切分的数量
print(a)

输出:
tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])

不进行初始化

a = torch.empty((2,3),dtype=torch.int32,device='cpu')#随机值,没有初始化
print(a)
print(a.type())

输出:
tensor([[ 895562337, 761422946, 1697801779],
[1667314733, 1650535731, 825058099]], dtype=torch.int32)
torch.IntTensor

a = torch.Tensor(2,3)#大T是值形状
print(a)
b = torch.tensor([2,3])#小t是指值
print(b)

输出:
tensor([[1.0384e+21, 2.5231e-09, 2.1876e-04],
[6.5644e-07, 4.0519e-11, 7.1450e+31]])
tensor([2, 3])

a = torch.Tensor(2,3)#大T是值形状,默认浮点型
print(a)
print(a.type())
a = torch.IntTensor(2,3)#大T是值形状,创建整形
print(a)
print(a.type())
a = torch.FloatTensor(2,3)#大T是值形状,创建浮点型
print(a)
print(a.type())

输出:
tensor([[2.1069e-07, 5.2898e-08, 4.2314e+21],
[2.6729e+23, 3.4154e-06, 2.7001e-06]])
torch.FloatTensor
tensor([[ 895562337, 761422946, 1697801779],
[1667314733, 1650535731, 825058099]], dtype=torch.int32)
torch.IntTensor
tensor([[2.1069e-07, 5.2898e-08, 4.2314e+21],
[2.6729e+23, 3.4154e-06, 2.7001e-06]])
torch.FloatTensor

a = torch.randn(3,3)#默认正态分布
print(a)

输出:
a = torch.randn(3,3)#默认正态分布
print(a)
a = torch.randn(3,3)#默认正态分布
print(a)
tensor([[-0.0480, 1.6209, -0.0681],
[ 2.4931, 1.1824, 0.0130],
[-1.8851, 1.0678, 0.3411]])

a = torch.randperm(10)#随机打散
print(a)

输出:
tensor([3, 6, 5, 2, 4, 0, 7, 1, 8, 9])

小结:对于没有初始化的,传的参数都是形状,对于初始化的,传的参数就是具体的值了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值