ravens/agents/conv_mlp.py
def train(self, dataset, num_iter, writer, validation_dataset):
        """Train on dataset for a specific number of iterations."""
        VALIDATION_RATE = 100

        @tf.function
        def pick_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss

        @tf.function
        def pick_valid_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            return loss

        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act = self.get_data_batch(dataset)

            # Compute train loss
            loss0 = self.regression_model.train_pick(batch_obs, batch_act, pick_train_step)
            with writer.as_default():
                tf.summary.scalar('pick_loss', self.regression_model.metric.result(),
                    step=self.total_iter+i)

            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} Iter time:', time.time() - start)

            if (self.total_iter + i) % VALIDATION_RATE == 0:
                print("Validating!")
                tf.keras.backend.set_learning_phase(0)
                batch_obs, batch_act = self.get_data_batch(validation_dataset, augment=False)

                # Compute valid loss
                loss0 = self.regression_model.train_pick(batch_obs, batch_act, pick_valid_step, validate=True)
                with writer.as_default():
                    tf.summary.scalar('validation_pick_loss', self.regression_model.val_metric.result(),
                        step=self.total_iter+i)

                tf.keras.backend.set_learning_phase(1)

        self.total_iter += num_iter
        self.save()
Train on dataset for a specific number of iterations.
Train on dataset for a specific number of iterations.
Train on dataset for a specific number of iterations .
def train(self, dataset, num_iter, writer, validation_dataset):
        VALIDATION_RATE = 100
        @tf.function
        def pick_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss
        @tf.function
        def pick_valid_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            return loss
        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act = self.get_data_batch(dataset)
            loss0 = self.regression_model.train_pick(batch_obs, batch_act, pick_train_step)
            with writer.as_default():
                tf.summary.scalar('pick_loss', self.regression_model.metric.result(),
                    step=self.total_iter+i)
            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} Iter time:', time.time() - start)
            if (self.total_iter + i) % VALIDATION_RATE == 0:
                print("Validating!")
                tf.keras.backend.set_learning_phase(0)
                batch_obs, batch_act = self.get_data_batch(validation_dataset, augment=False)
                loss0 = self.regression_model.train_pick(batch_obs, batch_act, pick_valid_step, validate=True)
                with writer.as_default():
                    tf.summary.scalar('validation_pick_loss', self.regression_model.val_metric.result(),
                        step=self.total_iter+i)
                tf.keras.backend.set_learning_phase(1)
        self.total_iter += num_iter
        self.save()
def train ( self , dataset , num_iter , writer , validation_dataset ) : VALIDATION_RATE = 100 @ tf . function def pick_train_step ( model , optim , in_tensor , yxtheta , loss_criterion ) : with tf . GradientTape ( ) as tape : output = model ( in_tensor ) loss = loss_criterion ( yxtheta , output ) grad = tape . gradient ( loss , model . trainable_variables ) optim . apply_gradients ( zip ( grad , model . trainable_variables ) ) return loss @ tf . function def pick_valid_step ( model , optim , in_tensor , yxtheta , loss_criterion ) : with tf . GradientTape ( ) as tape : output = model ( in_tensor ) loss = loss_criterion ( yxtheta , output ) return loss for i in range ( num_iter ) : start = time . time ( ) batch_obs , batch_act = self . get_data_batch ( dataset ) loss0 = self . regression_model . train_pick ( batch_obs , batch_act , pick_train_step ) with writer . as_default ( ) : tf . summary . scalar ( 'pick_loss' , self . regression_model . metric . result ( ) , step = self . total_iter + i ) print ( f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} Iter time:' , time . time ( ) - start ) if ( self . total_iter + i ) % VALIDATION_RATE == 0 : print ( "Validating!" ) tf . keras . backend . set_learning_phase ( 0 ) batch_obs , batch_act = self . get_data_batch ( validation_dataset , augment = False ) loss0 = self . regression_model . train_pick ( batch_obs , batch_act , pick_valid_step , validate = True ) with writer . as_default ( ) : tf . summary . scalar ( 'validation_pick_loss' , self . regression_model . val_metric . result ( ) , step = self . total_iter + i ) tf . keras . backend . set_learning_phase ( 1 ) self . total_iter += num_iter self . save ( )
Train on dataset for a specific number of iterations.
Train on dataset for a specific number of iterations .
"""Train on dataset for a specific number of iterations.""" # Compute train loss # Compute valid loss
self
dataset
num_iter
writer
validation_dataset