def test_rbm_pcd_gibbs(x_test, Whv, bh, bv, p_target=0.5, n_gibbs_steps=5000, thinning=10, burnin=20): rbm = BernoulliRBM(n_components=Whv.shape[0], learning_rate=0.0) rbm.components_, rbm.intercept_hidden_, rbm.intercept_visible_ = Whv, bh, bv evidence_mask = np.random.binomial( 1, p_target, x_test.shape) # 0: target node, 1: evidence node, V = np.random.binomial(1, p_target, x_test.shape) V = x_test * evidence_mask + V * (1 - evidence_mask) prob1 = np.zeros_like(V) count = 0 for it in range(n_gibbs_steps): V = rbm.gibbs(V) V = x_test * evidence_mask + V * (1 - evidence_mask) if (it + 1) % thinning == 0 and it > burnin: prob1 += V count += 1 prob1 /= count prob1_clipped = prob1.clip(1e-15, 1 - 1e-15) target_mask = 1 - evidence_mask logp = x_test * np.log(prob1_clipped) + ( 1 - x_test) * np.log(1 - prob1_clipped) logp *= target_mask return -logp.sum() / target_mask.sum() / np.log(2)
def classify(self,X): transformed = self.first_pipeline.transform(X) transformed = np.concatenate((transformed,[[0]*10]*len(transformed)),axis=1) # The inverse of rbm_3 to go from hidden layer to visible layer rbm_aux = BernoulliRBM() rbm_aux.intercept_hidden_ = self.rbm_3.intercept_visible_ rbm_aux.intercept_visible_ = self.rbm_3.intercept_hidden_ rbm_aux.components_ = np.transpose(self.rbm_3.components_) results = rbm_aux.transform(self.rbm_3.transform(transformed)) results = results[:,-10:] return np.argmax(results,axis=1)
def classify(self, X): transformed = self.first_pipeline.transform(X) transformed = np.concatenate( (transformed, [[0] * 10] * len(transformed)), axis=1) # The inverse of rbm_3 to go from hidden layer to visible layer rbm_aux = BernoulliRBM() rbm_aux.intercept_hidden_ = self.rbm_3.intercept_visible_ rbm_aux.intercept_visible_ = self.rbm_3.intercept_hidden_ rbm_aux.components_ = np.transpose(self.rbm_3.components_) results = rbm_aux.transform(self.rbm_3.transform(transformed)) results = results[:, -10:] return np.argmax(results, axis=1)
image_size = [28, 28] train_imgs, train_lbls, test_imgs, test_lbls = read_mnist(dim=image_size, n_train=6000, n_test=10000) n_iter = 10 v_h = BernoulliRBM(n_components=500, learning_rate=0.01, batch_size=20, verbose=1, n_iter=n_iter) try: v_h.components_, v_h.intercept_hidden_, v_h.intercept_visible_ = load_stuff( "v_h_sklearn.npz") except FileNotFoundError: v_h.fit(train_imgs) save_stuff( "v_h_sklearn.npz", [v_h.components_, v_h.intercept_hidden_, v_h.intercept_visible_]) v_h_p_hv = sigmoid(test_imgs @ v_h.components_.T + v_h.intercept_hidden_) v_h_p_vh = sigmoid( sample_binary(v_h_p_hv) @ v_h.components_ + v_h.intercept_visible_) print("Layer1 MSE:", mean_squared_error(test_imgs, v_h_p_vh)) h_p = BernoulliRBM(n_components=500, learning_rate=0.01, batch_size=20,
def get_likelihood(data, W, vb, hb): rbm = BernoulliRBM(n_components=W.shape[1]) rbm.components_ = W.T rbm.intercept_hidden_ = hb rbm.intercept_visible_ = vb return rbm.score_samples(data).mean()