Demonstrate how to train and afterwards sample from it using Theano. This is demonstrated on MNIST. :param learning_rate: learning rate used for training the RBM :param training_epochs: number of epochs used for training :param dataset: path the the pickled dataset :par
(learning_rate=0.1, training_epochs=15,
dataset='mnist.pkl.gz', batch_size=20,
n_chains=20, n_samples=10, output_folder='rbm_plots',
n_hidden=500)
| 361 | |
| 362 | |
| 363 | def test_rbm(learning_rate=0.1, training_epochs=15, |
| 364 | dataset='mnist.pkl.gz', batch_size=20, |
| 365 | n_chains=20, n_samples=10, output_folder='rbm_plots', |
| 366 | n_hidden=500): |
| 367 | """ |
| 368 | Demonstrate how to train and afterwards sample from it using Theano. |
| 369 | |
| 370 | This is demonstrated on MNIST. |
| 371 | |
| 372 | :param learning_rate: learning rate used for training the RBM |
| 373 | |
| 374 | :param training_epochs: number of epochs used for training |
| 375 | |
| 376 | :param dataset: path the the pickled dataset |
| 377 | |
| 378 | :param batch_size: size of a batch used to train the RBM |
| 379 | |
| 380 | :param n_chains: number of parallel Gibbs chains to be used for sampling |
| 381 | |
| 382 | :param n_samples: number of samples to plot for each chain |
| 383 | |
| 384 | """ |
| 385 | datasets = load_data(dataset) |
| 386 | |
| 387 | train_set_x, train_set_y = datasets[0] |
| 388 | test_set_x, test_set_y = datasets[2] |
| 389 | |
| 390 | # compute number of minibatches for training, validation and testing |
| 391 | n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size |
| 392 | |
| 393 | # allocate symbolic variables for the data |
| 394 | index = T.lscalar() # index to a [mini]batch |
| 395 | x = T.matrix('x') # the data is presented as rasterized images |
| 396 | |
| 397 | rng = numpy.random.RandomState(123) |
| 398 | theano_rng = RandomStreams(rng.randint(2 ** 30)) |
| 399 | |
| 400 | # initialize storage for the persistent chain (state = hidden |
| 401 | # layer of chain) |
| 402 | persistent_chain = theano.shared(numpy.zeros((batch_size, n_hidden), |
| 403 | dtype=theano.config.floatX), |
| 404 | borrow=True) |
| 405 | |
| 406 | # construct the RBM class |
| 407 | rbm = RBM(input=x, n_visible=28 * 28, |
| 408 | n_hidden=n_hidden, numpy_rng=rng, theano_rng=theano_rng) |
| 409 | |
| 410 | # get the cost and the gradient corresponding to one step of CD-15 |
| 411 | cost, updates = rbm.get_cost_updates(lr=learning_rate, |
| 412 | persistent=persistent_chain, k=15) |
| 413 | |
| 414 | ################################# |
| 415 | # Training the RBM # |
| 416 | ################################# |
| 417 | if not os.path.isdir(output_folder): |
| 418 | os.makedirs(output_folder) |
| 419 | os.chdir(output_folder) |
| 420 |
no test coverage detected