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])
相关推荐
多恩Stone1 天前
【3DV 进阶-9】Hunyuan3D2.1 中的 MoE
人工智能·pytorch·python·算法·aigc
shayudiandian1 天前
TensorFlow vs PyTorch:哪个更适合你?
人工智能·pytorch·tensorflow
Keep_Trying_Go1 天前
基于Transformer的目标统计方法(CounTR: Transformer-based Generalised Visual Counting)
人工智能·pytorch·python·深度学习·transformer·多模态·目标统计
人工智能培训1 天前
卷积神经网络(CNN)详细介绍及其原理详解(2)
人工智能·神经网络·cnn
Aspect of twilight2 天前
PyTorch DDP分布式训练Pytorch代码讲解
人工智能·pytorch·python
tomeasure2 天前
INTERNAL ASSERT FAILED at “/pytorch/c10/cuda/CUDACachingAllocator.cpp“:983
人工智能·pytorch·python·nvidia
一瞬祈望2 天前
【环境配置】Windows 下使用 Anaconda 创建 Python 3.8 环境 + 安装 PyTorch + CUDA(完整教程)
pytorch·windows·python
喜乐boy2 天前
CV系列——Conda + PyTorch + CUDA + cuDNN + Python 环境无脑安装速查笔记[2025.12]
pytorch·python·conda·cuda·cv
过尽漉雪千山2 天前
Anaconda的虚拟环境下使用清华源镜像安装Pytorch
人工智能·pytorch·python·深度学习·机器学习
weixin_457760002 天前
GIOU (Generalized Intersection over Union) 详解
pytorch·python