torch.flatten
时间: 2023-10-15 15:25:26 浏览: 145
0460-极智开发-解读torch.flatten算子
5星 · 资源好评率100%
torch.flatten()函数是PyTorch中用于将张量降维的函数。具体使用方法如下:
1. 首先创建一个三维张量x,例如x = torch.randn(2, 3, 4)。
2. 调用torch.flatten()函数,可以将张量x降维为一维向量。例如A = torch.flatten(x),则A的形状为torch.Size([24]),A的内容为tensor([0.5366, 0.5488, 0.4033, -0.5649, -0.9119, 0.8464, -2.6698, 0.9435, 1.4485, 0.6482, -0.3760, 0.4114, 0.1044, 0.8057, -0.6402, 0.4294, 0.4673, -3.0244, 0.4310, -0.2473, 0.9410, 0.1142, 1.8234, 0.9855)。
3. 可以通过指定参数dim来控制降维的方式。例如B = torch.flatten(x, 1),则将张量x以行的方式降维为一个二维张量。B的形状为torch.Size([2, 12]),B的内容为tensor([[-0.6268, 2.0879, -0.3395, 0.7372, 1.2479, 0.7701, -1.0685, -0.1118, 0.8185, -0.7564, -0.1037, 0.6884],[ 0.0925, -0.9300, -1.2214, 0.1166, 1.7271, 0.8715, -0.0598, 0.5371, 0.3556, -0.7636, 0.4855, 0.0844]])。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* [torch.flatten()函数详解](https://blog.csdn.net/weixin_46319994/article/details/123118446)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [pytorch permute维度转换方法](https://download.csdn.net/download/weixin_38738189/12865675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文