pytorch笔记

本文由用户“畜崽”分享发布 更新时间:2022-03-31 21:57:44 举报文档

以下为《pytorch笔记》的无排版文字预览,完整格式请下载

下载前请仔细阅读文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

Torch数据类型

Torch.FloatTensor>>>>>>Float Torch.IntTensor>>>>>>>>>>Int

obj.type(): 查看对象的类型

元素创建

numpy创建:torch.from_numpy()

随机方式:torch.rand(2,3), torch.rand_like(a), torch.randint(1,10,(2,3)), torch.randperm(10), torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1)),

特殊方式:torch.arange(n), torch.eye(), torch.full((2,3),7), torch.linspace(0,10,steps=4)

torch.ones(3,3), torch.zeros(3,3) torch.eye(3,2)

利用已知大小的创建:torch.new_ones(5,3) torch.randn_like(x,detype=torch.float)

索引

Tensor_obj.index_select( dim, index)

功能:在指定的维度dim上选取数据,不如选取某些行,列

第一个参数dim是要查找的维度,因为通常情况下我们使用的都是二维张量,所以可以简单的记忆:?0代表行,1代表列

第二个参数index是你要索引的序列,它是一个tensor对象(不能是列表或其他对象)

参考资料:

https://zhuanlan.zhihu.com/p/***9

/

维度变换

维数变换:obj.reshape(tuple)或torch.reshape(shape)

维度更换:obj.permute(dims)

x = torch.randn(2, 3, 5) x.permute(2, 0, 1).size() torch.Size([5, 2, 3])

维数增加或删除:obj.squeeze()或obj.unsqueeze()在既定位置插入维度 1或删除指定维度

参考:https://zhuanlan.zhihu.com/p/***

更换维数:obj.expand(3, 4) obj.expand(-1, 4)

https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html

广播运算

/

参考资料:https://blog.csdn.net/qq_***/article/details/***9

乘法运算

对应元素相乘(torch.mul或*)

向量点乘 (torch.matmul)

参考资料:

https://blog.csdn.net/McEason/article/details/***8?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

拼接与拆分

1. 指定维度合并:torch.cat((a,b),dim):a与b除了在指定dim上大小不一致,其余必须一致

2. 指定维度合并:torch.stack((a,b),dim):在指定dim上增加一维,然后进行类似cat操作

3. 指定维度按照既定长度拆分:obj.split(le 内容过长,仅展示头部和尾部部分文字预览,全文请查看图片预览。 nearity='relu')

elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):

nn.init.constant_(m.weight, 1)

nn.init.constant_(m.bias, 0)

def forward(self, x):

x = self.layer1(x)

x = self.layer2(x)

x = self.layer3(x)

return x

[文章尾部最后300字内容到此结束,中间部分内容请查看底下的图片预览]

以上为《pytorch笔记》的无排版文字预览,完整格式请下载

下载前请仔细阅读上面文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

图片预览