Belle II Software development
myBN Class Reference
Inheritance diagram for myBN:

Public Member Functions

 __init__ (self, num_channels, eps=1e-5, momentum=0.1)
 Constructor.
 
 reset_stats (self)
 reset standing stats
 
 forward (self, x, gain, bias)
 forward
 

Public Attributes

 momentum = momentum
 momentum for updating running stats
 
 eps = eps
 epsilon to avoid dividing by 0
 
bool accumulate_standing = False
 Accumulate running means and vars.
 
bool training
 Training mode flag (inherited from nn.Module).
 

Detailed Description

My batchnorm, supports standing stats

Definition at line 419 of file ieagan.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
num_channels,
eps = 1e-5,
momentum = 0.1 )

Constructor.

Definition at line 425 of file ieagan.py.

425 def __init__(self, num_channels, eps=1e-5, momentum=0.1):
426 super(myBN, self).__init__()
427
428 self.momentum = momentum
429
430 self.eps = eps
431 # Momentum
432 self.momentum = momentum
433 # Register buffers
434 self.register_buffer("stored_mean", torch.zeros(num_channels))
435 self.register_buffer("stored_var", torch.ones(num_channels))
436 self.register_buffer("accumulation_counter", torch.zeros(1))
437
438 self.accumulate_standing = False
439
440 self.training: bool
441

Member Function Documentation

◆ forward()

forward ( self,
x,
gain,
bias )

forward

Definition at line 449 of file ieagan.py.

449 def forward(self, x, gain, bias):
450 if self.training:
451 out, mean, var = manual_bn(
452 x, gain, bias, return_mean_var=True, eps=self.eps
453 )
454 # If accumulating standing stats, increment them
455 if self.accumulate_standing:
456 self.stored_mean[:] = self.stored_mean + mean.data
457 self.stored_var[:] = self.stored_var + var.data
458 self.accumulation_counter += 1.0
459 # If not accumulating standing stats, take running averages
460 else:
461 self.stored_mean[:] = (
462 self.stored_mean * (1 - self.momentum) + mean * self.momentum
463 )
464 self.stored_var[:] = (
465 self.stored_var * (1 - self.momentum) + var * self.momentum
466 )
467 return out
468 # If not in training mode, use the stored statistics
469 else:
470 mean = self.stored_mean.view(1, -1, 1, 1)
471 var = self.stored_var.view(1, -1, 1, 1)
472 # If using standing stats, divide them by the accumulation counter
473 if self.accumulate_standing:
474 mean = mean / self.accumulation_counter
475 var = var / self.accumulation_counter
476 return fused_bn(x, mean, var, gain, bias, self.eps)
477
478

◆ reset_stats()

reset_stats ( self)

reset standing stats

Definition at line 443 of file ieagan.py.

443 def reset_stats(self):
444 self.stored_mean[:] = 0
445 self.stored_var[:] = 0
446 self.accumulation_counter[:] = 0
447

Member Data Documentation

◆ accumulate_standing

bool accumulate_standing = False

Accumulate running means and vars.

Definition at line 438 of file ieagan.py.

◆ eps

eps = eps

epsilon to avoid dividing by 0

Definition at line 430 of file ieagan.py.

◆ momentum

momentum = momentum

momentum for updating running stats

Definition at line 428 of file ieagan.py.

◆ training

bool training

Training mode flag (inherited from nn.Module).

True if the module is in training mode.

Definition at line 440 of file ieagan.py.


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