Calculate loss of the supplied rollout
515520
def calculate_gradient(self, batch_info, device, model, rollout):
        
        evaluator = model.evaluate(rollout)

        # Use evaluator interface to get the what we are interested in from the model
        advantages = evaluator.get('rollout:advantages')
        returns = evaluator.get('rollout:returns')
        rollout_values = evaluator.get('rollout:values')

        logprobs = evaluator.get('model:action:logprobs')
        values = evaluator.get('model:values')
        entropy = evaluator.get('model:entropy')

        # Actual calculations. Pretty trivial
        policy_loss = -torch.mean(advantages * logprobs)
        value_loss = 0.5 * F.mse_loss(values, returns)
        policy_entropy = torch.mean(entropy)

        loss_value = (
            policy_loss - self.entropy_coefficient * policy_entropy + self.value_coefficient * value_loss
        )

        loss_value.backward()

        return {
            'policy_loss': policy_loss.item(),
            'value_loss': value_loss.item(),
            'policy_entropy': policy_entropy.item(),
            'advantage_norm': torch.norm(advantages).item(),
            'explained_variance': explained_variance(returns, rollout_values)
        }
1241300
def logloss(y, p):
    

    p[p < EPS] = EPS
    p[p > 1 - EPS] = 1 - EPS
    return log_loss(y, p)
1185251
def get_dmg_csq(self, assets_by_site, gmf):
        
        A = sum(len(assets) for assets in assets_by_site)
        L = len(self.loss_types)
        D = len(self.damage_states)
        out = numpy.zeros((A, L, 1, D + 1), F32)
        for assets, gmv in zip(assets_by_site, gmf):
            group = group_array(assets, 'taxonomy')
            for taxonomy, assets in group.items():
                for l, loss_type in enumerate(self.loss_types):
                    fracs = self[taxonomy](loss_type, assets, [gmv])
                    for asset, frac in zip(assets, fracs):
                        dmg = asset['number'] * frac[0, :D]
                        csq = asset['value-' + loss_type] * frac[0, D]
                        out[asset['ordinal'], l, 0, :D] = dmg
                        out[asset['ordinal'], l, 0, D] = csq
        return out
897625
def ppo_loss(policy_net_apply,
             new_policy_params,
             old_policy_params,
             value_net_apply,
             value_net_params,
             padded_observations,
             padded_actions,
             padded_rewards,
             reward_mask,
             gamma=0.99,
             lambda_=0.95,
             epsilon=0.2):
  """"""
  B, T = padded_rewards.shape  # pylint: disable=invalid-name
  assert (B, T + 1) == padded_observations.shape[:2]
  assert (B, T) == padded_actions.shape
  assert (B, T) == padded_rewards.shape
  assert (B, T) == reward_mask.shape

  # Compute predicted values and predicted log-probs and hand it over to
  # `ppo_loss_given_predictions`.

  # (B, T+1, 1)
  predicted_values = value_net_apply(padded_observations, value_net_params)
  assert (B, T + 1, 1) == predicted_values.shape

  # log_probab_actions_{old,new} are both (B, T+1, A)
  log_probab_actions_old = policy_net_apply(padded_observations,
                                            old_policy_params)
  log_probab_actions_new = policy_net_apply(padded_observations,
                                            new_policy_params)
  assert (B, T + 1) == log_probab_actions_old.shape[:2]
  assert (B, T + 1) == log_probab_actions_new.shape[:2]
  assert log_probab_actions_old.shape[-1] == log_probab_actions_new.shape[-1]

  return ppo_loss_given_predictions(log_probab_actions_new,
                                    log_probab_actions_old,
                                    predicted_values,
                                    padded_actions,
                                    padded_rewards,
                                    reward_mask,
                                    gamma=gamma,
                                    lambda_=lambda_,
                                    epsilon=epsilon)
922143
def _partition(episodes):
    """"""

    from ray.rllib.evaluation.sampler import RolloutMetrics

    rollouts, estimates = [], []
    for e in episodes:
        if isinstance(e, RolloutMetrics):
            rollouts.append(e)
        elif isinstance(e, OffPolicyEstimate):
            estimates.append(e)
        else:
            raise ValueError("Unknown metric type: {}".format(e))
    return rollouts, estimates
1109211
def export_losses_by_asset(ekey, dstore):
    
    loss_dt = dstore['oqparam'].loss_dt(stat_dt)
    losses_by_asset = dstore[ekey[0]].value
    rlzs = dstore['csm_info'].get_rlzs_assoc().realizations
    assets = get_assets(dstore)
    writer = writers.CsvWriter(fmt=writers.FIVEDIGITS)
    for rlz in rlzs:
        losses = losses_by_asset[:, rlz.ordinal]
        dest = dstore.build_fname('losses_by_asset', rlz, 'csv')
        data = compose_arrays(assets, losses.copy().view(loss_dt)[:, 0])
        writer.save(data, dest)
    return writer.getsaved()
