Ejemplo n.º 1
0
    def update(self, samples, axis=0) -> None:
        """
        Update the online calculation of the mean and variance.

        Notes:
            Modifies the metrics in place.

        Args:
            samples: data samples

        Returns:
            None

        """
        # compute the sample size and sample mean
        n = len(samples)
        sample_mean = be.tsum(samples, axis=axis) / max(n, 1)
        sample_square = be.tsum(be.square(be.subtract(sample_mean, samples)),
                                axis=axis)

        if self.mean is None:
            self.mean = be.zeros_like(sample_mean)
            self.square = be.zeros_like(sample_square)
            self.var = be.zeros_like(sample_square)
            self.num = 0

        delta = sample_mean - self.mean
        new_num = self.num + n
        correction = n * self.num * be.square(delta) / max(new_num, 1)

        self.square += sample_square + correction
        self.var = self.square / max(new_num - 1, 1)
        self.mean = (self.num * self.mean + n * sample_mean) / max(new_num, 1)
        self.num = new_num
Ejemplo n.º 2
0
    def update(self, samples, axis=0) -> None:
        """
        Update the online calculation of the mean.

        Notes:
            Modifies the metrics in place.

        Args:
            samples: data samples

        Returns:
            None

        """
        # compute the sample size and sample mean
        n = len(samples)
        sample_mean = be.tsum(samples, axis=axis) / n

        # initialize the num and mean attributes if necessary
        if self.mean is None:
            self.mean = be.zeros_like(sample_mean)
            self.num = 0

        # update the num and mean attributes
        tmp = self.num * self.mean + n * sample_mean
        self.num += n
        self.mean = tmp / self.num
Ejemplo n.º 3
0
def compute_reconstructions(rbm,
                            v_data,
                            fit,
                            n_recon=10,
                            vertical=False,
                            num_to_avg=1):

    v_model = be.zeros_like(v_data)

    # Average over n reconstruction attempts
    for k in range(num_to_avg):
        data_state = State.from_visible(v_data, rbm)
        visible = data_state.units[0]
        reconstructions = fit.DrivenSequentialMC(rbm)
        reconstructions.set_state(data_state)
        dropout_scale = State.dropout_rescale(rbm)
        reconstructions.update_state(1, dropout_scale)
        v_model += rbm.deterministic_iteration(1, reconstructions.state,
                                               dropout_scale).units[0]

    v_model /= num_to_avg

    idx = numpy.random.choice(range(len(v_model)), n_recon, replace=False)
    grid = numpy.array(
        [[be.to_numpy_array(visible[i]),
          be.to_numpy_array(v_model[i])] for i in idx])
    if vertical:
        return grid
    else:
        return grid.swapaxes(0, 1)
Ejemplo n.º 4
0
def compute_reconstructions(rbm, v_data, n_recon=10, vertical=False, num_to_avg=1):
    v_model = be.zeros_like(v_data)
    # Average over n reconstruction attempts
    for k in range(num_to_avg):
        reconstructions = rbm.compute_reconstructions(v_data)
        v_model += reconstructions.get_visible() / num_to_avg

    idx = np.random.choice(range(len(v_model)), n_recon, replace=False)
    grid = np.array([[be.to_numpy_array(v_data[i]),
                         be.to_numpy_array(v_model[i])] for i in idx])
    if vertical:
        return grid
    else:
        return grid.swapaxes(0,1)