本文整理匯總了Python中torch.nn.ReLU方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.ReLU方法的具體用法?Python nn.ReLU怎麽用?Python nn.ReLU使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.ReLU方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, input_size, n_channels, ngf, n_layers, activation='tanh'):
super(ImageDecoder, self).__init__()
ngf = ngf * (2 ** (n_layers - 2))
layers = [nn.ConvTranspose2d(input_size, ngf, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True)]
for i in range(1, n_layers - 1):
layers += [nn.ConvTranspose2d(ngf, ngf // 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf // 2),
nn.ReLU(True)]
ngf = ngf // 2
layers += [nn.ConvTranspose2d(ngf, n_channels, 4, 2, 1, bias=False)]
if activation == 'tanh':
layers += [nn.Tanh()]
elif activation == 'sigmoid':
layers += [nn.Sigmoid()]
else:
raise NotImplementedError
self.main = nn.Sequential(*layers)
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self):
super(GoogLeNet, self).__init__()
self.pre_layers = nn.Sequential(
nn.Conv2d(3, 192, kernel_size=3, padding=1),
nn.BatchNorm2d(192),
nn.ReLU(True),
)
self.a3 = Inception(192, 64, 96, 128, 16, 32, 32)
self.b3 = Inception(256, 128, 128, 192, 32, 96, 64)
self.maxpool = nn.MaxPool2d(3, stride=2, padding=1)
self.a4 = Inception(480, 192, 96, 208, 16, 48, 64)
self.b4 = Inception(512, 160, 112, 224, 24, 64, 64)
self.c4 = Inception(512, 128, 128, 256, 24, 64, 64)
self.d4 = Inception(512, 112, 144, 288, 32, 64, 64)
self.e4 = Inception(528, 256, 160, 320, 32, 128, 128)
self.a5 = Inception(832, 256, 160, 320, 32, 128, 128)
self.b5 = Inception(832, 384, 192, 384, 48, 128, 128)
self.avgpool = nn.AvgPool2d(8, stride=1)
self.linear = nn.Linear(1024, 10)
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, block, layers, num_classes=1000):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
# maxpool different from pytorch-resnet, to match tf-faster-rcnn
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
# use stride 1 for the last conv4 layer (same as tf-faster-rcnn)
self.layer4 = self._make_layer(block, 512, layers[3], stride=1)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
開發者ID:Sunarker,項目名稱:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代碼行數:24,代碼來源:resnet_v1.py
示例4: vgg
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def vgg(conv_arch, fc_features, fc_hidden_units=4096):
net = nn.Sequential()
# 卷積層部分
for i, (num_convs, in_channels, out_channels) in enumerate(conv_arch):
# 每經過一個 vgg_block 寬高減半
net.add_module('vgg_block_' + str(i+1), vgg_block(num_convs, in_channels, out_channels))
# 全連接部分
net.add_module('fc', nn.Sequential(
utils.FlattenLayer(),
nn.Linear(fc_features, fc_hidden_units),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(fc_hidden_units, fc_hidden_units),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(fc_hidden_units, 10)
))
return net
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, block, layers, num_classes=1000):
self.inplanes = 64
super(MyResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# note the increasing dilation
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilation=1)
self.layer3 = self._make_layer(block, 256, layers[2], stride=1, dilation=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4)
# these layers will not be used
self.avgpool = nn.AvgPool2d(7)
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, rnn_type, input_size, node_fdim, hidden_size, depth, dropout):
super(MPNEncoder, self).__init__()
self.hidden_size = hidden_size
self.input_size = input_size
self.depth = depth
self.W_o = nn.Sequential(
nn.Linear(node_fdim + hidden_size, hidden_size),
nn.ReLU(),
nn.Dropout(dropout)
)
if rnn_type == 'GRU':
self.rnn = GRU(input_size, hidden_size, depth)
elif rnn_type == 'LSTM':
self.rnn = LSTM(input_size, hidden_size, depth)
else:
raise ValueError('unsupported rnn cell type ' + rnn_type)
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self,
in_channels,
out_channels,
kernel_size=3,
deformable_groups=4):
super(FeatureAlign, self).__init__()
offset_channels = kernel_size * kernel_size * 2
self.conv_offset = nn.Conv2d(
4, deformable_groups * offset_channels, 1, bias=False)
self.conv_adaption = DeformConv(
in_channels,
out_channels,
kernel_size=kernel_size,
padding=(kernel_size - 1) // 2,
deformable_groups=deformable_groups)
self.relu = nn.ReLU(inplace=True)
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self,
in_channels,
out_channels,
kernel_size=3,
deformable_groups=4):
super(FeatureAdaption, self).__init__()
offset_channels = kernel_size * kernel_size * 2
self.conv_offset = nn.Conv2d(
2, deformable_groups * offset_channels, 1, bias=False)
self.conv_adaption = DeformConv(
in_channels,
out_channels,
kernel_size=kernel_size,
padding=(kernel_size - 1) // 2,
deformable_groups=deformable_groups)
self.relu = nn.ReLU(inplace=True)
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self,
num_branches,
blocks,
num_blocks,
in_channels,
num_channels,
multiscale_output=True,
with_cp=False,
conv_cfg=None,
norm_cfg=dict(type='BN')):
super(HRModule, self).__init__()
self._check_branches(num_branches, num_blocks, in_channels,
num_channels)
self.in_channels = in_channels
self.num_branches = num_branches
self.multiscale_output = multiscale_output
self.norm_cfg = norm_cfg
self.conv_cfg = conv_cfg
self.with_cp = with_cp
self.branches = self._make_branches(num_branches, blocks, num_blocks,
num_channels)
self.fuse_layers = self._make_fuse_layers()
self.relu = nn.ReLU(inplace=False)
示例10: _make_layers
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
# net = VGG('VGG11')
# x = torch.randn(2,3,32,32)
# print(net(Variable(x)).size())
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__( self ):
super(CNN, self).__init__()
self.elmo_feature_extractor = nn.Sequential(
nn.Conv2d( 1024, 32, kernel_size=(7,1), padding=(3,0) ),
nn.ReLU(),
nn.Dropout( 0.25 ),
)
n_final_in = 32
self.dssp3_classifier = nn.Sequential(
nn.Conv2d( n_final_in, 3, kernel_size=(7,1), padding=(3,0))
)
self.dssp8_classifier = nn.Sequential(
nn.Conv2d( n_final_in, 8, kernel_size=(7,1), padding=(3,0))
)
self.diso_classifier = nn.Sequential(
nn.Conv2d( n_final_in, 2, kernel_size=(7,1), padding=(3,0))
)
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, vocab_size, word_dim, embed_size, use_abs=False, glove_path='data/glove.pkl'):
super(EncoderTextCNN, self).__init__()
self.use_abs = use_abs
self.embed_size = embed_size
# word embedding
self.embed = nn.Embedding(vocab_size, word_dim-300, padding_idx=0) # 0 for <pad>
_, embed_weight = pickle.load(open(glove_path, 'rb'))
self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False)
channel_num = embed_size // 4
self.conv2 = nn.Conv1d(word_dim, channel_num, 2)
self.conv3 = nn.Conv1d(word_dim, channel_num, 3)
self.conv4 = nn.Conv1d(word_dim, channel_num, 4)
self.conv5 = nn.Conv1d(word_dim, channel_num, 5)
self.drop = nn.Dropout(p=0.5)
self.relu = nn.ReLU()
# self.mlp = nn.Linear(embed_size, embed_size)
self.init_weights()
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, block, layers, in_channels=3):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, in_ch, out_ch,group_conv,dilation=1):
super(double_conv, self).__init__()
if group_conv:
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1,groups = min(out_ch,in_ch)),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1,groups = out_ch),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
else:
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ReLU [as 別名]
def __init__(self, args):
super(HierVGNN, self).__init__()
self.latent_size = args.latent_size
self.encoder = HierMPNEncoder(args.vocab, args.atom_vocab, args.rnn_type, args.embed_size, args.hidden_size, args.depthT, args.depthG, args.dropout)
self.decoder = HierMPNDecoder(args.vocab, args.atom_vocab, args.rnn_type, args.embed_size, args.hidden_size, args.hidden_size, args.diterT, args.diterG, args.dropout, attention=True)
self.encoder.tie_embedding(self.decoder.hmpn)
self.T_mean = nn.Linear(args.hidden_size, args.latent_size)
self.T_var = nn.Linear(args.hidden_size, args.latent_size)
self.G_mean = nn.Linear(args.hidden_size, args.latent_size)
self.G_var = nn.Linear(args.hidden_size, args.latent_size)
self.W_tree = nn.Sequential( nn.Linear(args.hidden_size + args.latent_size, args.hidden_size), nn.ReLU() )
self.W_graph = nn.Sequential( nn.Linear(args.hidden_size + args.latent_size, args.hidden_size), nn.ReLU() )