1

我刚从https://github.com/heykeetae/Self-Attention-GAN得到代码(文件是spectral.py)。部分代码在下面。我真的不明白 .data 的用途是什么,这是某个类中的方法吗?如果是,它属于哪个类?

import torch
from torch.optim.optimizer import Optimizer, required
from torch.autograd import Variable
import torch.nn.functional as F
from torch import nn
from torch import Tensor
from torch.nn import Parameter

def l2normalize(v, eps=1e-12):
    return v / (v.norm() + eps)


class SpectralNorm(nn.Module):

    def _make_params(self):
        w = getattr(self.module, self.name)

        height = w.data.shape[0]
        width = w.view(height, -1).data.shape[1]

        u = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
        v = Parameter(w.data.new(width).normal_(0, 1), requires_grad=False)
        u.data = l2normalize(u.data)
        v.data = l2normalize(v.data)
        w_bar = Parameter(w.data)
4

1 回答 1

-1

好的,所以SpectralNorm.__init__设置self.module = moduleand self.name = name(default: weight) 这是一个构造函数参数。似乎是SpectralNorm(nn.Conv2d(3, conv_dim, 4, 2, 1)))这样称呼的_ _modulenn.Conv2dnn.Module

于 2018-07-05T16:14:13.490 回答