本文整理匯總了Python中torch.nn.ZeroPad2d方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.ZeroPad2d方法的具體用法?Python nn.ZeroPad2d怎麽用?Python nn.ZeroPad2d使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.ZeroPad2d方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, num_filters_in, num_filters_out, filter_size=(2,3), stride=(1,1),
shift_output_down=False, norm='weight_norm'):
super(down_shifted_conv2d, self).__init__()
assert norm in [None, 'batch_norm', 'weight_norm']
self.conv = nn.Conv2d(num_filters_in, num_filters_out, filter_size, stride)
self.shift_output_down = shift_output_down
self.norm = norm
self.pad = nn.ZeroPad2d((int((filter_size[1] - 1) / 2), # pad left
int((filter_size[1] - 1) / 2), # pad right
filter_size[0] - 1, # pad top
0) ) # pad down
if norm == 'weight_norm':
self.conv = wn(self.conv)
elif norm == 'batch_norm':
self.bn = nn.BatchNorm2d(num_filters_out)
if shift_output_down :
self.down_shift = lambda x : down_shift(x, pad=nn.ZeroPad2d((0, 0, 1, 0)))
示例2: max_pool2d
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def max_pool2d(h_kernel_size, h_stride=1):
def compile_fn(di, dh):
(_, _, height, width) = di['in'].size()
padding = nn.ZeroPad2d(
calculate_same_padding(height, width, dh['stride'],
dh['kernel_size']))
max_pool = nn.MaxPool2d(dh['kernel_size'], stride=dh['stride'])
def fn(di):
x = padding(di['in'])
return {'out': max_pool(x)}
return fn, [padding, max_pool]
return siso_pytorch_module('MaxPool2D', compile_fn, {
'kernel_size': h_kernel_size,
'stride': h_stride
})
示例3: avg_pool2d
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def avg_pool2d(h_kernel_size, h_stride=1):
def compile_fn(di, dh):
(_, _, height, width) = di['in'].size()
padding = nn.ZeroPad2d(
calculate_same_padding(height, width, dh['stride'],
dh['kernel_size']))
avg_pool = nn.AvgPool2d(dh['kernel_size'], stride=dh['stride'])
def fn(di):
x = padding(di['in'])
return {'out': avg_pool(x)}
return fn, [padding, avg_pool]
return siso_pytorch_module('AvgPool2D', compile_fn, {
'kernel_size': h_kernel_size,
'stride': h_stride
})
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
self.stride = self.stride if len(self.stride) == 2 else [self.stride[0]] * 2
# Calculate padding based on image size and save it
assert image_size is not None
ih, iw = image_size if type(image_size) == list else [image_size, image_size]
kh, kw = self.weight.size()[-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] + (kh - 1) * self.dilation[0] + 1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] + (kw - 1) * self.dilation[1] + 1 - iw, 0)
if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d((pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
self.stride = self.stride if len(self.stride) == 2 else [
self.stride[0]] * 2
# Calculate padding based on image size and save it
assert image_size is not None
ih, iw = image_size if type(image_size) == list else [
image_size, image_size]
kh, kw = self.weight.size()[-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] +
(kh - 1) * self.dilation[0] + 1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] +
(kw - 1) * self.dilation[1] + 1 - iw, 0)
if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d(
(pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
stem_cell=False, zero_pad=False):
super(BranchSeparables, self).__init__()
padding = kernel_size // 2
middle_channels = out_channels if stem_cell else in_channels
self.zero_pad = nn.ZeroPad2d((1, 0, 1, 0)) if zero_pad else None
self.relu_1 = nn.ReLU()
self.separable_1 = SeparableConv2d(in_channels, middle_channels,
kernel_size, dw_stride=stride,
dw_padding=padding)
self.bn_sep_1 = nn.BatchNorm2d(middle_channels, eps=0.001)
self.relu_2 = nn.ReLU()
self.separable_2 = SeparableConv2d(middle_channels, out_channels,
kernel_size, dw_stride=1,
dw_padding=padding)
self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001)
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(
self, image_size, in_channels, out_channels, kernel_size, **kernel_wargs
):
super().__init__(in_channels, out_channels, kernel_size, **kernel_wargs)
image_h, image_w = image_size
kernel_h, kernel_w = self.weight.size()[-2:]
stride_h, stride_w = self.stride
dilation_h, dilation_w = self.dilation
out_h, out_w = math.ceil(image_h / stride_h), math.ceil(image_w / stride_w)
pad_h = max(
(out_h - 1) * self.stride[0] + (kernel_h - 1) * dilation_h + 1 - image_h, 0
)
pad_w = max(
(out_w - 1) * self.stride[1] + (kernel_w - 1) * dilation_w + 1 - image_w, 0
)
self.out_h = out_h
self.out_w = out_w
self.same_padding = None
if pad_h > 0 or pad_w > 0:
self.same_padding = nn.ZeroPad2d(
(pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
)
self.image_size = image_size
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self,
in_channels,
out_channels,
stride):
super(ShakeShakeShortcut, self).__init__()
assert (out_channels % 2 == 0)
mid_channels = out_channels // 2
self.pool = nn.AvgPool2d(
kernel_size=1,
stride=stride)
self.conv1 = conv1x1(
in_channels=in_channels,
out_channels=mid_channels)
self.conv2 = conv1x1(
in_channels=in_channels,
out_channels=mid_channels)
self.bn = nn.BatchNorm2d(num_features=out_channels)
self.pad = nn.ZeroPad2d(padding=(1, 0, 1, 0))
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride,
padding,
extra_padding=False):
super(NasDwsConv, self).__init__()
self.extra_padding = extra_padding
self.activ = nn.ReLU()
self.conv = DwsConv(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
bias=False)
self.bn = nasnet_batch_norm(channels=out_channels)
if self.extra_padding:
self.pad = nn.ZeroPad2d(padding=(1, 0, 1, 0))
示例10: data
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def data(device="cpu"):
N, C, Hin, Win = 100, 10, 32, 32
padding = [1, 2, 3, 4]
Hout = Hin + padding[2] + padding[3]
Wout = Win + padding[0] + padding[1]
X = randn(N, C, Hin, Win, requires_grad=True, device=device)
module = extend(ZeroPad2d(padding)).to(device=device)
out = module(X)
vout = randn(N, C, Hin, Win, device=device)
vin = randn(N, C, Hout, Wout, device=device)
return {
"X": X,
"module": module,
"output": out,
"vout_ag": vout,
"vout_bp": vout.view(N, -1, 1),
"vin_ag": vin,
"vin_bp": vin.view(N, -1, 1),
}
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, config):
super(BertPreTrainedModel, self).__init__(config)
config.kernel_size = basic_config.dpcnn.kernel_size
config.num_filters = basic_config.dpcnn.num_filters
self.bert = BertModel(config)
for param in self.bert.parameters():
param.requires_grad = True
self.conv_region = nn.Conv2d(
1, config.num_filters, (3, config.hidden_size), stride=1)
self.conv = nn.Conv2d(config.num_filters,
config.num_filters, (3, 1), stride=1)
self.max_pool = nn.MaxPool2d(kernel_size=(3, 1), stride=2)
self.padding1 = nn.ZeroPad2d((0, 0, 1, 1)) # top bottom
self.padding2 = nn.ZeroPad2d((0, 0, 0, 1)) # bottom
self.relu = nn.ReLU()
self.fc = nn.Linear(config.num_filters, config.num_labels)
開發者ID:hscspring,項目名稱:Multi-Label-Text-Classification-for-Chinese,代碼行數:19,代碼來源:bert_for_multi_label.py
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, input_shape):
super(GeneratorUNet, self).__init__()
channels, _, _ = input_shape
self.down1 = UNetDown(channels, 64, normalize=False)
self.down2 = UNetDown(64, 128)
self.down3 = UNetDown(128, 256, dropout=0.5)
self.down4 = UNetDown(256, 512, dropout=0.5)
self.down5 = UNetDown(512, 512, dropout=0.5)
self.down6 = UNetDown(512, 512, dropout=0.5, normalize=False)
self.up1 = UNetUp(512, 512, dropout=0.5)
self.up2 = UNetUp(1024, 512, dropout=0.5)
self.up3 = UNetUp(1024, 256, dropout=0.5)
self.up4 = UNetUp(512, 128)
self.up5 = UNetUp(256, 64)
self.final = nn.Sequential(
nn.Upsample(scale_factor=2), nn.ZeroPad2d((1, 0, 1, 0)), nn.Conv2d(128, channels, 4, padding=1), nn.Tanh()
)
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, input_shape):
super(Discriminator, self).__init__()
channels, height, width = input_shape
# Calculate output shape of image discriminator (PatchGAN)
self.output_shape = (1, height // 2 ** 4, width // 2 ** 4)
def discriminator_block(in_filters, out_filters, normalize=True):
"""Returns downsampling layers of each discriminator block"""
layers = [nn.Conv2d(in_filters, out_filters, 4, stride=2, padding=1)]
if normalize:
layers.append(nn.InstanceNorm2d(out_filters))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*discriminator_block(channels, 64, normalize=False),
*discriminator_block(64, 128),
*discriminator_block(128, 256),
*discriminator_block(256, 512),
nn.ZeroPad2d((1, 0, 1, 0)),
nn.Conv2d(512, 1, 4, padding=1)
)
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, in_channels=3):
super(Discriminator, self).__init__()
def discriminator_block(in_filters, out_filters, normalization=True):
"""Returns downsampling layers of each discriminator block"""
layers = [nn.Conv2d(in_filters, out_filters, 4, stride=2, padding=1)]
if normalization:
layers.append(nn.InstanceNorm2d(out_filters))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*discriminator_block(in_channels * 2, 64, normalization=False),
*discriminator_block(64, 128),
*discriminator_block(128, 256),
*discriminator_block(256, 512),
nn.ZeroPad2d((1, 0, 1, 0)),
nn.Conv2d(512, 1, 4, padding=1, bias=False)
)
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import ZeroPad2d [as 別名]
def __init__(self, in_dim, out_dim, kernel_size=3, stride=1, padding=0,
pad_type='reflect', bias=True, norm_layer=None, nl_layer=None):
super(Conv2dBlock, self).__init__()
if pad_type == 'reflect':
self.pad = nn.ReflectionPad2d(padding)
elif pad_type == 'replicate':
self.pad = nn.ReplicationPad2d(padding)
elif pad_type == 'zero':
self.pad = nn.ZeroPad2d(padding)
self.conv = spectral_norm(nn.Conv2d(in_dim, out_dim, kernel_size=kernel_size,
stride=stride, padding=0, bias=bias))
if norm_layer is not None:
self.norm = norm_layer(out_dim)
else:
self.norm = lambda x: x
if nl_layer is not None:
self.activation = nl_layer()
else:
self.activation = lambda x: x