コード例 #1
0
def compute_variance(res, n_avg):

    activations, rate_map, g, pos = compute_ratemaps(model,
                                                     data_manager,
                                                     options,
                                                     res=res,
                                                     n_avg=n_avg)

    counts = np.zeros([res, res])
    variance = np.zeros([res, res])

    x_all = (pos[:, 0] + options['box_width'] / 2) / options['box_width'] * res
    y_all = (pos[:, 1] +
             options['box_height'] / 2) / options['box_height'] * res
    for i in tqdm(range(len(g))):
        x = int(x_all[i])
        y = int(y_all[i])
        if x >= 0 and x < res and y >= 0 and y < res:
            counts[x, y] += 1
            variance[x, y] += np.linalg.norm(
                g[i] - activations[:, x, y]) / np.linalg.norm(
                    g[i]) / np.linalg.norm(activations[:, x, y])

    for x in range(res):
        for y in range(res):
            if counts[x, y] > 0:
                variance[x, y] /= counts[x, y]

    return variance
コード例 #2
0
    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        avg_err = torch.stack([x['val_err'].mean() for x in outputs]).mean()

        # avg position error per step in sequence
        avg_seq_err = (torch.stack([x['val_err'] for x in outputs]).mean(dim=0)).mean(dim=-1)
        save_seq_err(avg_seq_err, self.options)

        # maybe only need to do outputs['val_loss']
        tensorboard_logs = {'val_loss': avg_loss, 'val_err': avg_err}
        # these are the full size of the epoch
        pos = torch.cat([x['pos'] for x in outputs], dim=0).to('cpu').detach().numpy()
        act = torch.cat([x['act'] for x in outputs], dim=0).to('cpu').detach().numpy()
        # TODO: fix these vars
        print(pos.shape, act.shape)
        # Save a picture of rate maps
        # save_ratemaps(self.model, self.trajectory_generator, self.options, step=tot_step)
        compute_ratemaps(pos, act, self.options, epoch=self.current_epoch) # TODO: specify range
        del pos, act
        return {'val_loss': avg_loss, 'val_err': avg_err, 'log': tensorboard_logs}
コード例 #3
0
    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        avg_err = torch.stack([x['val_err'].mean() for x in outputs]).mean()

        # avg position error per step in sequence
        avg_seq_err = (torch.stack([x['val_err'] for x in outputs
                                    ]).mean(dim=0)).mean(dim=-1)
        save_seq_err(avg_seq_err, self.options)

        # maybe only need to do outputs['val_loss']
        tensorboard_logs = {'val_loss': avg_loss, 'val_err': avg_err}
        # these are the full size of the epoch
        pos = torch.cat([x['coord'] for x in outputs], dim=0)
        # all_but_last_two_dims = pos.size()[:-2]
        # pos = pos.view(*all_but_last_two_dims, -1)
        pos = pos.to('cpu').detach().numpy()
        act = torch.cat([x['layer_outputs'] for x in outputs], dim=0)
        # all_but_last_two_dims = act.size()[:-2]
        # act = act.view(*all_but_last_two_dims, -1)
        act = act.to('cpu').detach().numpy()
        # TODO: fix these vars

        # Save a picture of rate maps
        # save_ratemaps(self.model, self.trajectory_generator, self.options, step=tot_step)
        for i in range(pos.shape[0], act.shape[0] + pos.shape[0],
                       pos.shape[0]):
            tmp_options = self.options
            j = i / pos.shape[0]
            tmp_options.run_ID + '_L{j}'
            ppos = pos.reshape(-1, pos.shape[-1])
            aact = act[i - pos.shape[0]:i].reshape(
                -1, act[i - pos.shape[0]:i].shape[-1])
            compute_ratemaps(ppos, aact, tmp_options, epoch=self.current_epoch)
        # TODO: specify range
        del pos, act
        return {
            'val_loss': avg_loss,
            'val_err': avg_err,
            'log': tensorboard_logs
        }