Belle II Software development
SNConv2d Class Reference
Inheritance diagram for SNConv2d:
SN

Public Member Functions

 __init__ (self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, num_svs=1, num_itrs=1, eps=1e-12)
 Constructor.
 
 forward (self, x)
 forward
 
 u (self)
 
 sv (self)
 
 W_ (self)
 

Public Attributes

 num_itrs = num_itrs
 Number of power iterations per step.
 
 num_svs = num_svs
 Number of singular values.
 
 transpose = transpose
 Transposed?
 
 eps = eps
 Epsilon value for avoiding divide-by-0.
 
bool training
 Training mode flag (inherited from nn.Module).
 

Detailed Description

2D Conv layer with spectral norm

Definition at line 299 of file ieagan.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
in_channels,
out_channels,
kernel_size,
stride = 1,
padding = 0,
dilation = 1,
groups = 1,
bias = True,
num_svs = 1,
num_itrs = 1,
eps = 1e-12 )

Constructor.

Definition at line 305 of file ieagan.py.

318 ):
319 nn.Conv2d.__init__(
320 self,
321 in_channels,
322 out_channels,
323 kernel_size,
324 stride,
325 padding,
326 dilation,
327 groups,
328 bias,
329 )
330 SN.__init__(self, num_svs, num_itrs, out_channels, eps=eps)
331

Member Function Documentation

◆ forward()

forward ( self,
x )

forward

Definition at line 333 of file ieagan.py.

333 def forward(self, x):
334 return F.conv2d(
335 x,
336 self.W_(),
337 # \cond false positive doxygen warning
338 self.bias,
339 self.stride,
340 self.padding,
341 self.dilation,
342 self.groups,
343 # \endcond
344 )
345
346

◆ sv()

sv ( self)
inherited
Singular values
note that these buffers are just for logging and are not used in training.

Definition at line 271 of file ieagan.py.

271 def sv(self):
272 """
273 Singular values
274 note that these buffers are just for logging and are not used in training.
275 """
276 return [getattr(self, f"sv{i:d}") for i in range(self.num_svs)]
277

◆ u()

u ( self)
inherited
Singular vectors (u side)

Definition at line 264 of file ieagan.py.

264 def u(self):
265 """
266 Singular vectors (u side)
267 """
268 return [getattr(self, f"u{i:d}") for i in range(self.num_svs)]
269

◆ W_()

W_ ( self)
inherited
Compute the spectrally-normalized weight

Definition at line 278 of file ieagan.py.

278 def W_(self):
279 """
280 Compute the spectrally-normalized weight
281 """
282 W_mat = self.weight.view(self.weight.size(0), -1)
283 if self.transpose:
284 W_mat = W_mat.t()
285 # Apply num_itrs power iterations
286 for _ in range(self.num_itrs):
287 svs, _, _ = power_iteration(
288 W_mat, self.u, update=self.training, eps=self.eps
289 )
290 # Update the svs
291 if self.training:
292 # Make sure to do this in a no_grad() context or you'll get memory leaks! # noqa
293 with torch.no_grad():
294 for i, sv in enumerate(svs):
295 self.sv[i][:] = sv
296 return self.weight / svs[0]
297
298

Member Data Documentation

◆ eps

eps = eps
inherited

Epsilon value for avoiding divide-by-0.

Definition at line 255 of file ieagan.py.

◆ num_itrs

num_itrs = num_itrs
inherited

Number of power iterations per step.

Definition at line 249 of file ieagan.py.

◆ num_svs

num_svs = num_svs
inherited

Number of singular values.

Definition at line 251 of file ieagan.py.

◆ training

bool training
inherited

Training mode flag (inherited from nn.Module).

True if the module is in training mode.

Definition at line 261 of file ieagan.py.

◆ transpose

transpose = transpose
inherited

Transposed?

Definition at line 253 of file ieagan.py.


The documentation for this class was generated from the following file: