Pytorch閱讀文檔中的flatten函數(shù)
Pytorch閱讀文檔中的flatten函數(shù)
pytorch中flatten函數(shù)

torch.flatten()
#展平一個連續(xù)范圍的維度,輸出類型為Tensor
torch.flatten(input, start_dim=0, end_dim=-1) → Tensor
# Parameters:input (Tensor) – 輸入為Tensor
#start_dim (int) – 展平的開始維度
#end_dim (int) – 展平的最后維度
#example
#一個3x2x2的三維張量
>>> t = torch.tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]],
[[9, 10],
[11, 12]]])
#當開始維度為0,最后維度為-1,展開為一維
>>> torch.flatten(t)
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
#當開始維度為0,最后維度為-1,展開為3x4,也就是說第一維度不變,后面的壓縮
>>> torch.flatten(t, start_dim=1)
tensor([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> torch.flatten(t, start_dim=1).size()
torch.Size([3, 4])
#下面的和上面進行對比應(yīng)該就能看出是,當鎖定最后的維度的時候
#前面的就會合并
>>> torch.flatten(t, start_dim=0, end_dim=1)
tensor([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
>>> torch.flatten(t, start_dim=0, end_dim=1).size()
torch.Size([6, 2])torch.nn.Flatten()
Class torch.nn.Flatten(start_dim=1, end_dim=-1)
#Flattens a contiguous range of dims into a tensor.
#For use with Sequential. :
#param start_dim: first dim to flatten (default = 1).
#param end_dim: last dim to flatten (default = -1).
#能力有限,個人認為是用于卷積中的
#Shape:
#Input: (N, *dims)(N,?dims)
#Output: (N, \prod *dims)(N,∏?dims) (for the default case).
#官方example
>>> m = nn.Sequential(
>>> nn.Conv2d(1, 32, 5, 1, 1),
>>> nn.Flatten()
>>> )
#源代碼為 TORCH.NN.MODULES.FLATTEN
from .module import Module
[docs]class Flatten(Module):
r"""
Flattens a contiguous range of dims into a tensor. For use with :class:`~nn.Sequential`.
Args:
start_dim: first dim to flatten (default = 1).
end_dim: last dim to flatten (default = -1).
Shape:
- Input: :math:`(N, *dims)`
- Output: :math:`(N, \prod *dims)` (for the default case).
Examples::
>>> m = nn.Sequential(
>>> nn.Conv2d(1, 32, 5, 1, 1),
>>> nn.Flatten()
>>> )
"""
__constants__ = ['start_dim', 'end_dim']
def __init__(self, start_dim=1, end_dim=-1):
super(Flatten, self).__init__()
self.start_dim = start_dim
self.end_dim = end_dim
def forward(self, input):
return input.flatten(self.start_dim, self.end_dim)torch.Tensor.flatten()
和torch.flatten()一樣

PyTorch中Flatten(start_dim=1, end_dim=-1)是什么意思
`Flatten(start_dim=1, end_dim=-1)` 是PyTorch中的一個函數(shù),用于將輸入張量進行扁平化操作。它可以將多維的張量轉(zhuǎn)換為一維張量,保持數(shù)據(jù)的順序不變。
參數(shù):
- `start_dim`(可選):指定開始扁平化的維度。默認值為 1,表示從第二個維度開始扁平化。注意,維度索引是從 0 開始的。
- `end_dim`(可選):指定結(jié)束扁平化的維度。默認值為 -1,表示扁平化到最后一個維度。
返回值:
- 返回一個新的張量,是輸入張量扁平化后的結(jié)果。
下面是一個示例,說明如何使用 `Flatten()` 函數(shù):
import torch
input = torch.tensor([[1, 2, 3],
[4, 5, 6]])
output = torch.flatten(input, start_dim=0, end_dim=1)
print(output)
tensor([1, 2, 3, 4, 5, 6])在上面的示例中,輸入張量 `input` 是一個 2D 張量,形狀為 (2, 3)。使用 `torch.flatten()` 函數(shù)對 `input` 進行扁平化操作,將其轉(zhuǎn)換為一維張量。由于沒有指定 `start_dim` 和 `end_dim`,默認從第二個維度(即行維度)開始扁平化,并扁平化到最后一個維度(即列維度)。最終的輸出張量 `output` 是一個一維張量,包含了原始張量中的所有元素,按照原始張量的順序排列。
請注意,`Flatten()` 函數(shù)返回的是一個新的張量,原始張量保持不變。
到此這篇關(guān)于Pytorch閱讀文檔中的flatten函數(shù)的文章就介紹到這了,更多相關(guān)Pytorch flatten函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python + opencv對拍照得到的圖片進行背景去除的實現(xiàn)方法
這篇文章主要介紹了Python + opencv對拍照得到的圖片進行背景去除的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Python configparser模塊配置文件解析與應(yīng)用探究
在Python中,configparser模塊是用于處理配置文件的重要工具,本文將全面探討configparser模塊的使用方法,包括讀取、修改、寫入配置文件,以及如何在實際項目中應(yīng)用該模塊,結(jié)合豐富的示例代碼,將深入剖析該模塊的功能和靈活性2024-01-01
Python 內(nèi)置函數(shù)之隨機函數(shù)詳情
這篇文章主要介紹了Python 內(nèi)置函數(shù)之隨機函數(shù),文章將圍繞Python 內(nèi)置函數(shù)、隨機函數(shù)的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下,希望對你有所幫助2021-11-11
PyQt5實現(xiàn)簡單數(shù)據(jù)標注工具
這篇文章主要為大家詳細介紹了PyQt5實現(xiàn)簡單數(shù)據(jù)標注工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
Python實現(xiàn)二值掩膜影像去噪與邊緣強化方法詳解
這篇文章主要介紹了Python實現(xiàn)二值掩膜影像去噪與邊緣強化方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧2023-01-01

