您好,登錄后才能下訂單哦!
我就廢話不多說了,直接上代碼吧!
# -*- coding: utf-8 -*- #@Time :2019/7/1 13:34 #@Author :XiaoMa import torch as t from torch import nn #Sequential的三種寫法 net1=nn.Sequential() net1.add_module('conv',nn.Conv2d(3,3,3)) #Conv2D(輸入通道數,輸出通道數,卷積核大小) net1.add_module('batchnorm',nn.BatchNorm2d(3)) #BatchNorm2d(特征數) net1.add_module('activation_layer',nn.ReLU()) net2=nn.Sequential(nn.Conv2d(3,3,3), nn.BatchNorm2d(3), nn.ReLU() ) from collections import OrderedDict net3=nn.Sequential(OrderedDict([ ('conv1',nn.Conv2d(3,3,3)), ('bh2',nn.BatchNorm2d(3)), ('al',nn.ReLU()) ])) print('net1',net1) print('net2',net2) print('net3',net3) #可根據名字或序號取出子module print(net1.conv,net2[0],net3.conv1)
輸出結果:
net1 Sequential( (conv): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1)) (batchnorm): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (activation_layer): ReLU() ) net2 Sequential( (0): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1)) (1): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (2): ReLU() ) net3 Sequential( (conv1): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1)) (bh2): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (al): ReLU() ) Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1)) Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1)) Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1))
以上這篇pytorch實現特殊的Module--Sqeuential三種寫法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。