欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年11月08日 09:51:23   作者:GhostintheCode  
PyTorch提供了一個(gè)非常方便的函數(shù)flatten()來完成這個(gè)任務(wù),本文將介紹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)文章

最新評(píng)論