l = data_np.shape[0] tr = int(0.8*l) valid_np = data_np[tr:] data_np = data_np[:tr] print("-----------------------------") print(data_np.shape) print(valid_np.shape) train_ls = [] for i in range(len(data_np)): img = data_np[i] l = img[-1] img = img[:-1] reconstructed_img = torch.FloatTensor(np.array(img)) _,reconstructed_img = rbm.reconstruct(reconstructed_img) reconstructed_img = reconstructed_img.squeeze() reconstructed_img = list(reconstructed_img) reconstructed_img.append(l) train_ls.append(reconstructed_img) valid_ls = [] for i in range(len(valid_np)): img = valid_np[i] l = img[-1] img = img[:-1] reconstructed_img = torch.FloatTensor(np.array(img)) _,reconstructed_img = rbm.reconstruct(reconstructed_img) reconstructed_img = reconstructed_img.squeeze() reconstructed_img = list(reconstructed_img) reconstructed_img.append(l)
[torch.Tensor(number) for i in range(len(particular_mnist))]) dbn_mnist = DBN(visible_units=28 * 28, hidden_units=[23 * 23, 18 * 18], k=5, learning_rate=0.01, learning_rate_decay=True, xavier_init=True, increase_to_cd_k=False, use_gpu=False) Epoch = [10, 15, 20, 25, 30, 35] for epoch in Epoch: print('-----------------------{}------------'.format(epoch)) dbn_mnist.train_static(train_data, train_label, epoch, batch_size) idx = 3 img = train_dataset.train_data[idx] reconstructed_img = img.view(1, -1).type(torch.FloatTensor) _, reconstructed_img = dbn_mnist.reconstruct(reconstructed_img) reconstructed_img = reconstructed_img.view((28, 28)) print("The original number: {}".format(train_dataset.train_labels[idx])) plt.imshow(img, cmap='gray') plt.show() print("The reconstructed image") plt.title(epoch) plt.imshow(reconstructed_img, cmap='gray') plt.show()