def fit(self,
            X_train,
            Y_train,
            X_val,
            Y_val,
            n_epoch=10,
            n_batch=100,
            logname='run'):
        """Train the model"""
        alpha = 1.0  # learning rate, which can be adjusted later
        n_data = len(X_train)
        n_superbatch = self.n_superbatch

        for epoch in range(n_epoch):
            # In each epoch, we do a full pass over the training data:
            train_batches, train_err, train_acc = 0, 0, 0
            start_time = time.time()

            # iterate over superbatches to save time on GPU memory transfer
            for X_sb, Y_sb in self.iterate_superbatches(
                    X_train,
                    Y_train,
                    n_superbatch,
                    datatype='train',
                    shuffle=True,
            ):
                for idx1, idx2 in iterate_minibatch_idx(len(X_sb), n_batch):
                    err, acc = self.train(idx1, idx2, alpha)
                    # collect metrics
                    err = self.pseudolikelihood(X_sb[idx1:idx2].reshape(
                        -1, 784))
                    train_batches += 1
                    train_err += err
                    train_acc += acc

                    if train_batches % 100 == 0:
                        n_total = epoch * n_data + n_batch * train_batches
                        metrics = [
                            n_total,
                            train_err / train_batches,
                            train_acc / train_batches,
                        ]
                        log_metrics(logname, metrics)

            print "Epoch {} of {} took {:.3f}s ({} minibatches)".format(
                epoch + 1, n_epoch,
                time.time() - start_time, train_batches)

            print "  training:\t\t{:.6f}\t{:.6f}".format(
                train_err / train_batches, train_acc / train_batches)

        # reserve N of training data points to kick start hallucinations
        hallu_i = self.numpy_rng.randint(n_data - self.n_chain)
        self.hallu_set = np.asarray(X_train[hallu_i:hallu_i + self.n_chain],
                                    dtype=theano.config.floatX)
    def fit(
        self, X_train, Y_train, X_val, Y_val,
        n_epoch=10, n_batch=100, logname='run',
    ):
        """Train the model"""
        alpha = 1.0  # learning rate, which can be adjusted later
        n_data = len(X_train)
        n_superbatch = self.n_superbatch

        for epoch in range(n_epoch):
            # In each epoch, we do a full pass over the training data:
            train_batches, train_err, train_acc = 0, 0, 0
            start_time = time.time()

            # iterate over superbatches to save time on GPU memory transfer
            for X_sb, Y_sb in self.iterate_superbatches(
                X_train, Y_train, n_superbatch,
                datatype='train', shuffle=True,
            ):
                for idx1, idx2 in iterate_minibatch_idx(len(X_sb), n_batch):
                    err, acc = self.train(idx1, idx2, alpha)
                    # collect metrics
                    train_batches += 1
                    train_err += err
                    train_acc += acc
                    if train_batches % 100 == 0:
                        n_total = epoch * n_data + n_batch * train_batches
                        metrics = [
                            n_total,
                            train_err / train_batches,
                            train_acc / train_batches,
                        ]
                        log_metrics(logname, metrics)

            print "Epoch {} of {} took {:.3f}s ({} minibatches)".format(
                epoch + 1, n_epoch, time.time() - start_time, train_batches)

            # make a full pass over the training data and record metrics:
            train_err, train_acc = evaluate(
                self.loss, X_train, Y_train, batchsize=100,
            )
            val_err, val_acc = evaluate(
                self.loss, X_val, Y_val, batchsize=100,
            )

            print "  training:\t\t{:.6f}\t{:.6f}".format(
                train_err, train_acc
            )
            print "  validation:\t\t{:.6f}\t{:.6f}".format(
                val_err, val_acc
            )

            metrics = [epoch, train_err, train_acc, val_err, val_acc]
            log_metrics(logname + '.val', metrics)
예제 #3
0
    def fit(
        self, X_train, Y_train, X_val, Y_val,
        n_epoch=10, n_batch=100, logname='run',
    ):
        """Train the model"""
        alpha = 1.0  # learning rate, which can be adjusted later
        n_data = len(X_train)
        n_superbatch = self.n_superbatch

        # print '...', self.rbm.logZ_exact()
        # print '...', self.rbm.logZ_exact(marg='h')

        for epoch in range(n_epoch):
            # In each epoch, we do a full pass over the training data:
            train_batches, train_err, train_acc = 0, 0, 0
            start_time = time.time()

            # iterate over superbatches to save time on GPU memory transfer
            for X_sb, Y_sb in self.iterate_superbatches(
                X_train, Y_train, n_superbatch,
                datatype='train', shuffle=True,
            ):
                for idx1, idx2 in iterate_minibatch_idx(len(X_sb), n_batch):
                    # train model
                    self.rbm.reset_averages()
                    err, acc, z = self.train(idx1, idx2, alpha)

                    # train rbm
                    z_rbm = z.reshape((128,1,8,8))
                    # self.rbm.train_batch(z.reshape((128,1,8,8)), np.zeros((n_batch,), dtype='int32'), alpha)

                    # q steps
                    for i in range(2): self.rbm.train_q0(z_rbm)

                    # p steps
                    self.rbm.train_p_batch(z_rbm, alpha)

                    # collect metrics
                    # print err, acc
                    train_batches += 1
                    train_err += err
                    train_acc += acc
                    if train_batches % 100 == 0:
                        n_total = epoch * n_data + n_batch * train_batches
                        metrics = [
                            n_total,
                            train_err / train_batches,
                            train_acc / train_batches,
                        ]
                        log_metrics(logname, metrics)

            print "Epoch {} of {} took {:.3f}s ({} minibatches)".format(
                epoch + 1, n_epoch, time.time() - start_time, train_batches)

            # make a full pass over the training data and record metrics:
            train_err, train_acc = evaluate(
                self.loss, X_train, Y_train, batchsize=128,
            )
            val_err, val_acc = evaluate(
                self.loss, X_val, Y_val, batchsize=128,
            )

            print "  training:\t\t{:.6f}\t{:.6f}".format(
                train_err, train_acc
            )
            print "  validation:\t\t{:.6f}\t{:.6f}".format(
                val_err, val_acc
            )

            metrics = [epoch, train_err, train_acc, val_err, val_acc]
            log_metrics(logname + '.val', metrics)

        # reserve N of training data points to kick start hallucinations
        self.rbm.hallu_set = np.asarray(
            z[:100,:].reshape((100,1,8,8)),
            dtype=theano.config.floatX
        )