我是靠谱客的博主 耍酷大山,最近开发中收集的这篇文章主要介绍torch.flatten(),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 将张量拉成一维的向量

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

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]
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]
]
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.flatten()所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部