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

Public Member Functions

 __init__ (self, in_features, out_features, 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

Linear layer with spectral norm

Definition at line 347 of file ieagan.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
in_features,
out_features,
bias = True,
num_svs = 1,
num_itrs = 1,
eps = 1e-12 )

Constructor.

Definition at line 353 of file ieagan.py.

361 ):
362 nn.Linear.__init__(self, in_features, out_features, bias)
363 SN.__init__(self, num_svs, num_itrs, out_features, eps=eps)
364

Member Function Documentation

◆ forward()

forward ( self,
x )

forward

Definition at line 366 of file ieagan.py.

366 def forward(self, x):
367 # \cond false positive doxygen warning
368 return F.linear(x, self.W_(), self.bias)
369 # \endcond
370
371

◆ 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: