本文整理匯總了Python中torch.nn.ModuleList方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.ModuleList方法的具體用法?Python nn.ModuleList怎麽用?Python nn.ModuleList使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.ModuleList方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, in_channels, out_channels, dilations=(1, 3, 6, 1)):
super().__init__()
assert dilations[-1] == 1
self.aspp = nn.ModuleList()
for dilation in dilations:
kernel_size = 3 if dilation > 1 else 1
padding = dilation if dilation > 1 else 0
conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=1,
dilation=dilation,
padding=padding,
bias=True)
self.aspp.append(conv)
self.gap = nn.AdaptiveAvgPool2d(1)
self.init_weights()
示例2: _init_cls_convs
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def _init_cls_convs(self):
"""Initialize classification conv layers of the head."""
self.cls_convs = nn.ModuleList()
for i in range(self.stacked_convs):
chn = self.in_channels if i == 0 else self.feat_channels
if self.dcn_on_last_conv and i == self.stacked_convs - 1:
conv_cfg = dict(type='DCNv2')
else:
conv_cfg = self.conv_cfg
self.cls_convs.append(
ConvModule(
chn,
self.feat_channels,
3,
stride=1,
padding=1,
conv_cfg=conv_cfg,
norm_cfg=self.norm_cfg,
bias=self.conv_bias))
示例3: _init_reg_convs
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def _init_reg_convs(self):
"""Initialize bbox regression conv layers of the head."""
self.reg_convs = nn.ModuleList()
for i in range(self.stacked_convs):
chn = self.in_channels if i == 0 else self.feat_channels
if self.dcn_on_last_conv and i == self.stacked_convs - 1:
conv_cfg = dict(type='DCNv2')
else:
conv_cfg = self.conv_cfg
self.reg_convs.append(
ConvModule(
chn,
self.feat_channels,
3,
stride=1,
padding=1,
conv_cfg=conv_cfg,
norm_cfg=self.norm_cfg,
bias=self.conv_bias))
示例4: build_roi_layers
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def build_roi_layers(self, layer_cfg, featmap_strides):
"""Build RoI operator to extract feature from each level feature map.
Args:
layer_cfg (dict): Dictionary to construct and config RoI layer
operation. Options are modules under ``mmdet/ops`` such as
``RoIAlign``.
featmap_strides (int): The stride of input feature map w.r.t to the
original image size, which would be used to scale RoI
coordinate (original image coordinate system) to feature
coordinate system.
Returns:
nn.ModuleList: The RoI extractor modules for each level feature
map.
"""
cfg = layer_cfg.copy()
layer_type = cfg.pop('type')
assert hasattr(ops, layer_type)
layer_cls = getattr(ops, layer_type)
roi_layers = nn.ModuleList(
[layer_cls(spatial_scale=1 / s, **cfg) for s in featmap_strides])
return roi_layers
示例5: _check_mask_head
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def _check_mask_head(mask_cfg, mask_head):
import torch.nn as nn
if isinstance(mask_cfg, list):
for single_mask_cfg, single_mask_head in zip(mask_cfg, mask_head):
_check_mask_head(single_mask_cfg, single_mask_head)
elif isinstance(mask_head, nn.ModuleList):
for single_mask_head in mask_head:
_check_mask_head(mask_cfg, single_mask_head)
else:
assert mask_cfg['type'] == mask_head.__class__.__name__
assert mask_cfg.in_channels == mask_head.in_channels
class_agnostic = mask_cfg.get('class_agnostic', False)
out_dim = (1 if class_agnostic else mask_cfg.num_classes)
if hasattr(mask_head, 'conv_logits'):
assert (mask_cfg.conv_out_channels ==
mask_head.conv_logits.in_channels)
assert mask_head.conv_logits.out_channels == out_dim
else:
assert mask_cfg.fc_out_channels == mask_head.fc_logits.in_features
assert (mask_head.fc_logits.out_features == out_dim *
mask_head.output_area)
示例6: _check_bbox_head
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def _check_bbox_head(bbox_cfg, bbox_head):
import torch.nn as nn
if isinstance(bbox_cfg, list):
for single_bbox_cfg, single_bbox_head in zip(bbox_cfg, bbox_head):
_check_bbox_head(single_bbox_cfg, single_bbox_head)
elif isinstance(bbox_head, nn.ModuleList):
for single_bbox_head in bbox_head:
_check_bbox_head(bbox_cfg, single_bbox_head)
else:
assert bbox_cfg['type'] == bbox_head.__class__.__name__
assert bbox_cfg.in_channels == bbox_head.in_channels
with_cls = bbox_cfg.get('with_cls', True)
if with_cls:
fc_out_channels = bbox_cfg.get('fc_out_channels', 2048)
assert (fc_out_channels == bbox_head.fc_cls.in_features)
assert bbox_cfg.num_classes + 1 == bbox_head.fc_cls.out_features
with_reg = bbox_cfg.get('with_reg', True)
if with_reg:
out_dim = (4 if bbox_cfg.reg_class_agnostic else 4 *
bbox_cfg.num_classes)
assert bbox_head.fc_reg.out_features == out_dim
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, e, hidden_state_size, message_size, n_layers, l_target, type='regression'):
super(MpnnGGNN, self).__init__()
# Define message
self.m = nn.ModuleList([MessageFunction('ggnn', args={'e_label': e, 'in': hidden_state_size, 'out': message_size})])
# Define Update
self.u = nn.ModuleList([UpdateFunction('ggnn',
args={'in_m': message_size,
'out': hidden_state_size})])
# Define Readout
self.r = ReadoutFunction('ggnn',
args={'in': hidden_state_size,
'target': l_target})
self.type = type
self.args = {}
self.args['out'] = hidden_state_size
self.n_layers = n_layers
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, d, in_n, out_update, hidden_state_readout, l_target, type='regression'):
super(MpnnDuvenaud, self).__init__()
n_layers = len(out_update)
# Define message 1 & 2
self.m = nn.ModuleList([MessageFunction('duvenaud') for _ in range(n_layers)])
# Define Update 1 & 2
self.u = nn.ModuleList([UpdateFunction('duvenaud', args={'deg': d, 'in': self.m[i].get_out_size(in_n[0], in_n[1]), 'out': out_update[0]}) if i == 0 else
UpdateFunction('duvenaud', args={'deg': d, 'in': self.m[i].get_out_size(out_update[i-1], in_n[1]), 'out': out_update[i]}) for i in range(n_layers)])
# Define Readout
self.r = ReadoutFunction('duvenaud',
args={'layers': len(self.m) + 1,
'in': [in_n[0] if i == 0 else out_update[i-1] for i in range(n_layers+1)],
'out': hidden_state_readout,
'target': l_target})
self.type = type
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, in_n, hidden_state_size, message_size, n_layers, l_target, type='regression'):
super(MPNN, self).__init__()
# Define message
self.m = nn.ModuleList(
[MessageFunction('mpnn', args={'edge_feat': in_n[1], 'in': hidden_state_size, 'out': message_size})])
# Define Update
self.u = nn.ModuleList([UpdateFunction('mpnn',
args={'in_m': message_size,
'out': hidden_state_size})])
# Define Readout
self.r = ReadoutFunction('mpnn',
args={'in': hidden_state_size,
'target': l_target})
self.type = type
self.args = {}
self.args['out'] = hidden_state_size
self.n_layers = n_layers
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, in_n, out_message, out_update, l_target, type='regression'):
super(MpnnIntNet, self).__init__()
n_layers = len(out_update)
# Define message 1 & 2
self.m = nn.ModuleList([MessageFunction('intnet', args={'in': 2*in_n[0] + in_n[1], 'out': out_message[i]})
if i == 0 else
MessageFunction('intnet', args={'in': 2*out_update[i-1] + in_n[1], 'out': out_message[i]})
for i in range(n_layers)])
# Define Update 1 & 2
self.u = nn.ModuleList([UpdateFunction('intnet', args={'in': in_n[0]+out_message[i], 'out': out_update[i]})
if i == 0 else
UpdateFunction('intnet', args={'in': out_update[i-1]+out_message[i], 'out': out_update[i]})
for i in range(n_layers)])
# Define Readout
self.r = ReadoutFunction('intnet', args={'in': out_update[-1], 'target': l_target})
self.type = type
示例11: init_duvenaud
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def init_duvenaud(self, params):
learn_args = []
learn_modules = []
args = {}
args['out'] = params['out']
# Define a parameter matrix W for each layer.
for l in range(params['layers']):
learn_args.append(nn.Parameter(torch.randn(params['in'][l], params['out'])))
# learn_modules.append(nn.Linear(params['out'], params['target']))
learn_modules.append(NNet(n_in=params['out'], n_out=params['target']))
return nn.ParameterList(learn_args), nn.ModuleList(learn_modules), args
# GG-NN, Li et al.
示例12: init_ggnn
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def init_ggnn(self, params):
learn_args = []
learn_modules = []
args = {}
# i
learn_modules.append(NNet(n_in=2*params['in'], n_out=params['target']))
# j
learn_modules.append(NNet(n_in=params['in'], n_out=params['target']))
args['out'] = params['target']
return nn.ParameterList(learn_args), nn.ModuleList(learn_modules), args
# Battaglia et al. (2016), Interaction Networks
示例13: __set_update
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __set_update(self, update_def, args):
self.u_definition = update_def.lower()
self.u_function = {
'duvenaud': self.u_duvenaud,
'ggnn': self.u_ggnn,
'intnet': self.u_intnet,
'mpnn': self.u_mpnn
}.get(self.u_definition, None)
if self.u_function is None:
print('WARNING!: Update Function has not been set correctly\n\tIncorrect definition ' + update_def)
init_parameters = {
'duvenaud': self.init_duvenaud,
'ggnn': self.init_ggnn,
'intnet': self.init_intnet,
'mpnn': self.init_mpnn
}.get(self.u_definition, lambda x: (nn.ParameterList([]), nn.ModuleList([]), {}))
self.learn_args, self.learn_modules, self.args = init_parameters(args)
# Get the name of the used update function
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self, base_model: torch.nn.Module, num_classes: int, weights_url: str = None):
super().__init__()
if not hasattr(self, 'decoder_block'):
self.decoder_block = UnetDecoderBlock
if not hasattr(self, 'bottleneck_type'):
self.bottleneck_type = ConvBottleneck
if weights_url is not None:
print("Model weights inited by url")
pretrained_weights = model_zoo.load_url(weights_url)
model_state_dict = base_model.state_dict()
pretrained_weights = {k: v for k, v in pretrained_weights.items() if k in model_state_dict}
base_model.load_state_dict(pretrained_weights)
filters = [64, 64, 128, 256, 512]
self.bottlenecks = nn.ModuleList([self.bottleneck_type(f * 2, f) for f in reversed(filters[:-1])])
self.decoder_stages = nn.ModuleList([self.get_decoder(filters, idx) for idx in range(1, len(filters))])
self.encoder_stages = nn.ModuleList([self.get_encoder(base_model, idx) for idx in range(len(filters))])
self.last_upsample = self.decoder_block(filters[0], filters[0])
self.final = self.make_final_classifier(filters[0], num_classes)
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ModuleList [as 別名]
def __init__(self):
super().__init__()
self.prenet = PreNet(in_features=hp.E) # [N, T, E//2]
self.conv1d_bank = Conv1dBank(K=hp.K, in_channels=hp.E // 2, out_channels=hp.E // 2) # [N, T, E//2 * K]
self.conv1d_1 = Conv1d(in_channels=hp.K * hp.E // 2, out_channels=hp.E // 2, kernel_size=3) # [N, T, E//2]
self.conv1d_2 = Conv1d(in_channels=hp.E // 2, out_channels=hp.E // 2, kernel_size=3) # [N, T, E//2]
self.bn1 = BatchNorm1d(num_features=hp.E // 2)
self.bn2 = BatchNorm1d(num_features=hp.E // 2)
self.highways = nn.ModuleList()
for i in range(hp.num_highways):
self.highways.append(Highway(in_features=hp.E // 2, out_features=hp.E // 2))
self.gru = nn.GRU(input_size=hp.E // 2, hidden_size=hp.E // 2, num_layers=2, bidirectional=True, batch_first=True)