Example #1
0
 def _append_iterations(self, iters):
     """
     Append some arrays for more iterations
     """
     self.L = np.append(self.L, misc.nans(iters))
     self.cputime = np.append(self.cputime, misc.nans(iters))
     for (node, l) in self.l.items():
         self.l[node] = np.append(l, misc.nans(iters))
     return
Example #2
0
 def _append_iterations(self, iters):
     """
     Append some arrays for more iterations
     """
     self.L = np.append(self.L, misc.nans(iters))
     self.cputime = np.append(self.cputime, misc.nans(iters))
     for (node, l) in self.l.items():
         self.l[node] = np.append(l, misc.nans(iters))
     return
Example #3
0
    def __init__(self, *parents, initialize=True, **kwargs):

        # Terms for the lower bound (G for latent and F for observed)
        self.g = np.array(np.nan)
        self.f = np.array(np.nan)

        super().__init__(*parents,
                         initialize=initialize,
                         dims=self.dims,
                         **kwargs)

        if not initialize:
            axes = len(self.plates)*(1,)
            self.phi = [misc.nans(axes+dim) for dim in self.dims]
Example #4
0
    def __init__(self, *parents, initialize=True, **kwargs):

        self.annealing = 1.0

        # Terms for the lower bound (G for latent and F for observed)
        self.g = np.array(np.nan)
        self.f = np.array(np.nan)

        super().__init__(*parents,
                         initialize=initialize,
                         dims=self.dims,
                         **kwargs)

        if not initialize:
            axes = len(self.plates) * (1, )
            self.phi = [misc.nans(axes + dim) for dim in self.dims]
Example #5
0
    def __init__(self, *args, initialize=True, dims=None, **kwargs):

        self._id = Node._id_counter
        Node._id_counter += 1

        super().__init__(*args, dims=dims, **kwargs)

        # Initialize moment array
        axes = len(self.plates) * (1, )
        self.u = [misc.nans(axes + dim) for dim in dims]

        # Not observed
        self.observed = False

        self.ndims = [len(dim) for dim in self.dims]

        if initialize:
            self.initialize_from_prior()
Example #6
0
    def __init__(self, *parents, initialize=True, phi_bias=None, **kwargs):

        self.annealing = 1.0

        # Terms for the lower bound (G for latent and F for observed)
        self.g = np.array(np.nan)
        self.f = np.array(np.nan)

        self._phi_bias = phi_bias if phi_bias is not None else len(self.dims) * [0.0]

        super().__init__(*parents,
                         initialize=initialize,
                         dims=self.dims,
                         **kwargs)

        if not initialize:
            axes = len(self.plates)*(1,)
            self.phi = [misc.nans(axes+dim) for dim in self.dims]
Example #7
0
    def __init__(self, *args, initialize=True, dims=None, **kwargs):

        self._id = Node._id_counter
        Node._id_counter += 1

        super().__init__(*args,
                         dims=dims,
                         **kwargs)

        # Initialize moment array
        axes = len(self.plates)*(1,)
        self.u = [misc.nans(axes+dim) for dim in dims]

        # Not observed
        self.observed = False

        self.ndims = [len(dim) for dim in self.dims]

        if initialize:
            self.initialize_from_prior()
Example #8
0
    def update(self, *nodes, repeat=1, plot=False, tol=None, verbose=True):

        # TODO/FIXME:
        #
        # If no nodes are given and thus everything is updated, the update order
        # should be from down to bottom. Or something similar..

        # Append the cost arrays
        self.L = np.append(self.L, misc.nans(repeat))
        for (node, l) in self.l.items():
            self.l[node] = np.append(l, misc.nans(repeat))

        # By default, update all nodes
        if len(nodes) == 0:
            nodes = self.model

        converged = False

        for i in range(repeat):
            t = time.clock()

            # Update nodes
            for node in nodes:
                X = self[node]
                if hasattr(X, 'update') and callable(X.update):
                    X.update()
                    if plot:
                        self.plot(X)

            # Call the custom function provided by the user
            if callable(self.callback):
                z = self.callback()
                if z is not None:
                    z = np.array(z)[...,np.newaxis]
                    if self.callback_output is None:
                        self.callback_output = z
                    else:
                        self.callback_output = np.concatenate((self.callback_output,z),
                                                              axis=-1)

            # Compute lower bound
            L = self.loglikelihood_lowerbound()
            if verbose:
                print("Iteration %d: loglike=%e (%.3f seconds)" 
                      % (self.iter+1, L, time.clock()-t))

            # Check the progress of the iteration
            if self.iter > 0:
                # Check for errors
                if self.L[self.iter-1] - L > 1e-6:
                    L_diff = (self.L[self.iter-1] - L)
                    warnings.warn("Lower bound decreased %e! Bug somewhere or "
                                  "numerical inaccuracy?" % L_diff)

                # Check for convergence
                if tol is None:
                    tol = self.tol
                div = 0.5 * (abs(L) + abs(self.L[self.iter-1]))
                if (L - self.L[self.iter-1]) / div < tol or L - self.L[self.iter-1] <= 0:
                    converged = True
                    if verbose:
                        print("Converged at iteration %d." % (self.iter+1))

            self.L[self.iter] = L
            self.iter += 1

            # Auto-save, if requested
            if (self.autosave_iterations > 0 
                and np.mod(self.iter, self.autosave_iterations) == 0):

                self.save(self.autosave_filename)
                if verbose:
                    print('Auto-saved to %s' % self.autosave_filename)

            if converged:
                return