本文整理汇总了Python中torch.nn.utils.spectral_norm方法的典型用法代码示例。如果您正苦于以下问题:Python utils.spectral_norm方法的具体用法?Python utils.spectral_norm怎么用?Python utils.spectral_norm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.utils
的用法示例。
在下文中一共展示了utils.spectral_norm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, in_ch, out_ch, h_ch=None, ksize=3, pad=1,
activation=F.relu, downsample=False):
super(Block, self).__init__()
self.activation = activation
self.downsample = downsample
self.learnable_sc = (in_ch != out_ch) or downsample
if h_ch is None:
h_ch = in_ch
else:
h_ch = out_ch
self.c1 = utils.spectral_norm(nn.Conv2d(in_ch, h_ch, ksize, 1, pad))
self.c2 = utils.spectral_norm(nn.Conv2d(h_ch, out_ch, ksize, 1, pad))
if self.learnable_sc:
self.c_sc = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, 1, 1, 0))
self._initialize()
示例2: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, num_features, num_classes=0, activation=F.relu):
super(SNResNetProjectionDiscriminator, self).__init__()
self.num_features = num_features
self.num_classes = num_classes
self.activation = activation
self.block1 = OptimizedBlock(3, num_features)
self.block2 = Block(num_features, num_features * 2,
activation=activation, downsample=True)
self.block3 = Block(num_features * 2, num_features * 4,
activation=activation, downsample=True)
self.block4 = Block(num_features * 4, num_features * 8,
activation=activation, downsample=True)
self.block5 = Block(num_features * 8, num_features * 16,
activation=activation, downsample=True)
self.block6 = Block(num_features * 16, num_features * 16,
activation=activation, downsample=True)
self.l7 = utils.spectral_norm(nn.Linear(num_features * 16, 1))
if num_classes > 0:
self.l_y = utils.spectral_norm(
nn.Embedding(num_classes, num_features * 16))
self._initialize()
示例3: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, num_features=64, num_classes=0, activation=F.relu):
super(SNResNetProjectionDiscriminator, self).__init__()
self.num_features = num_features
self.num_classes = num_classes
self.activation = activation
self.block1 = OptimizedBlock(3, num_features)
self.block2 = Block(num_features, num_features * 2,
activation=activation, downsample=True)
self.block3 = Block(num_features * 2, num_features * 4,
activation=activation, downsample=True)
self.block4 = Block(num_features * 4, num_features * 8,
activation=activation, downsample=True)
self.block5 = Block(num_features * 8, num_features * 16,
activation=activation, downsample=True)
self.l6 = utils.spectral_norm(nn.Linear(num_features * 16, 1))
if num_classes > 0:
self.l_y = utils.spectral_norm(
nn.Embedding(num_classes, num_features * 16))
self._initialize()
示例4: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, depth=4):
super(ConvEnergy, self).__init__()
self.preprocess = nn.Conv2d(3, _next(0), 1)
self.blocks = nn.ModuleList([
spectral_norm(nn.Conv2d(_next(idx), _next(idx + 1), 3, padding=1))
for idx in range(depth)
])
self.project = [
upscale(_next(idx + 1))
for idx in range(depth)
]
self.bn = nn.ModuleList([
nn.ReLU()
for idx in range(depth)
])
self.postprocess = spectral_norm(nn.Conv2d(_next(depth), 128, 1))
self.predict = spectral_norm(nn.Linear(128, 10))
示例5: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, in_size, out_size, latent_size,
hidden_size=None, upsample=1,
normalization=tsn.AdaptiveBatchNorm,
activation=func.relu):
super(BigGANBlock, self).__init__()
if hidden_size is None:
hidden_size = in_size // 4
self.in_size = in_size
self.out_size = out_size
self.upsample = upsample
self.bn = nn.ModuleList([
normalization(in_size, latent_size),
normalization(hidden_size, latent_size),
normalization(hidden_size, latent_size),
normalization(hidden_size, latent_size)
])
self.blocks = nn.ModuleList([
spectral_norm(nn.Conv2d(in_size, hidden_size, 1)),
spectral_norm(nn.Conv2d(hidden_size, hidden_size, 3, padding=1)),
spectral_norm(nn.Conv2d(hidden_size, hidden_size, 3, padding=1)),
spectral_norm(nn.Conv2d(hidden_size, out_size, 1))
])
self.activation = activation
示例6: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, fin, fout, fhidden=None, is_bias=True):
super(GatedResnetBlock,self).__init__()
# Attributes
self.is_bias = is_bias
self.learned_shortcut = (fin != fout)
self.fin = fin
self.fout = fout
if fhidden is None:
self.fhidden = min(fin, fout)
else:
self.fhidden = fhidden
norm_layer='instance'
# Submodules
self.conv_0 = spectral_norm(nn.Conv2d(self.fin, self.fhidden, 3, stride=1, padding=1))
self.conv_1 = spectral_norm(nn.Conv2d(self.fhidden, self.fout, 3, stride=1, padding=1, bias=is_bias))
if self.learned_shortcut:
self.conv_s = spectral_norm( nn.Conv2d(self.fin, self.fout, 1, stride=1, padding=0, bias=False))
示例7: turn_on_spectral_norm
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def turn_on_spectral_norm(self):
"""
private helper for turning on the spectral normalization
:return: None (has side effect)
"""
from torch.nn.utils import spectral_norm
if self.spectral_norm_mode is not None:
assert self.spectral_norm_mode is False, \
"can't apply spectral_norm. It is already applied"
# apply the same to the remaining relevant blocks
for module in self.layers:
module.conv_1 = spectral_norm(module.conv_1)
module.conv_2 = spectral_norm(module.conv_2)
# toggle the state variable:
self.spectral_norm_mode = True
示例8: turn_off_spectral_norm
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def turn_off_spectral_norm(self):
"""
private helper for turning off the spectral normalization
:return: None (has side effect)
"""
from torch.nn.utils import remove_spectral_norm
if self.spectral_norm_mode is not None:
assert self.spectral_norm_mode is True, \
"can't remove spectral_norm. It is not applied"
# remove the applied spectral norm
for module in self.layers:
remove_spectral_norm(module.conv_1)
remove_spectral_norm(module.conv_2)
# toggle the state variable:
self.spectral_norm_mode = False
示例9: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self,in_channels,out_channels,kernel_size,padding,stride,num_classes=0,activation=nn.LeakyReLU(0.2),conv_groups=1):
"""
:param in_channels:
:param out_channels:
:param kernel_size:
:param padding:
:param stride:
:param num_classes:
:param activation:
:param conv_groups:
"""
super(StandardGeneratorBlock,self).__init__()
self.activation = activation
self.num_classes = num_classes
self.conv = spectral_norm(ConvTranspose2d(in_channels,out_channels,kernel_size=kernel_size,padding=padding,stride=stride,weight_init=Xavier_Uniform(),groups=conv_groups))
if num_classes > 0:
self.bn = ConditionalBatchNorm2d(out_channels,num_classes)
else:
self.bn = BatchNorm2d(out_channels)
示例10: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=None,
bias=True,
spectral_norm=False,
residual_init=True):
super(CustomConv2d, self).__init__()
self.residual_init = residual_init
if padding is None:
padding = int((kernel_size - 1) / 2)
self.conv = nn.Conv2d(in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
bias=bias)
if spectral_norm:
self.conv = utils.spectral_norm(self.conv)
示例11: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self):
super(Discriminator_VGG_128_SN, self).__init__()
# features
# hxw, c
# 128, 64
self.lrelu = nn.LeakyReLU(0.2, True)
self.conv0 = spectral_norm(nn.Conv2d(3, 64, 3, 1, 1))
self.conv1 = spectral_norm(nn.Conv2d(64, 64, 4, 2, 1))
# 64, 64
self.conv2 = spectral_norm(nn.Conv2d(64, 128, 3, 1, 1))
self.conv3 = spectral_norm(nn.Conv2d(128, 128, 4, 2, 1))
# 32, 128
self.conv4 = spectral_norm(nn.Conv2d(128, 256, 3, 1, 1))
self.conv5 = spectral_norm(nn.Conv2d(256, 256, 4, 2, 1))
# 16, 256
self.conv6 = spectral_norm(nn.Conv2d(256, 512, 3, 1, 1))
self.conv7 = spectral_norm(nn.Conv2d(512, 512, 4, 2, 1))
# 8, 512
self.conv8 = spectral_norm(nn.Conv2d(512, 512, 3, 1, 1))
self.conv9 = spectral_norm(nn.Conv2d(512, 512, 4, 2, 1))
# 4, 512
# classifier
self.linear0 = spectral_norm(nn.Linear(512 * 4 * 4, 100))
self.linear1 = spectral_norm(nn.Linear(100, 1))
示例12: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, size=64, z=32):
super().__init__()
self.z = z
self.preprocess = spectral_norm(nn.Conv2d(5 + 3, size, 3, padding=1))
self.noise = nn.Parameter(torch.rand(6, 1, size, 1, 1))
self.post_noise = nn.Parameter(torch.rand(3, 1, size, 1, 1))
self.bg = nn.Parameter(torch.randn(1, 64, 8, 8))
self.color = nn.Conv2d(size, 3, 1)
self.encoder = nn.ModuleList([
spectral_norm(nn.Conv2d(size, size, 3, dilation=idx + 1, padding=idx + 1))
for idx in range(6)
])
self.encoder_norm = nn.ModuleList([
nn.InstanceNorm2d(size)
for idx in range(6)
])
self.decoder = nn.ModuleList([
nn.Conv2d(2 * size, size, 3, dilation=idx + 1, padding=idx + 1)
for idx in reversed(range(6))
])
self.decoder_norm = nn.ModuleList([
AdaptiveInstanceNormPP(size, z)
for idx in reversed(range(6))
])
self.post = nn.ModuleList([
nn.Conv2d(size, size, 3, dilation=1, padding=1)
for idx in range(3)
])
self.post_norm = nn.ModuleList([
AdaptiveInstanceNormPP(size, z)
for idx in reversed(range(3))
])
示例13: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self):
super(Energy, self).__init__()
self.input = MLP(28 * 28, 128, hidden_size=128, depth=3, batch_norm=False, normalization=spectral_norm)
self.condition = MLP(10, 128, depth=3, batch_norm=False, normalization=spectral_norm)
self.combine = MLP(128, 1, hidden_size=64, depth=3, batch_norm=False, normalization=spectral_norm)
示例14: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, single, size=5, latents=64):
super(Encoder, self).__init__()
self.size = size
self.single = single
self.weight = spectral_norm(nn.Linear(128, 1))
self.combine = MLP(
128, 128, 64,
depth=3, batch_norm=False,
normalization=spectral_norm,
activation=func.leaky_relu
)
self.mean = spectral_norm(nn.Linear(128, latents))
self.logvar = spectral_norm(nn.Linear(128, latents))
示例15: __init__
# 需要导入模块: from torch.nn import utils [as 别名]
# 或者: from torch.nn.utils import spectral_norm [as 别名]
def __init__(self, latents=32):
super(SingleEncoder, self).__init__()
self.block = MLP(28 * 28, latents, hidden_size=64, depth=4, batch_norm=False, normalization=spectral_norm)