Pytorch閱讀文檔中的flatten函數(shù)
Pytorch閱讀文檔中的flatten函數(shù)
pytorch中flatten函數(shù)
torch.flatten()
#展平一個(gè)連續(xù)范圍的維度,輸出類型為Tensor torch.flatten(input, start_dim=0, end_dim=-1) → Tensor # Parameters:input (Tensor) – 輸入為Tensor #start_dim (int) – 展平的開始維度 #end_dim (int) – 展平的最后維度 #example #一個(gè)3x2x2的三維張量 >>> t = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]) #當(dāng)開始維度為0,最后維度為-1,展開為一維 >>> torch.flatten(t) tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) #當(dāng)開始維度為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]) #下面的和上面進(jìn)行對(duì)比應(yīng)該就能看出是,當(dāng)鎖定最后的維度的時(shí)候 #前面的就會(huì)合并 >>> 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). #能力有限,個(gè)人認(rèn)為是用于卷積中的 #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中的一個(gè)函數(shù),用于將輸入張量進(jìn)行扁平化操作。它可以將多維的張量轉(zhuǎn)換為一維張量,保持?jǐn)?shù)據(jù)的順序不變。
參數(shù):
- `start_dim`(可選):指定開始扁平化的維度。默認(rèn)值為 1,表示從第二個(gè)維度開始扁平化。注意,維度索引是從 0 開始的。
- `end_dim`(可選):指定結(jié)束扁平化的維度。默認(rèn)值為 -1,表示扁平化到最后一個(gè)維度。
返回值:
- 返回一個(gè)新的張量,是輸入張量扁平化后的結(jié)果。
下面是一個(gè)示例,說明如何使用 `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` 是一個(gè) 2D 張量,形狀為 (2, 3)。使用 `torch.flatten()` 函數(shù)對(duì) `input` 進(jìn)行扁平化操作,將其轉(zhuǎn)換為一維張量。由于沒有指定 `start_dim` 和 `end_dim`,默認(rèn)從第二個(gè)維度(即行維度)開始扁平化,并扁平化到最后一個(gè)維度(即列維度)。最終的輸出張量 `output` 是一個(gè)一維張量,包含了原始張量中的所有元素,按照原始張量的順序排列。
請(qǐng)注意,`Flatten()` 函數(shù)返回的是一個(gè)新的張量,原始張量保持不變。
到此這篇關(guān)于Pytorch閱讀文檔中的flatten函數(shù)的文章就介紹到這了,更多相關(guān)Pytorch flatten函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何執(zhí)行精確的浮點(diǎn)數(shù)運(yùn)算
這篇文章主要介紹了Python如何執(zhí)行精確的浮點(diǎn)數(shù)運(yùn)算,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07windows下Python安裝、使用教程和Notepad++的使用教程
這篇文章主要介紹了windows下Python安裝、使用教程和Notepad++的使用教程,需要的朋友可以參考下2019-10-10Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError
這篇文章主要介紹了Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError,簡單介紹了python 發(fā)送郵件的步驟,需要的朋友可以參考下2019-12-12重寫django的model下的objects模型管理器方式
這篇文章主要介紹了重寫django的model下的objects模型管理器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05python 實(shí)用工具狀態(tài)機(jī)transitions
這篇文章主要介紹了python 實(shí)用工具狀態(tài)機(jī)transitions的使用,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-11-11