Beispiel #1
0
def model():
    tf.reset_default_graph()
    m = Model()
    with m.sess.graph.as_default():
        ema_op, load, restore = _add_ema(m, 0.99)
        with tf.control_dependencies([ema_op]):
            train_op = m.opt.minimize(m.loss)
    yield m, train_op, load, restore
    m.sess.close()
Beispiel #2
0
def model():
    tf.reset_default_graph()
    m = Model()
    with m.sess.graph.as_default():
       ema_op, load, restore = _add_ema(m, 0.99)
       with tf.control_dependencies([ema_op]):
            train_op = m.opt.minimize(m.loss)
    yield m, train_op, load, restore
    m.sess.close()
Beispiel #3
0
    def __init__(self, model_params, **kwargs):
        """Create a Trainer, and give it the parameters needed to instantiate the model

        :param model_params: The model parameters
        :param kwargs: See below

        :Keyword Arguments:

          * *nsteps* (`int`) -- If we should report every n-steps, this should be passed
          * *ema_decay* (`float`) -- If we are doing an exponential moving average, what decay to us4e
          * *clip* (`int`) -- If we are doing gradient clipping, what value to use
          * *optim* (`str`) -- The name of the optimizer we are using
          * *lr* (`float`) -- The learning rate we are using
          * *mom* (`float`) -- If we are using SGD, what value to use for momentum
          * *beta1* (`float`) -- Adam-specific hyper-param, defaults to `0.9`
          * *beta2* (`float`) -- Adam-specific hyper-param, defaults to `0.999`
          * *epsilon* (`float`) -- Adam-specific hyper-param, defaults to `1e-8

        """
        super().__init__()
        if type(model_params) is dict:
            self.model = create_model_for('classify', **model_params)
        else:
            self.model = model_params
        self.sess = self.model.sess
        self.loss = self.model.create_loss()
        self.test_loss = self.model.create_test_loss()
        self.global_step, train_op = optimizer(
            self.loss,
            colocate_gradients_with_ops=True,
            variables=self.model.trainable_variables,
            **kwargs)
        self.nsteps = kwargs.get('nsteps', six.MAXSIZE)
        decay = kwargs.get('ema_decay', None)
        if decay is not None:
            self.ema = True
            ema_op, self.ema_load, self.ema_restore = _add_ema(
                self.model, float(decay))
            with tf.compat.v1.control_dependencies([ema_op]):
                self.train_op = tf.identity(train_op)
        else:
            self.ema = False
            self.train_op = train_op

        tables = tf.compat.v1.tables_initializer()
        self.model.sess.run(tables)
        self.model.sess.run(tf.compat.v1.global_variables_initializer())
        self.model.set_saver(tf.compat.v1.train.Saver())
        checkpoint = kwargs.get('checkpoint')
        if checkpoint is not None:
            skip_blocks = kwargs.get('blocks_to_skip', ['OptimizeLoss'])
            reload_checkpoint(self.model.sess, checkpoint, skip_blocks)
Beispiel #4
0
 def __init__(self, model, **kwargs):
     super(ClassifyTrainerTf, self).__init__()
     self.sess = model.sess
     self.loss = model.create_loss()
     self.test_loss = model.create_test_loss()
     self.model = model
     self.global_step, train_op = optimizer(self.loss, colocate_gradients_with_ops=True, **kwargs)
     self.nsteps = kwargs.get('nsteps', six.MAXSIZE)
     decay = kwargs.get('ema_decay', None)
     if decay is not None:
         self.ema = True
         ema_op, self.ema_load, self.ema_restore = _add_ema(model, float(decay))
         with tf.control_dependencies([ema_op]):
             self.train_op = tf.identity(train_op)
     else:
         self.ema = False
         self.train_op = train_op
Beispiel #5
0
 def __init__(self, model, **kwargs):
     super(ClassifyTrainerTf, self).__init__()
     self.sess = model.sess
     self.loss = model.create_loss()
     self.test_loss = model.create_test_loss()
     self.model = model
     self.global_step, train_op = optimizer(self.loss, colocate_gradients_with_ops=True, **kwargs)
     self.nsteps = kwargs.get('nsteps', six.MAXSIZE)
     decay = kwargs.get('ema_decay', None)
     if decay is not None:
         self.ema = True
         ema_op, self.ema_load, self.ema_restore = _add_ema(model, float(decay))
         with tf.control_dependencies([ema_op]):
             self.train_op = tf.identity(train_op)
     else:
         self.ema = False
         self.train_op = train_op