Beispiel #1
0
    def loss(self, inputs, model_output, log_error_arr=False):
        losses = AttrDict()
        
        # Length prediction loss
        if self._hp.regress_length:
            losses.update(self.length_pred.loss(inputs, model_output))
        
        # Dense Reconstruction loss
        losses.update(self.dense_rec.loss(inputs, model_output.dense_rec, log_error_arr))

        # Inverse Model loss
        if self._hp.attach_inv_mdl:
            losses.update(self.inv_mdl.loss(inputs, model_output, add_total=False))

        # Cost model loss
        if self._hp.attach_cost_mdl and self._hp.run_cost_mdl:
            losses.update(self.cost_mdl.loss(inputs, model_output))

        # State regressor cost
        if self._hp.attach_state_regressor:
            reg_len = model_output.regressed_state.shape[1]
            losses.state_regression = L2Loss(1.0)(model_output.regressed_state, inputs.demo_seq_states[:, :reg_len],
                                                  weights=inputs.pad_mask[:, :reg_len][:, :, None])

        # Negative Log-likelihood (upper bound)
        if 'dense_img_rec' in losses and 'kl' in losses:
            losses.nll = AttrDict(value=losses.dense_img_rec.value + 1.0 * losses.kl.value, weight=0.0)

        return losses
Beispiel #2
0
    def loss(self, inputs, model_output):
        losses = super().loss(inputs, model_output)
        tree = model_output.tree

        if self._hp.supervise_fraction_weight > 0.0:
            key_frac = ar2ten(
                self.criterion.get_index(inputs.demo_seq),
                self._hp.device).float() / (self._hp.max_seq_len - 1)

            weights = 1.0
            if self._hp.supervise_fraction == 'top_index':
                estimates = tree.bf.fraction[:, 0]
                targets = key_frac.float()
            elif self._hp.supervise_fraction == 'balanced':
                estimates = tree.bf.fraction
                targets = torch.ones_like(estimates) * 0.5
            elif self._hp.supervise_fraction == 'index+balanced':
                estimates = tree.bf.fraction
                targets = torch.cat([
                    key_frac.float()[:, None],
                    torch.ones_like(estimates[:, 1:]) * 0.5
                ], 1)

                weights = targets.new_ones([1, targets.shape[1]])
                weights[:, 0] = self._hp.max_seq_len

            losses.supervise_fraction = L2Loss(
                self._hp.supervise_fraction_weight)(estimates,
                                                    targets,
                                                    weights=weights)

        return losses
Beispiel #3
0
    def loss(self, inputs, outputs, add_total=True):
        losses = AttrDict()

        # subgoal reconstruction loss
        n_action_output = outputs.actions.shape[1]
        loss_weights = broadcast_final(
            outputs.pad_mask[:, :n_action_output],
            inputs.actions) if 'pad_mask' in outputs else 1
        losses.action_reconst = L2Loss(self._hp.action_rec_weight)(
            outputs.actions,
            outputs.action_targets[:, :n_action_output],
            weights=loss_weights)
        if self._hp.pred_states:
            losses.state_reconst = L2Loss(self._hp.state_rec_weight)(
                outputs.states, outputs.state_targets)

        return losses
Beispiel #4
0
    def loss(self, inputs, model_output):
        losses = AttrDict()

        # action prediction loss
        n_actions = model_output.actions.shape[1]
        losses.action_reconst = L2Loss(1.0)(model_output.actions, inputs.actions[:, :n_actions],
                                            weights=broadcast_final(inputs.pad_mask[:, :n_actions], inputs.actions))

        # compute total loss
        #total_loss = torch.stack([loss[1].value * loss[1].weight for loss in losses.items()]).sum()
        #losses.total = AttrDict(value=total_loss)
        # losses.total = total_loss*torch.tensor(np.nan)   # for checking if backprop works
        return losses
Beispiel #5
0
 def loss(self, inputs, outputs, add_total=True):
     return AttrDict(
         cost_estimation=L2Loss(1.0)(outputs.cost, outputs.cost_target)
     )
Beispiel #6
0
 def loss(self):
     est = self.pre_dists_sum
     explaining = L2Loss(self._hp.hack_explaining_loss)(est.float().mean(), 1)
     return AttrDict(explaining=explaining)
Beispiel #7
0
 def loss(self, inputs, model_output, add_total=True):
     return AttrDict(cost_estimation=L2Loss(1.0)(model_output.cost,
                                                 model_output.cost_target))