复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45import torch if __name__ == "__main__": # 构造一个5x3矩阵,不初始化 x = torch.empty(5, 3) print(x) # 构造一个随机初始化的矩阵: x = torch.rand(5, 3) print(x) # 构造一个矩阵全为 0,而且数据类型是 long. x = torch.zeros(5, 3, dtype=torch.long) print(x) # 直接使用数据构造一个张量 x = torch.tensor([5.5, 3]) print(x) # 创建一个 tensor 基于已经存在的 tensor x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes print(x) x = torch.randn_like(x, dtype=torch.float) # override dtype! print(x) # result has the same size # 获取它的维度信息 print(x.size()) # torch.Size 是一个元组,所以它支持左右的元组操作 # 加法:方式一 y = torch.rand(5, 3) print(x + y) # 加法: 方式2 print(torch.add(x, y)) # 加法: 提供一个输出 tensor 作为参数 result = torch.empty(5, 3) torch.add(x, y, out=result) print(result) # adds x to y y.add_(x) print(y) # 改变大小:如果你想改变一个 tensor 的大小或者形状,你可以使用 torch.view: x = torch.randn(4, 4) y = x.view(16) z = x.view(-1, 8) # the size -1 is inferred from other dimensions print(x.size(), y.size(), z.size())
最后
以上就是舒心大地最近收集整理的关于PyTorch学习笔记(一)的全部内容,更多相关PyTorch学习笔记(一)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复