<s> import numpy as np <EOL> class ValidLossTracker ( object ) : <EOL> def __init__ ( self , loss_block , logger ) : <EOL> self . loss_block = loss_block <EOL> self . logger = logger <EOL> self . observers = [ ] <EOL> self . losses = [ ] <EOL> self . context = self . loss_block . context <EOL> def add_observer ( self , observer ) : <EOL> self . observers . append ( observer ) <EOL> def accumulate_loss ( self ) : <EOL> loss = self . loss_block . loss <EOL> if type ( loss ) is list : <EOL> self . losses . extend ( loss ) <EOL> else : <EOL> self . losses . append ( loss ) <EOL> def notify_about_fprop ( self ) : <EOL> self . loss_block . calculate_loss ( self . context ) <EOL> self . context . add_callback ( self . accumulate_loss ) <EOL> def notify ( self , iteration ) : <EOL> self . context . add_callback ( self . _notify , iteration ) <EOL> def _notify ( self , iteration ) : <EOL> loss = np . mean ( self . losses ) <EOL> self . losses = [ ] <EOL> self . logger . info ( '<STR_LIT>' . <EOL> format ( iteration , loss ) ) <EOL> for observer in self . observers : <EOL> observer . notify ( loss ) </s>
