我是靠谱客的博主 耍酷大山,这篇文章主要介绍torch.flatten(),现在分享给大家,希望可以做个参考。

 将张量拉成一维的向量

x=torch.randn(2,3,2)
x2=torch.flatten(x,0)
x3=torch.flatten(x,1)
x4=torch.flatten(x,2)

复制代码
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
import torch x=torch.randn(2,3,2) print(x) #生成: [ [ [-0.5829, 0.8214], [ 0.6218, 0.3298], [ 0.0222, -0.8473] ], [ [ 0.1044, -1.8784], [ 1.2323, 2.6551], [ 0.0382, 0.6649] ] ] x2=torch.flatten(x,0)#等价于x2=torch.flatten(x) print(x2) [-0.5829, 0.8214, 0.6218, 0.3298, 0.0222, -0.8473, 0.1044, -1.8784, 1.2323, 2.6551,0.0382, 0.6649]
复制代码
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
import torch x=torch.randn(2,3,2) print(x) #生成: [ [ [-0.5829, 0.8214], [ 0.6218, 0.3298], [ 0.0222, -0.8473] ], [ [ 0.1044, -1.8784], [ 1.2323, 2.6551], [ 0.0382, 0.6649] ] ] x3=torch.flatten(x,1) print(x3) [ [-0.5829, 0.8214, 0.6218, 0.3298, 0.0222, -0.8473], [ 0.1044, -1.8784, 1.2323, 2.6551, 0.0382, 0.6649] ]
复制代码
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
import torch x=torch.randn(2,3,2) print(x) #生成: [ [ [-0.5829, 0.8214], [ 0.6218, 0.3298], [ 0.0222, -0.8473] ], [ [ 0.1044, -1.8784], [ 1.2323, 2.6551], [ 0.0382, 0.6649] ] ] x4=torch.flatten(x,2) print(x4) [ [ [-0.5829, 0.8214], [ 0.6218, 0.3298], [ 0.0222, -0.8473] ], [ [ 0.1044, -1.8784], [ 1.2323, 2.6551], [ 0.0382, 0.6649] ] ]

最后

以上就是耍酷大山最近收集整理的关于torch.flatten()的全部内容,更多相关torch内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(92)

评论列表共有 0 条评论

立即
投稿
返回
顶部