class_docstr: Trainer API
  
  Trainer class is the very basic class for training neural network. You can composite this class to your own trainer class and delegate the train method of this class to your class.
  
  Args:    
      updater (:obj:`Updater` or list of :obj:`Updater`): Updater object.
      evaluator (:obj:`Evaluator` or list of :obj:`Evaluator`): Evaluator object.
      model_save_path (:obj:`str`): Model save path.
      max_epoch (:obj:`int`): Max epoch to train.
      iter_per_epoch (:obj:`int`, optional): Iterations per one epoch.
      callback_on_start (callable :obj:`object`, function, lambda, or list of these, optional): Callback called before the trainer.train.
      callback_on_finish (callable :obj:`object`, function, lambda, or list of these, optional): Callback called after the trainer.train.
      update_callback_on_start (callable :obj:`object`, function, lambda, or list of these, optional): Callback called before the updater.update.
      update_callback_on_finish (callable :obj:`object`, function, lambda, or list of these, optional): Callback called after the updater.update.
  
  The following example is a complete snippet to use this base trainer.
  
  Example:
  
      .. code-block:: python
  
          import nnabla as nn
          import nnabla.functions as F
          import nnabla.parametric_functions as PF
          import nnabla.solvers as S
  
          from nnabla.monitor import Monitor, MonitorSeries, MonitorTimeElapsed
  
          import numpy as np
  
          from nnabla.experimental.trainers import Trainer, Updater, Evaluator
  
          # Batch, channel, height, width
          b, c, h, w = 32, 1, 128, 128
  
          # Train Input
          tinput = nn.Variable([b, c, h, w])
          tlabel = nn.Variable([b, c, h, w])
  
          # Train Model and Loss
          tpred = <training model>.apply(persistent=True)
          tloss = F.mean(F.softmax_cross_entropy(tpred, tlabel))
  
          # Test Input
          vinput = nn.Variable([b, c, h, w])
          vlabel = nn.Variable([b, c, h, w])
  
          # Test Model and Error
          vpred = <evaluation model>.apply(persistent=True)
          vloss = F.mean(F.softmax_cross_entropy(vpred, vlabel))
          verror = F.mean(F.top_n_error(vpred.get_unlinked_variable(), vlabel))
  
          # Solver
          solver = S.Adam()
          solver.set_parameters(nn.get_parameters())
  
          # DataIterator
          tdata = <training_data_iterator>
          vdata = <validation_data_iterator>
  
          # Monitor
          monitor = Monitor(<monitor_path>)
          monitor_loss = MonitorSeries("Training loss", monitor, interval=10)
          monitor_err = MonitorSeries("Training error", monitor, interval=10)
          monitor_time = MonitorTimeElapsed("Training time", monitor, interval=100)
          monitor_verr = MonitorSeries("Valid error", monitor, interval=10)
  
          # Updater
          def tdata_feeder():
              tinput.d, tlabel.d = tdata.next()
          def update_callback_on_finish(i):
              monitor_loss.add(i, tloss.d)
              monitor_time.add(i)
          updater = Updater(solver, tloss, 
                            data_feeder=tdata_feeder, 
                            update_callback_on_finish=update_callback_on_finish)
  
          # Evaluator
          def vdata_feeder():
              vinput.d, vlabel.d = vdata.next()
          def eval_callback_on_finish(i, ve):
              monitor_verr.add(i, ve)
          evaluator = Evaluator(verror, 
                                data_feeder=vdata_feeder, 
                                val_iter=vdata.size // b, 
                                callback_on_finish=eval_callback_on_finish)
  
          # Trainer
          trainer = Trainer(updater, evaluator, <model_save_path>, 
                            max_epoch=<max_epoch>, iter_per_epoch=tdata.size // b)
          trainer.train()
class_docstr_tok: ["Trainer","API","Trainer","class","is","the","very","basic","class","for","training","neural","network",".","You","can","composite","this","class","to","your","own","trainer","class","and","delegate","the","train","method","of","this","class","to","your","class",".","Args",":","updater","(",":","obj",":","`","Updater","`","or","list","of",":","obj",":","`","Updater","`",")",":","Updater","object",".","evaluator","(",":","obj",":","`","Evaluator","`","or","list","of",":","obj",":","`","Evaluator","`",")",":","Evaluator","object",".","model_save_path","(",":","obj",":","`","str","`",")",":","Model","save","path",".","max_epoch","(",":","obj",":","`","int","`",")",":","Max","epoch","to","train",".","iter_per_epoch","(",":","obj",":","`","int","`",",","optional",")",":","Iterations","per","one","epoch",".","callback_on_start","(","callable",":","obj",":","`","object","`",",","function",",","lambda",",","or","list","of","these",",","optional",")",":","Callback","called","before","the","trainer.train",".","callback_on_finish","(","callable",":","obj",":","`","object","`",",","function",",","lambda",",","or","list","of","these",",","optional",")",":","Callback","called","after","the","trainer.train",".","update_callback_on_start","(","callable",":","obj",":","`","object","`",",","function",",","lambda",",","or","list","of","these",",","optional",")",":","Callback","called","before","the","updater.update",".","update_callback_on_finish","(","callable",":","obj",":","`","object","`",",","function",",","lambda",",","or","list","of","these",",","optional",")",":","Callback","called","after","the","updater.update",".","The","following","example","is","a","complete","snippet","to","use","this","base","trainer",".","Example",":","..","code-block",":",":","python","import","nnabla","as","nn","import","nnabla.functions","as","F","import","nnabla.parametric_functions","as","PF","import","nnabla.solvers","as","S","from","nnabla.monitor","import","Monitor",",","MonitorSeries",",","MonitorTimeElapsed","import","numpy","as","np","from","nnabla.experimental.trainers","import","Trainer",",","Updater",",","Evaluator","#","Batch",",","channel",",","height",",","width","b",",","c",",","h",",","w","=","32",",","1",",","128",",","128","#","Train","Input","tinput","=","nn.Variable","(","[","b",",","c",",","h",",","w","]",")","tlabel","=","nn.Variable","(","[","b",",","c",",","h",",","w","]",")","#","Train","Model","and","Loss","tpred","=","<","training","model",">",".apply","(","persistent=True",")","tloss","=","F.mean","(","F.softmax_cross_entropy","(","tpred",",","tlabel",")",")","#","Test","Input","vinput","=","nn.Variable","(","[","b",",","c",",","h",",","w","]",")","vlabel","=","nn.Variable","(","[","b",",","c",",","h",",","w","]",")","#","Test","Model","and","Error","vpred","=","<","evaluation","model",">",".apply","(","persistent=True",")","vloss","=","F.mean","(","F.softmax_cross_entropy","(","vpred",",","vlabel",")",")","verror","=","F.mean","(","F.top_n_error","(","vpred.get_unlinked_variable","(",")",",","vlabel",")",")","#","Solver","solver","=","S.Adam","(",")","solver.set_parameters","(","nn.get_parameters","(",")",")","#","DataIterator","tdata","=","<","training_data_iterator",">","vdata","=","<","validation_data_iterator",">","#","Monitor","monitor","=","Monitor","(","<","monitor_path",">",")","monitor_loss","=","MonitorSeries","(","``","Training","loss","''",",","monitor",",","interval=10",")","monitor_err","=","MonitorSeries","(","``","Training","error","''",",","monitor",",","interval=10",")","monitor_time","=","MonitorTimeElapsed","(","``","Training","time","''",",","monitor",",","interval=100",")","monitor_verr","=","MonitorSeries","(","``","Valid","error","''",",","monitor",",","interval=10",")","#","Updater","def","tdata_feeder","(",")",":","tinput.d",",","tlabel.d","=","tdata.next","(",")","def","update_callback_on_finish","(","i",")",":","monitor_loss.add","(","i",",","tloss.d",")","monitor_time.add","(","i",")","updater","=","Updater","(","solver",",","tloss",",","data_feeder=tdata_feeder",",","update_callback_on_finish=update_callback_on_finish",")","#","Evaluator","def","vdata_feeder","(",")",":","vinput.d",",","vlabel.d","=","vdata.next","(",")","def","eval_callback_on_finish","(","i",",","ve",")",":","monitor_verr.add","(","i",",","ve",")","evaluator","=","Evaluator","(","verror",",","data_feeder=vdata_feeder",",","val_iter=vdata.size","\/\/","b",",","callback_on_finish=eval_callback_on_finish",")","#","Trainer","trainer","=","Trainer","(","updater",",","evaluator",",","<","model_save_path",">",",","max_epoch=","<","max_epoch",">",",","iter_per_epoch=tdata.size","\/\/","b",")","trainer.train","(",")"]
code: def train(self):
      # On-start callback
      for callback in self.callback_on_start:
          callback()
  
      # Training loop
      for e in range(self.max_epoch):
          for j in range(self.iter_per_epoch):
              i = e * self.iter_per_epoch + j
              # On-start callback
              for callback in self.update_callback_on_start:
                  callback(i)
              # Update
              for updater in self.updater:
                  updater.update(i)
              # On-finish callback
              for callback in self.update_callback_on_finish:
                  callback(i)
  
          # Save parameters
          if self.model_save_path is not None:
              nn.save_parameters(
                  os.path.join(
                      self.model_save_path, "params_{:06}.h5".format(i)
                  )
              )
  
          # Evaluate
          for evaluator in self.evaluator:
              evaluator.evaluate(i)
  
      # On-finish callback
      for callback in self.callback_on_finish:
          callback()
  
      # Save parameters
      if self.model_save_path is not None:
          nn.save_parameters(
              os.path.join(
                  self.model_save_path, "params_{:06}.h5".format(i)
              )
          )
  
      # Evaluate
      for evaluator in self.evaluator:
          evaluator.evaluate(i)
  
file_path: nnabla/python/src/nnabla/experimental/trainers/trainer.py
imports: import os
  import nnabla
  import nnabla.functions
  from nnabla.experimental.trainers import Evaluator
  from nnabla.experimental.trainers import Updater
  from nnabla.monitor import Monitor, MonitorSeries, MonitorTimeElapsed
  from six.moves import range
label_desc: Use image node_id 3 for calling the Trainer obj's underlying member method code with example usage: obj.train() without return types