def test_DBN(finetune_lr=0.1, pretraining_epochs=10, pretrain_lr=0.01, k=1, training_epochs=10, dataset='mnist.pkl.gz', batch_size=10): datasets = logistic.load_my_train_data() train_set_x, train_set_y = datasets[0] test_set_x, test_set_y = datasets[1] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size numpy_rng = numpy.random.RandomState(123) print('... building the model') dbn = DBN(numpy_rng=numpy_rng, n_ins=28 * 28, hidden_layers_sizes=[1000, 1000, 1000], n_outs=10) print('... getting the pretraining functions') pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_x, batch_size=batch_size, k=k) print('... pre-training the model') for i in range(dbn.n_layers): for epoch in range(pretraining_epochs): c = [] for batch_index in range(n_train_batches): c.append(pretraining_fns[i](index=batch_index, lr=pretrain_lr)) print('Pre-training layer %i, epoch %d, cost ' % (i, epoch)) print(numpy.mean(c, dtype='float64')) print('The pretraining code for file ' + os.path.split(__file__)[1]) train_fn, valid_model, test_model = dbn.build_finetune_functions( datasets=datasets, batch_size=batch_size, learning_rate=finetune_lr) print '.....finetuning the model' done_looping = False epoch = 0 best_test_score = 1 while (epoch < training_epochs) and (not done_looping): epoch += 1 for minibatch_index in range(n_train_batches): thefinetune_cost = train_fn(minibatch_index) print 'epoch %i, %f' % (epoch, thefinetune_cost) test_losses = test_model() test_score = numpy.mean(test_losses, dtype='float64') if test_score < best_test_score: print((' epoch %i, minibatch %i/%i, test error of ' 'best model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) best_test_score = test_score print(('Optimization complete with best score of %f %%, ') % (best_test_score * 100.)) print 'start predict' dbn.predict()
def test_dA(learning_rate=0.1, training_epochs=15, batch_size=500, output_folder='dA_plots.csv'): print('... data prepare') dataset = logistic.load_my_train_data() train_set_x, train_set_y = dataset[0] test_set_x, test_set_y = dataset[1] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size print('... building the model') index = T.lscalar() x = T.matrix('x') rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2**30)) da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x, n_visible=28 * 28, n_hidden=500) cost, updates = da.get_cost_updates(corruption_level=0., learning_rate=learning_rate) train_da = theano.function( [index], cost, updates=updates, givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]}) for epoch in range(training_epochs): c = [] for batch_index in range(n_train_batches): print('raining epoch %d', batch_index) c.append(train_da(batch_index)) print('Training epoch %d, cost ' % epoch, numpy.mean(c, dtype='float64'))
def test_rbm(learning_rate=0.1, training_epochs=20, dataset='mnist.pkl.gz', batch_size=20, n_chains=20, n_samples=10, output_folder='rbm_plots', n_hidden=500): dataset = logistic.load_my_train_data() train_set_x, train_set_y = dataset[0] test_set_x, test_set_y = dataset[1] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size index = T.lscalar() x = T.matrix('x') rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2**30)) persistent_chain = theano.shared(numpy.zeros((batch_size, n_hidden), dtype=theano.config.floatX), borrow=True) rbm = RBM(input=x, n_visible=28 * 28, n_hidden=n_hidden, numpy_rng=rng, theano_rng=theano_rng) cost, updates = rbm.get_cost_updates(lr=learning_rate, persistent=persistent_chain, k=15) if not os.path.isdir(output_folder): os.makedirs(output_folder) os.chdir(output_folder) train_rbm = theano.function( [index], cost, updates=updates, givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]}, name='train_rbm') for epoch in range(training_epochs): mean_cost = [] for batch_index in range(n_train_batches): mean_cost += [train_rbm(batch_index)] print('Training epoch %d, cost is ' % epoch, numpy.mean(mean_cost)) print rbm.W.get_value(borrow=True).T.shape image = Image.fromarray( tile_raster_images(X=rbm.W.get_value(borrow=True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1))) image.save('filters_at_epoch_%i.png' % epoch) print('Training took end minutes') number_of_test_samples = test_set_x.get_value(borrow=True).shape[0] test_idx = rng.randint(number_of_test_samples - n_chains) persistent_vis_chain = theano.shared( numpy.asarray(test_set_x.get_value(borrow=True)[test_idx:test_idx + n_chains], dtype=theano.config.floatX)) plot_every = 1000 ([presig_hids, hid_mfs, hid_samples, presig_vis, vis_mfs, vis_samples], updates) = theano.scan( rbm.gibbs_vhv, outputs_info=[None, None, None, None, None, persistent_vis_chain], n_steps=plot_every, name="gibbs_vhv") updates.update({persistent_vis_chain: vis_samples[-1]}) sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]], updates=updates, name='sample_fn') image_data = numpy.zeros((29 * n_samples + 1, 29 * n_chains - 1), dtype='uint8') for idx in range(n_samples): # generate `plot_every` intermediate samples that we discard, # because successive samples in the chain are too correlated vis_mf, vis_sample = sample_fn() print(' ... plotting sample %d' % idx) image_data[29 * idx:29 * idx + 28, :] = tile_raster_images( X=vis_mf, img_shape=(28, 28), tile_shape=(1, n_chains), tile_spacing=(1, 1)) # construct image image = Image.fromarray(image_data) image.save('samples.png') # end-snippet-7 os.chdir('../')
def test_mlp(learning_rate=0.01, L1_reg=0.00, L2_reg=0.0001, n_epochs=1000, batch_size=600, n_hidden=500): dataset = logistic.load_my_train_data() train_set_x, train_set_y = dataset[0] test_set_x, test_set_y = dataset[1] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size print('... building the model') index = T.lscalar() x = T.matrix('x') y = T.ivector('y') rng = numpy.random.RandomState(1234) classifier = MLP( rng = rng, input=x, n_in=28 * 28, n_hidden=n_hidden, n_out=10 ) cost = ( classifier.negative_log_likelihood(y) + L1_reg * classifier.L1 + L2_reg * classifier.L2_sqr ) test_model = theano.function( inputs=[index], outputs=classifier.errors(y), givens={ x: test_set_x[index * batch_size:(index + 1) * batch_size], y: test_set_y[index * batch_size:(index + 1) * batch_size] } ) gparams = [T.grad(cost, param) for param in classifier.params] updates = [ (param, param - learning_rate * gparam) for param, gparam in zip(classifier.params, gparams) ] train_model = theano.function( inputs=[index], outputs=cost, updates=updates, givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size] } ) print('... training the model') done_looping = False epoch = 0 best_test_score = 1 while (epoch < n_epochs) and (not done_looping): epoch += 1 for minibatch_index in range(n_train_batches): minibatch_avg_cost = train_model(minibatch_index) print minibatch_avg_cost test_losses = [test_model(i) for i in range(n_test_batches)] test_score = numpy.mean(test_losses) print ('epoch %i testscore %f %%') % (epoch, test_score) if test_score < best_test_score: print( ( ' epoch %i, minibatch %i/%i, test error of' ' best model %f %%' ) % ( epoch, minibatch_index + 1, n_train_batches, test_score * 100. ) ) best_test_score = test_score with open('best_modelh.pkl', 'wb') as f: pickle.dump(classifier.hiddenLayer, f) with open('best_modell.pkl', 'wb') as f: pickle.dump(classifier.logRegressionLayer, f)
def test_cnn(nkerns=(6, 12), learning_rate=0.01, n_epochs=10, batch_size=60, n_hidden=500, n_out=10): print('... data prepare') dataset = logistic.load_my_train_data() train_set_x, train_set_y = dataset[0] test_set_x, test_set_y = dataset[1] n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size print('... building the model') index = T.lscalar() x = T.matrix('x') y = T.ivector('y') rng = numpy.random.RandomState(1234) # layer0_input = x.reshape((batch_size, 1, 28, 28)) cnn = CNN(rng=rng, input=x, n_hidden_out=n_hidden, n_out=n_out, nkerns=nkerns, batch_size=batch_size) cost = (cnn.negative_log_likelihood(y)) test_model = theano.function( inputs=[index], outputs=cnn.errors(y), givens={ x: test_set_x[index * batch_size:(index + 1) * batch_size], y: test_set_y[index * batch_size:(index + 1) * batch_size] }) gparams = [T.grad(cost, param) for param in cnn.params] updates = [(param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(cnn.params, gparams)] train_model = theano.function( [index], cost, updates=updates, givens={ x: train_set_x[index * batch_size:(index + 1) * batch_size], y: train_set_y[index * batch_size:(index + 1) * batch_size] }) print('... training the model') done_looping = False epoch = 0 best_test_score = 1 while (epoch < n_epochs) and (not done_looping): epoch += 1 for minibatch_index in range(n_train_batches): minibatch_avg_cost = train_model(minibatch_index) print minibatch_avg_cost test_losses = [test_model(i) for i in range(n_test_batches)] test_score = numpy.mean(test_losses) print('epoch %i testscore %f %%') % (epoch, test_score) if test_score < best_test_score: print((' epoch %i, minibatch %i/%i, test error of' ' best model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) best_test_score = test_score with open('cnnlayer0.pkl', 'wb') as f: pickle.dump(cnn.layer0, f) with open('cnnlayer1.pkl', 'wb') as f: pickle.dump(cnn.layer1, f) with open('cnnlayer2.pkl', 'wb') as f: pickle.dump(cnn.layer2, f) with open('cnnlayer3.pkl', 'wb') as f: pickle.dump(cnn.logRegressionLayer, f)