예제 #1
0
    def fit(self, w0=None, verbose=0, fnpy='_'):
        N, U, D = self.N, self.U, self.D

        if verbose > 0:
            t0 = time.time()

        if verbose > 0:
            print('\nC: %g, %g, p: %g' % (self.C1, self.C2, self.p))

        num_vars = (U + N + 1) * D
        if w0 is None:
            np.random.seed(0)
            if fnpy is not None:
                try:
                    w0 = np.load(fnpy, allow_pickle=False)
                    print('Restore from %s' % fnpy)
                except (IOError, ValueError):
                    w0 = 1e-3 * np.random.randn(num_vars)
            else:
                w0 = 1e-3 * np.random.randn(num_vars)
        if w0.shape != (num_vars, ):
            raise ValueError('ERROR: incorrect dimention for initial weights.')

        try:
            # f: callable(x, g, *args)
            # LBFGS().minimize(f, x0, progress=progress, args=args)
            optim = LBFGS()
            optim.linesearch = 'wolfe'
            optim.orthantwise_c = self.C2
            optim.orthantwise_start = U * D  # start index to compute L1 regularisation (included)
            optim.orthantwise_end = w0.shape[
                0]  # end   index to compute L1 regularisation (not included)
            res = optim.minimize(objective,
                                 w0,
                                 progress,
                                 args=(self.X, self.Y, self.C1, self.p,
                                       self.cliques, self.data_helper, verbose,
                                       fnpy))
            self.V = res[:U * D].reshape(U, D)
            self.W = res[U * D:(U + N) * D].reshape(N, D)
            self.mu = res[(U + N) * D:]
            assert self.mu.shape == (D, )
            self.trained = True
        except (LBFGSError, MemoryError) as err:
            self.trained = False
            sys.stderr.write('LBFGS failed: {0}\n'.format(err))
            sys.stderr.flush()

        if verbose > 0:
            print('Training finished in %.1f seconds' % (time.time() - t0))
예제 #2
0
    def fit(self, w0=None, verbose=0, fnpy='_'):
        N, U, D = self.N, self.U, self.D

        if verbose > 0:
            t0 = time.time()

        if verbose > 0:
            print('\nC: %g, p: %g' % (self.C, self.p))

        if w0 is not None:
            assert w0.shape[0] == (U + N + 1) * D
        else:
            if fnpy is not None:
                try:
                    w0 = np.load(fnpy, allow_pickle=False)
                    assert w0.shape[0] == (U + N + 1) * D
                    print('Restore from %s' % fnpy)
                except (IOError, ValueError):
                    w0 = np.zeros((U + N + 1) * D)

        try:
            # f: callable(x, g, *args)
            # LBFGS().minimize(f, x0, progress=progress, args=args)
            optim = LBFGS()
            optim.linesearch = 'wolfe'
            optim.orthantwise_c = self.C
            optim.orthantwise_start = (U + 1) * D  # start index to compute L1 regularisation (included)
            optim.orthantwise_end = w0.shape[0]     # end   index to compute L1 regularisation (not included)
            res = optim.minimize(objective_L1, w0, progress,
                                 args=(self.X, self.Y, self.C, self.p, self.cliques, self.data_helper, verbose, fnpy))
            self.mu = res[:D]
            self.V = res[D:(U + 1) * D].reshape(U, D)
            self.W = res[(U + 1) * D:].reshape(N, D)
            self.trained = True
        except (LBFGSError, MemoryError) as err:
            self.trained = False
            sys.stderr.write('LBFGS failed: {0}\n'.format(err))
            sys.stderr.flush()

        if verbose > 0:
            print('Training finished in %.1f seconds' % (time.time() - t0))