def partial_fit(self, X): """ Fit the model to the data X which should contain a partial segment of the data. Adjust the parameters to maximize the likelihood of v using Stochastic Maximum Likelihood (SML). Parameters ---------- X : array-like, shape (n_samples, n_features) The data to use for training. """ v_pos = X h_pos = self._mean_hiddens(v_pos) if self.use_pcd: v_neg = self._sample_visibles(self.h_samples_) else: v_neg = self._sample_visibles(h_pos) h_neg = self._mean_hiddens(v_neg) lr = float(self.learning_rate) / v_pos.shape[0] op.add_dot(h_pos, v_pos, self.dW, True, False, alpha=1.0, beta=self.momentum) op.add_dot(h_neg, v_neg, self.dW, True, False, alpha=-1.0, beta=1.0) self.W += lr * self.dW self.dbh *= self.momentum self.dbv *= self.momentum self.dbh += (op.sum(h_pos, axis=0) - op.sum(h_neg, axis=0)).reshape(1, self.dbh.shape[1]) self.dbv += (op.sum(v_pos, axis=0) - op.sum(v_neg, axis=0)).reshape(1, self.dbv.shape[1]) self.bh += lr * self.dbh self.bv += lr * self.dbv if self.use_pcd: self.h_samples_ = op.sample_binomial(h_neg)
def fit(self, X, y=None): """Fit the model to the data X. Parameters ---------- X : {array-like, sparse matrix} shape (n_samples, n_features) Training data. Returns ------- self : BernoulliRBM The fitted model. """ self.h_samples_ *= 0 begin = time.time() for self.current_epoch in xrange(self.n_iter): for batch_slice in generate_slices(X.shape[0], self.batch_size): self.partial_fit(X[batch_slice]) if self.verbose: end = time.time() H = self.transform(X) R = self._mean_visibles(H) d = np.sqrt((op.sum((R - X)**2)) / X.shape[0]) print( "[%s] Iteration %d, ReconstructionRMSE %.4f time = %.2fs" % (type(self).__name__, self.current_epoch, d, end - begin)) begin = end return self
def fit(self, X, y=None): """Fit the model to the data X. Parameters ---------- X : {array-like, sparse matrix} shape (n_samples, n_features) Training data. Returns ------- self : BernoulliRBM The fitted model. """ self.h_samples_ *= 0 begin = time.time() for self.current_epoch in xrange(self.n_iter): for batch_slice in generate_slices(X.shape[0], self.batch_size): self.partial_fit(X[batch_slice]) if self.verbose: end = time.time() H = self.transform(X) R = self._mean_visibles(H) d = np.sqrt((op.sum((R-X)**2))/X.shape[0]) print("[%s] Iteration %d, ReconstructionRMSE %.4f time = %.2fs" % (type(self).__name__, self.current_epoch, d, end - begin)) begin = end return self
def partial_fit(self, X): """ Fit the model to the data X which should contain a partial segment of the data. Adjust the parameters to maximize the likelihood of v using Stochastic Maximum Likelihood (SML). Parameters ---------- X : array-like, shape (n_samples, n_features) The data to use for training. """ v_pos = X h_pos = self._mean_hiddens(v_pos) if self.use_pcd: v_neg = self._sample_visibles(self.h_samples_) else: v_neg = self._sample_visibles(h_pos) h_neg = self._mean_hiddens(v_neg) lr = float(self.learning_rate) / v_pos.shape[0] op.add_dot(h_pos, v_pos, self.dW, True, False, alpha=1.0, beta=self.momentum) op.add_dot(h_neg, v_neg, self.dW, True, False, alpha=-1.0, beta=1.0) self.W += lr * self.dW self.dbh *= self.momentum self.dbv *= self.momentum self.dbh += (op.sum(h_pos, axis=0) - op.sum(h_neg, axis=0)).reshape( 1, self.dbh.shape[1]) self.dbv += (op.sum(v_pos, axis=0) - op.sum(v_neg, axis=0)).reshape( 1, self.dbv.shape[1]) self.bh += lr * self.dbh self.bv += lr * self.dbv if self.use_pcd: self.h_samples_ = op.sample_binomial(h_neg)
def calculate_reconstruction_rmse(self, X): H = self.transform(X) R = self._mean_visibles(H) d = (op.sum((R - X)**2)) / X.shape[0] return np.sqrt(op.to_cpu(d))