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

        As with the gt_state, need a special case to handle the num_iter=0 case.
        """
        if self.pick_model is None:
            self.init_model(dataset)

        if self.USE_MDN:
            loss_criterion = mdn_utils.mdn_loss
        else:
            loss_criterion = tf.keras.losses.MeanSquaredError()

        @tf.function
        def train_step(pick_model, place_model, batch_obs, batch_act, loss_criterion):
            with tf.GradientTape() as tape:
                prediction = pick_model(batch_obs)
                loss0 = loss_criterion(batch_act[:,0:3], prediction)
                grad = tape.gradient(loss0, pick_model.trainable_variables)
                self.pick_optim.apply_gradients(zip(grad, pick_model.trainable_variables))
            with tf.GradientTape() as tape:
                #batch_obs = tf.concat((batch_obs, batch_act[:,0:3] + tf.random.normal(shape=batch_act[:,0:3].shape, stddev=0.001)), axis=1)
                batch_obs = tf.concat((batch_obs, batch_act[:,0:3]), axis=1)
                prediction = place_model(batch_obs)
                loss1 = loss_criterion(batch_act[:,3:], prediction)
                grad = tape.gradient(loss1, place_model.trainable_variables)
                self.place_optim.apply_gradients(zip(grad, place_model.trainable_variables))
            return loss0 + loss1

        # Need this case due to some quirks with saving this type of model. No gradient tupdates.
        if num_iter == 0:
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            print('Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.')
            prediction = self.pick_model(batch_obs)
            batch_obs = tf.concat((batch_obs, batch_act[:,0:3]), axis=1)
            prediction = self.place_model(batch_obs)

        print_rate = 50
        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)

            # Forward through model, compute training loss, update weights.
            self.metric.reset_states()
            loss = train_step(self.pick_model, self.place_model, batch_obs, batch_act, loss_criterion)
            self.metric(loss)
            with writer.as_default():
                tf.summary.scalar('gt_state_loss', self.metric.result(), step=self.total_iter+i)
            if i % print_rate == 0:
                loss = np.float32(loss)
                print(f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:', time.time() - start)

        self.total_iter += num_iter
        self.save()
Train on dataset for a specific number of iterations.

        As with the gt_state, need a special case to handle the num_iter=0 case.
        
Train on dataset for a specific number of iterations.
As with the gt_state, need a special case to handle the num_iter=0 case.
Train on dataset for a specific number of iterations . As with the gt_state need a special case to handle the num_iter = 0 case .
def train(self, dataset, num_iter, writer, validation_dataset=None):
        if self.pick_model is None:
            self.init_model(dataset)
        if self.USE_MDN:
            loss_criterion = mdn_utils.mdn_loss
        else:
            loss_criterion = tf.keras.losses.MeanSquaredError()
        @tf.function
        def train_step(pick_model, place_model, batch_obs, batch_act, loss_criterion):
            with tf.GradientTape() as tape:
                prediction = pick_model(batch_obs)
                loss0 = loss_criterion(batch_act[:,0:3], prediction)
                grad = tape.gradient(loss0, pick_model.trainable_variables)
                self.pick_optim.apply_gradients(zip(grad, pick_model.trainable_variables))
            with tf.GradientTape() as tape:
                batch_obs = tf.concat((batch_obs, batch_act[:,0:3]), axis=1)
                prediction = place_model(batch_obs)
                loss1 = loss_criterion(batch_act[:,3:], prediction)
                grad = tape.gradient(loss1, place_model.trainable_variables)
                self.place_optim.apply_gradients(zip(grad, place_model.trainable_variables))
            return loss0 + loss1
        if num_iter == 0:
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            print('Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.')
            prediction = self.pick_model(batch_obs)
            batch_obs = tf.concat((batch_obs, batch_act[:,0:3]), axis=1)
            prediction = self.place_model(batch_obs)
        print_rate = 50
        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            self.metric.reset_states()
            loss = train_step(self.pick_model, self.place_model, batch_obs, batch_act, loss_criterion)
            self.metric(loss)
            with writer.as_default():
                tf.summary.scalar('gt_state_loss', self.metric.result(), step=self.total_iter+i)
            if i % print_rate == 0:
                loss = np.float32(loss)
                print(f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:', time.time() - start)
        self.total_iter += num_iter
        self.save()
def train ( self , dataset , num_iter , writer , validation_dataset = None ) : if self . pick_model is None : self . init_model ( dataset ) if self . USE_MDN : loss_criterion = mdn_utils . mdn_loss else : loss_criterion = tf . keras . losses . MeanSquaredError ( ) @ tf . function def train_step ( pick_model , place_model , batch_obs , batch_act , loss_criterion ) : with tf . GradientTape ( ) as tape : prediction = pick_model ( batch_obs ) loss0 = loss_criterion ( batch_act [ : , 0 : 3 ] , prediction ) grad = tape . gradient ( loss0 , pick_model . trainable_variables ) self . pick_optim . apply_gradients ( zip ( grad , pick_model . trainable_variables ) ) with tf . GradientTape ( ) as tape : batch_obs = tf . concat ( ( batch_obs , batch_act [ : , 0 : 3 ] ) , axis = 1 ) prediction = place_model ( batch_obs ) loss1 = loss_criterion ( batch_act [ : , 3 : ] , prediction ) grad = tape . gradient ( loss1 , place_model . trainable_variables ) self . place_optim . apply_gradients ( zip ( grad , place_model . trainable_variables ) ) return loss0 + loss1 if num_iter == 0 : batch_obs , batch_act , obs , act , info = self . get_data_batch ( dataset ) print ( 'Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.' ) prediction = self . pick_model ( batch_obs ) batch_obs = tf . concat ( ( batch_obs , batch_act [ : , 0 : 3 ] ) , axis = 1 ) prediction = self . place_model ( batch_obs ) print_rate = 50 for i in range ( num_iter ) : start = time . time ( ) batch_obs , batch_act , obs , act , info = self . get_data_batch ( dataset ) self . metric . reset_states ( ) loss = train_step ( self . pick_model , self . place_model , batch_obs , batch_act , loss_criterion ) self . metric ( loss ) with writer . as_default ( ) : tf . summary . scalar ( 'gt_state_loss' , self . metric . result ( ) , step = self . total_iter + i ) if i % print_rate == 0 : loss = np . float32 ( loss ) print ( f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:' , time . time ( ) - start ) 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.

        As with the gt_state, need a special case to handle the num_iter=0 case.
        """ #batch_obs = tf.concat((batch_obs, batch_act[:,0:3] + tf.random.normal(shape=batch_act[:,0:3].shape, stddev=0.001)), axis=1) # Need this case due to some quirks with saving this type of model. No gradient tupdates. # Forward through model, compute training loss, update weights.
self
dataset
num_iter
writer
validation_dataset