image_size = [28,28] train_imgs,train_lbls,test_imgs,test_lbls = read_mnist(dim=image_size, n_train=60000, n_test=10000) ''' deep- belief net ''' print ("\nStarting a Deep Belief Net..") batch_size = 20 dbn = DeepBeliefNet(sizes={"vis":image_size[0]*image_size[1], "hid":500, "pen":500, "top":2000, "lbl":10}, image_size=image_size, n_labels=10, batch_size=batch_size ) ''' greedy layer-wise training ''' epochs = 10 iterations = int(60000*epochs/batch_size) dbn.train_greedylayerwise(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=iterations) #dbn.recognize(train_imgs, train_lbls) dbn.recognize(test_imgs, test_lbls) for digit in range(10): digit_1hot = np.zeros(shape=(1,10)) digit_1hot[0,digit] = 1 dbn.generate(digit_1hot, name="rbms")
epochs = 1 iterations = int(60000 * epochs / batch_size) dbn.train_greedylayerwise(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=iterations) #dbn.recognize(train_imgs, train_lbls) #dbn.recognize(test_imgs, test_lbls) #for digit in range(10): # digit_1hot = np.zeros(shape=(1,10)) # digit_1hot[0,digit] = 1 # dbn.generate(digit_1hot, name="rbms") ''' fine-tune wake-sleep training ''' dbn.train_wakesleep_finetune(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=iterations) dbn.recognize(train_imgs, train_lbls) dbn.recognize(test_imgs, test_lbls) for digit in range(10): digit_1hot = np.zeros(shape=(1, 10)) digit_1hot[0, digit] = 1 dbn.generate(digit_1hot, name="dbn")
dbn.train_greedylayerwise(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=20, verbose=True) # print("Classify training set") # dbn.recognize(train_imgs, train_lbls) # # print("Classify test set") # dbn.recognize(test_imgs, test_lbls) print("Image generation") for digit in range(10): digit_1hot = np.zeros(shape=(1, 10)) digit_1hot[0, digit] = 1 dbn.generate(digit_1hot, name="rbm") ''' fine-tune wake-sleep training ''' dbn.train_wakesleep_finetune(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=10) # dbn.recognize(train_imgs, train_lbls) # dbn.recognize(test_imgs, test_lbls) for digit in range(50): digit_1hot = np.zeros(shape=(1, 10)) digit_1hot[0, 6] = 1 dbn.generate(digit_1hot, name="dbn" + str(digit))
#plt.legend() #plt.savefig("pictures/4_2_recon_losses.png") # for name, im, lb in [("train", train_imgs, train_lbls), ("test", test_imgs, test_lbls)]: # acc = [] # for trials in range(10): # acc.append(dbn.recognize(train_imgs, train_lbls)) # print (name, acc[-1]) # print (f"{name} & {np.mean(acc):.5f} & {np.std(acc):.5f}") ### DO IMAGE GEN HERE! labels = np.zeros([1, 10]) labels[0, 0] = 1 vis = dbn.generate(labels) vis = vis.reshape(28, 28) plt.clf() plt.subplot(4, 5, 1) plt.imshow(vis) plt.show(block=False) plt.pause(0.1) for it in range(1, 10): labels[0, it] = 1 labels[0, it - 1] = 0 print(labels) vis = dbn.generate(labels) vis = vis.reshape(28, 28) plt.subplot(4, 5, it + 1) plt.imshow(vis) plt.show(block=False)
batch_size=20) ''' greedy layer-wise training ''' dbn.train_greedylayerwise(vis_trainset=train_imgs, lbl_trainset=train_lbls, n_iterations=90000) # dbn.recognize(train_imgs[:1000, :], train_lbls[:1000, :]) # dbn.recognize(test_imgs, test_lbls) imgs = [] for digit in range(10): digit_1hot = np.zeros(shape=(1, 10)) digit_1hot[0, digit] = 1 imgs.append(dbn.generate(digit_1hot, name="rbms", init_random_pen=True)) plot_generated(np.array(imgs), np.array([i for i in range(10)])) imgs = [] for digit in range(10): digit_1hot = np.zeros(shape=(1, 10)) digit_1hot[0, digit] = 1 imgs.append( dbn.generate(digit_1hot, name="rbms", init_random_pen=False)) plot_generated(np.array(imgs), np.array([i for i in range(10)]), dirname='rand_img') # ''' fine-tune wake-sleep training ''' # dbn.train_wakesleep_finetune(