716392
def rollback(self):
        """"""
        commands = []
        commands.append('configure replace flash:rollback-0')
        commands.append('write memory')
        self.device.run_commands(commands)
738666
def plotLoss(self, filename=None):
    
    plt.figure()
    plt.plot(self.losses.keys(), self.losses.values())
    plt.xlabel("Iteration")
    plt.ylabel("Loss")
    plt.title("Learning curve for {}".format(self))

    if filename is not None:
      plt.savefig(filename)
1007238
def _log_loss(y, y_pred, w):
    """"""
    eps = 1e-15
    inv_y_pred = np.clip(1 - y_pred, eps, 1 - eps)
    y_pred = np.clip(y_pred, eps, 1 - eps)
    score = y * np.log(y_pred) + (1 - y) * np.log(inv_y_pred)
    return np.average(-score, weights=w)
960780
def plot_loss(self, n_skip=10, n_skip_end=5):
        '''
         
        '''
        if not in_ipynb(): plt.switch_backend('agg')
        plt.plot(self.iterations[n_skip:-n_skip_end], self.losses[n_skip:-n_skip_end])
        if not in_ipynb():
            plt.savefig(os.path.join(self.save_path, 'loss_plot.png'))
            np.save(os.path.join(self.save_path, 'losses.npy'), self.losses[10:])
150239
def sample_batch_trajectories(self, rollout_length):
        
        results = []

        for i in range(self.num_envs):
            results.append(self.sample_rollout_single_env(rollout_length))

        return np.stack(results, axis=-1)
1334932
def rollback(self, label, plane):
        """"""
        cm_label = 'condoor-{}'.format(int(time.time()))
        self.device.send(self.rollback_cmd.format(label), timeout=120)
        return cm_label
285941
def __roll(self, unrolled):
        """"""
        rolled = []
        index = 0
        for count in range(len(self.__sizes) - 1):
            in_size = self.__sizes[count]
            out_size = self.__sizes[count+1]
            theta_unrolled = np.matrix(unrolled[index:index+(in_size+1)*out_size])
            theta_rolled = theta_unrolled.reshape((out_size, in_size+1))
            rolled.append(theta_rolled)
            index += (in_size + 1) * out_size
        return rolled
252205
def extract_src_loss_table(dstore, loss_type):
    
    oq = dstore['oqparam']
    li = oq.lti[loss_type]
    source_ids = dstore['source_info']['source_id']
    idxs = dstore['ruptures'].value[['srcidx', 'grp_id']]
    losses = dstore['rup_loss_table'][:, li]
    slt = numpy.zeros(len(source_ids), [('grp_id', U32), (loss_type, F32)])
    for loss, (srcidx, grp_id) in zip(losses, idxs):
        slt[srcidx][loss_type] += loss
        slt[srcidx]['grp_id'] = grp_id
    slt = util.compose_arrays(source_ids, slt, 'source_id')
    slt.sort(order=loss_type)
    return slt[::-1]
293633
def _huber_loss(x, delta=1.0):
    """"""
    return tf.where(
        tf.abs(x) < delta,
        tf.square(x) * 0.5, delta * (tf.abs(x) - 0.5 * delta))
270587
function rollback() {
    var parsed = ensureConfig();
    var name = parsed.name;
    // check if is an index number.
    // if yes try to lookup the id
    // by its index.
    try {
        var idx = void 0;
        if (/^[0-9]+$/.test(name)) {
            idx = parseInt(parsed.name) - 1;
            if (idx >= 0) {
                var keys = Object.keys(mu.rollbacks);
                var key = keys[idx];
                if (key)
                    name = key;
            }
        }
    }
    catch (ex) { }
    mu.rollback(parsed.name, parsed.output);
}
765632
def build(self, losses_by_event, stats=()):
        
        P, R = len(self.return_periods), len(self.weights)
        L = len(self.loss_dt.names)
        array = numpy.zeros((P, R, L), F32)
        for r in losses_by_event:
            num_events = self.num_events[r]
            losses = losses_by_event[r]
            for l, lt in enumerate(self.loss_dt.names):
                ls = losses[:, l].flatten()  # flatten only in ucerf
                # NB: do not use squeeze or the gmf_ebrisk tests will break
                lbp = losses_by_period(
                    ls, self.return_periods, num_events, self.eff_time)
                array[:, r, l] = lbp
        return self.pair(array, stats)