CNN记录】pytorch中flatten函数

pytorch原型

python 复制代码
torch.flatten(input, start_dim=0, end_dim=- 1)

作用:将连续的维度范围展平维张量,一般写再某个nn后用于对输出处理,

参数:

start_dim:开始的维度

end_dim:终止的维度,-1为最后一个轴

默认值时展平为1维

例子

1、默认参数

python 复制代码
input = torch.randn(2, 3, 4, 5)
output = torch.flatten(input)
输出维:torch.Size([120])

2、设置参数

python 复制代码
input = torch.randn(2, 3, 4, 5)

output = torch.flatten(input,1)
输出shape为:torch.Size([2, 60])

output = torch.flatten(input,1,2)
输出shape为:torch.Size([2, 12, 5])
相关推荐
Studying 开龙wu9 小时前
深度学习PyTorch 实战九:YOLOv1目标检测从标注-训练-预测
pytorch·深度学习·yolo
ting945200013 小时前
动手学深度学习(PyTorch版)深度详解(8):现代循环神经网络(实战 + 避坑)
pytorch·rnn·深度学习
郝学胜-神的一滴14 小时前
二分类任务核心:BCE 损失函数从原理到 PyTorch 实战
人工智能·pytorch·python·算法·机器学习·分类·数据挖掘
T.i.s1 天前
总变差正则化(TV Loss)的思考
人工智能·pytorch·深度学习
盼小辉丶1 天前
PyTorch强化学习实战(4)——PyTorch基础
人工智能·pytorch·python·强化学习
keineahnung23452 天前
為什麼這個 Tensor 算 dense?PyTorch _eval_is_non_overlapping_and_dense 深入解析
人工智能·pytorch·python·深度学习
袋子(PJ)2 天前
2026年pytorch基础学习(基于jupyter notebook开发)——从原理到落地:PyTorch神经网络架构与工程优化解析
人工智能·pytorch·深度学习·学习·jupyter
九成宫2 天前
动手学深度学习PyTorch版初步安装过程
人工智能·pytorch·深度学习
AI技术增长2 天前
Pytorch图像去噪实战(十三):DDIM加速扩散模型采样,让去噪从1000步降到50步
人工智能·pytorch·python
小糖学代码2 天前
LLM系列:1.python入门:16.正则表达式与文本处理 (re)
人工智能·pytorch·python·深度学习·神经网络·正则表达式