pytorch之torch.flatten()和torch.nn.Flatten()的用法
torch.flatten()和torch.nn.Flatten()的用法
flatten()函數(shù)的作用是將tensor鋪平成一維
torch.flatten(input, start_dim=0, end_dim=- 1) → Tensor
input (Tensor)– the input tensor.start_dim (int)– the first dim to flattenend_dim (int)– the last dim to flatten
start_dim和end_dim構(gòu)成了整個(gè)你要選擇鋪平的維度范圍
下面舉例說明
x = torch.tensor([[1,2], [3,4], [5,6]]) x = x.flatten(0) x ------------------------ tensor([1, 2, 3, 4, 5, 6])
對于圖片數(shù)據(jù),我們往往期望進(jìn)入fc層的維度為(channels, N)這樣
x = torch.tensor([[[1,2],[3,4]], [[5,6],[7,8]]])
x = x.flatten(1)
x
-------------------------
tensor([[1, 2],
[3, 4],
[5, 6]])注:
torch.nn.Flatten(start_dim=1, end_dim=- 1)
start_dim 默認(rèn)為 1
所以在構(gòu)建網(wǎng)絡(luò)時(shí),下面兩種是等價(jià)的
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
# The arguments for commonly used modules:
# torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0)
# torch.nn.MaxPool2d(kernel_size, stride=None, padding=0)
# input image size: [3, 128, 128]
self.cnn_layers = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(kernel_size=4, stride=4, padding=0),
)
self.fc_layers = nn.Sequential(
nn.Linear(256 * 8 * 8, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 11)
)
def forward(self, x):
# input (x): [batch_size, 3, 128, 128]
# output: [batch_size, 11]
# Extract features by convolutional layers.
x = self.cnn_layers(x)
# The extracted feature map must be flatten before going to fully-connected layers.
x = x.flatten(1)
# The features are transformed by fully-connected layers to obtain the final logits.
x = self.fc_layers(x)
return xclass Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(kernel_size=4, stride=4, padding=0),
nn.Flatten(),
nn.Linear(256 * 8 * 8, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 11)
)
def forward(self, x):
x = self.layers(x)
return x總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python pandas 計(jì)算每行的增長率與累計(jì)增長率
這篇文章主要介紹了Python pandas 計(jì)算每行的增長率與累計(jì)增長率,文章舉例詳細(xì)說明。需要的小伙伴可以參考一下2022-03-03
使用Python將PDF轉(zhuǎn)成Excel的代碼實(shí)現(xiàn)
在日常工作中,您是否曾被困擾于從復(fù)雜的PDF文檔中手動提取數(shù)據(jù),特別是表格數(shù)據(jù),然后逐一錄入到Excel,這項(xiàng)任務(wù)不僅耗時(shí)耗力,還極易引入人為錯(cuò)誤,嚴(yán)重影響工作效率,本文將深入探討如何利用Spire.PDF?for?Python這一高效庫,輕松實(shí)現(xiàn)PDF轉(zhuǎn)Excel的需求2025-10-10
tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解
今天小編就為大家分享一篇tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python實(shí)現(xiàn)OFD文件轉(zhuǎn)PDF
OFD 文件是由中國國家標(biāo)準(zhǔn)化管理委員會制定的國家標(biāo)準(zhǔn),是一種開放式文檔格式,具有高度可擴(kuò)展性和可編輯性,本文主要介紹了如何利用Python實(shí)現(xiàn)OFD文件轉(zhuǎn)PDF,需要的可以參考下2024-10-10
tensorflow實(shí)現(xiàn)對圖片的讀取的示例代碼
本篇文章主要介紹了tensorflow實(shí)現(xiàn)對圖片的讀取的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
在Python編程過程中用單元測試法調(diào)試代碼的介紹
這篇文章主要介紹了在Python編程過程中用單元測試法調(diào)試代碼的介紹,包括使用斷言等,有助于debug時(shí)的效率提升,需要的朋友可以參考下2015-04-04
Python2隨機(jī)數(shù)列生成器簡單實(shí)例
這篇文章主要介紹了Python2隨機(jī)數(shù)列生成器,結(jié)合簡單實(shí)例形式分析了Python基于random模塊操作隨機(jī)數(shù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
Python網(wǎng)絡(luò)爬蟲技術(shù)高階用法
網(wǎng)絡(luò)爬蟲成為了自動化數(shù)據(jù)抓取的核心工具,Python?擁有強(qiáng)大的第三方庫支持,在網(wǎng)絡(luò)爬蟲領(lǐng)域的應(yīng)用尤為廣泛,本文將深入探討?Python?網(wǎng)絡(luò)爬蟲的高階用法,包括處理反爬蟲機(jī)制、動態(tài)網(wǎng)頁抓取、分布式爬蟲以及并發(fā)和異步爬蟲等技術(shù),幫助讀者掌握高級Python爬蟲技術(shù)2024-12-12

