Public Member Functions | Public Attributes | List of all members
my_callbacks.ModelCheckpointDetached Class Reference
Inheritance diagram for my_callbacks.ModelCheckpointDetached:

Public Member Functions

def __init__ (self, filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
 
def on_epoch_end (self, epoch, logs=None)
 

Public Attributes

 monitor
 
 verbose
 
 filepath
 
 save_best_only
 
 save_weights_only
 
 period
 
 epochs_since_last_save
 
 monitor_op
 
 best
 

Detailed Description

Save detached from multi-GPU encapsulation model
(very small) modification from https://github.com/fchollet/keras/blob/master/keras/callbacks.py#L331

`filepath` can contain named formatting options,
which will be filled the value of `epoch` and
keys in `logs` (passed in `on_epoch_end`).

For example: if `filepath` is `weights.{epoch:02d}-{val_loss:.2f}.hdf5`,
then the model checkpoints will be saved with the epoch number and
the validation loss in the filename.

# Arguments
    filepath: string, path to save the model file.
    monitor: quantity to monitor.
    verbose: verbosity mode, 0 or 1.
    save_best_only: if `save_best_only=True`,
        the latest best model according to
        the quantity monitored will not be overwritten.
    mode: one of {auto, min, max}.
        If `save_best_only=True`, the decision
        to overwrite the current save file is made
        based on either the maximization or the
        minimization of the monitored quantity. For `val_acc`,
        this should be `max`, for `val_loss` this should
        be `min`, etc. In `auto` mode, the direction is
        automatically inferred from the name of the monitored quantity.
    save_weights_only: if True, then only the model's weights will be
        saved (`model.save_weights(filepath)`), else the full model
        is saved (`model.save(filepath)`).
    period: Interval (number of epochs) between checkpoints.

Definition at line 92 of file my_callbacks.py.

Constructor & Destructor Documentation

def my_callbacks.ModelCheckpointDetached.__init__ (   self,
  filepath,
  monitor = 'val_loss',
  verbose = 0,
  save_best_only = False,
  save_weights_only = False,
  mode = 'auto',
  period = 1 
)

Definition at line 128 of file my_callbacks.py.

128  mode='auto', period=1):
129  super(ModelCheckpointDetached, self).__init__()
130  self.monitor = monitor
131  self.verbose = verbose
132  self.filepath = filepath
133  self.save_best_only = save_best_only
134  self.save_weights_only = save_weights_only
135  self.period = period
137 
138  if mode not in ['auto', 'min', 'max']:
139  warnings.warn('ModelCheckpoint mode %s is unknown, '
140  'fallback to auto mode.' % mode, RuntimeWarning)
141  mode = 'auto'
142 
143  if mode == 'min':
144  self.monitor_op = np.less
145  self.best = np.Inf
146  elif mode == 'max':
147  self.monitor_op = np.greater
148  self.best = -np.Inf
149  else:
150  if 'acc' in self.monitor or self.monitor.startswith('fmeasure'):
151  self.monitor_op = np.greater
152  self.best = -np.Inf
153  else:
154  self.monitor_op = np.less
155  self.best = np.Inf
156 
def __init__(self, filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)

Member Function Documentation

def my_callbacks.ModelCheckpointDetached.on_epoch_end (   self,
  epoch,
  logs = None 
)

Definition at line 157 of file my_callbacks.py.

157  def on_epoch_end(self, epoch, logs=None):
158  logs = logs or {}
159  self.epochs_since_last_save += 1
160  if self.epochs_since_last_save >= self.period:
161  self.epochs_since_last_save = 0
162  filepath = self.filepath.format(epoch=epoch, **logs)
163  if self.save_best_only:
164  current = logs.get(self.monitor)
165  if current is None:
166  warnings.warn('Can save best model only with %s available, '
167  'skipping.' % self.monitor, RuntimeWarning)
168  else:
169  if self.monitor_op(current, self.best):
170  if self.verbose > 0:
171  print('Epoch %05d: %s improved from %0.5f to %0.5f,'
172  ' saving model to %s'
173  % (epoch, self.monitor, self.best,
174  current, filepath))
175  self.best = current
176  if self.save_weights_only:
177  detachmodel(self.model).save_weights(filepath, overwrite=True)
178  else:
179  detachmodel(self.model).save(filepath, overwrite=True)
180  else:
181  if self.verbose > 0:
182  print('Epoch %05d: %s did not improve' %
183  (epoch, self.monitor))
184  else:
185  if self.verbose > 0:
186  print('Epoch %05d: saving model to %s' % (epoch, filepath))
187  if self.save_weights_only:
188  detachmodel(self.model).save_weights(filepath, overwrite=True)
189  else:
190  detachmodel(self.model).save(filepath, overwrite=True)
191 
def on_epoch_end(self, epoch, logs=None)
def save(obj, fname)
Definition: root.py:19
def detachmodel(m)
Definition: my_callbacks.py:78

Member Data Documentation

my_callbacks.ModelCheckpointDetached.best

Definition at line 145 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.epochs_since_last_save

Definition at line 136 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.filepath

Definition at line 132 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.monitor

Definition at line 130 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.monitor_op

Definition at line 144 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.period

Definition at line 135 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.save_best_only

Definition at line 133 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.save_weights_only

Definition at line 134 of file my_callbacks.py.

my_callbacks.ModelCheckpointDetached.verbose

Definition at line 131 of file my_callbacks.py.


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