【论文复现】UGATIT 关于spectral_norm:'Conv2D' object has no attribute 'weight'
Created by: Ryan906k9
import paddle.fluid.dygraph.nn as nn
class Spectralnorm(nn.Layer):
def __init__(self,
layer,
dim=0,
power_iters=1,
eps=1e-12,
dtype='float32'):
super(Spectralnorm, self).__init__()
self.spectral_norm = nn.SpectralNorm(layer.weight.shape, dim, power_iters, eps, dtype)
self.dim = dim
self.power_iters = power_iters
self.eps = eps
self.layer = layer
weight = layer._parameters['weight']
del layer._parameters['weight']
self.weight_orig = self.create_parameter(weight.shape, dtype=weight.dtype)
self.weight_orig.set_value(weight)
def forward(self, x):
weight = self.spectral_norm(self.weight_orig)
self.layer.weight = weight
out = self.layer(x)
return out
尝试一下这样的写法把
Originally posted by @LielinJiang in https://github.com/PaddlePaddle/Paddle/issues/26229#issuecomment-674106061
Traceback (most recent call last): File "main.py", line 94, in main() File "main.py", line 86, in main gan.train() File "/home/aistudio/UGATIT.py", line 278, in train real_GA_logit, real_GA_cam_logit, _ = self.disGA(real_A) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 460, in call outputs = self.forward(*inputs, **kwargs) File "/home/aistudio/networks.py", line 560, in forward x = self.conv1(x) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 460, in call outputs = self.forward(*inputs, **kwargs) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py", line 230, in forward out = core.ops.conv2d(input, self.weight, *attrs) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 532, in getattr return object.getattribute(self, name) AttributeError: 'Conv2D' object has no attribute 'weight'