Pytorch網(wǎng)絡(luò)搭建及訓(xùn)練步驟

一煞茫、網(wǎng)絡(luò)設(shè)計

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

二帕涌、前向傳播

optimizer = optim.SGD(net.parameters(), lr=0.01)
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)

三、損失函數(shù)

target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()
loss = criterion(output, target)
loss.backward()

四续徽、優(yōu)化參數(shù)

optimizer.step()    # Does the update

五蚓曼、完整過程

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
net = net.to("cuda")
print(net)

####
input = torch.randn(1, 1, 32, 32)
input = input.to("cuda")

######
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
criterion = nn.MSELoss()
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
target = target.to("cuda")
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update

六、注意事項

6.1用gpu訓(xùn)練

需要對網(wǎng)絡(luò)及輸入的數(shù)據(jù)設(shè)置gpu處理钦扭,即

net.to("cuda")
input.to("cuda")
target.to("cuda")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末纫版,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子客情,更是在濱河造成了極大的恐慌捎琐,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件裹匙,死亡現(xiàn)場離奇詭異瑞凑,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)概页,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門籽御,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事技掏×褰” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵哑梳,是天一觀的道長劲阎。 經(jīng)常有香客問我,道長鸠真,這世上最難降的妖魔是什么悯仙? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮吠卷,結(jié)果婚禮上锡垄,老公的妹妹穿的比我還像新娘。我一直安慰自己祭隔,他們只是感情好货岭,可當(dāng)我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著疾渴,像睡著了一般千贯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上搞坝,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天丈牢,我揣著相機(jī)與錄音,去河邊找鬼瞄沙。 笑死,一個胖子當(dāng)著我的面吹牛慌核,可吹牛的內(nèi)容都是我干的距境。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼垮卓,長吁一口氣:“原來是場噩夢啊……” “哼垫桂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起粟按,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤诬滩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后灭将,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疼鸟,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年庙曙,在試婚紗的時候發(fā)現(xiàn)自己被綠了空镜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吴攒,靈堂內(nèi)的尸體忽然破棺而出张抄,到底是詐尸還是另有隱情,我是刑警寧澤洼怔,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布署惯,位于F島的核電站,受9級特大地震影響镣隶,放射性物質(zhì)發(fā)生泄漏极谊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一矾缓、第九天 我趴在偏房一處隱蔽的房頂上張望怀酷。 院中可真熱鬧,春花似錦嗜闻、人聲如沸蜕依。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽样眠。三九已至,卻和暖如春翠肘,著一層夾襖步出監(jiān)牢的瞬間檐束,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工束倍, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留被丧,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓绪妹,卻偏偏與公主長得像甥桂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子邮旷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,941評論 2 355