def test_sample_ae(): """ Visualize some samples from the trained unsupervised GSN. """ with open("gsn_ae_example.pkl") as f: gsn = pickle.load(f) # random point to start at mb_data = MNIST(which_set='test').X[105:106, :] history = gsn.get_samples([(0, mb_data)], walkback=1000, symbolic=False, include_first=True) history = list(itertools.chain(*history)) history = np.vstack(history) tiled = image.tile_raster_images(history, img_shape=[28, 28], tile_shape=[50, 50], tile_spacing=(2, 2)) image.save("gsn_ae_example.png", tiled) # code to get log likelihood from kernel density estimator # this crashed on GPU (out of memory), but works on CPU pw = ParzenWindows(MNIST(which_set='test').X, .20) print(pw.get_ll(history))
def model1(): #pdb.set_trace() # train set X has dim (60,000, 784), y has dim (60,000, 10) train_set = MNIST(which_set='train', one_hot=True) # test set X has dim (10,000, 784), y has dim (10,000, 10) valid_set = MNIST(which_set='test', one_hot=True) test_set = MNIST(which_set='test', one_hot=True) #import pdb #pdb.set_trace() #print train_set.X.shape[1] # =====<Create the MLP Model>===== h2_layer = NoisyRELU(layer_name='h1', sparse_init=15, noise_factor=5, dim=1000, desired_active_rate=0.2, bias_factor=20, max_col_norm=1) #h2_layer = RectifiedLinear(layer_name='h2', dim=100, sparse_init=15, max_col_norm=1) #print h1_layer.get_params() #h2 = RectifiedLinear(layer_name='h2', dim=500, sparse_init=15, max_col_norm=1) y_layer = Softmax(layer_name='y', n_classes=10, irange=0., max_col_norm=1) mlp = MLP(batch_size=200, input_space=VectorSpace(dim=train_set.X.shape[1]), layers=[h2_layer, y_layer]) # =====<Create the SGD algorithm>===== sgd = SGD(init_momentum=0.1, learning_rate=0.01, monitoring_dataset={'valid': valid_set}, cost=MethodCost('cost_from_X'), termination_criterion=MonitorBased( channel_name='valid_y_misclass', prop_decrease=0.001, N=50)) #sgd.setup(model=mlp, dataset=train_set) # =====<Extensions>===== ext = [MomentumAdjustor(start=1, saturate=10, final_momentum=0.9)] # =====<Create Training Object>===== save_path = './mlp_model1.pkl' train_obj = Train(dataset=train_set, model=mlp, algorithm=sgd, extensions=ext, save_path=save_path, save_freq=0) #train_obj.setup_extensions() #import pdb #pdb.set_trace() train_obj.main_loop() # =====<Run the training>===== '''
def main(): #import pdb #pdb.set_trace() ################### #BUILD THE DATASET# ################### print 'build the dataset' train_set = MNIST(which_set='train', one_hot=False) test_set = MNIST(which_set='test', one_hot=False) train_setX, valid_setX = split(train_set.X, [50000], axis=0) train_sety, valid_sety = split(train_set.y, [50000], axis=0) #import pdb #pdb.set_trace() ################## #BUILD THE LAYERS# ################## print 'build the layers' input_size = len(train_setX[0]) h1 = Tanh(prev_layer_size=input_size, this_layer_size=2000) output_layer = Softmax(prev_layer_size=h1.this_layer_size, this_layer_size=10, W_range=[0,0], b_range=[0,0]) #y_layer = Sigmoid(prev_layer_size=h2.this_layer_size, this_layer_size=[10,1]) mlp = MLP(input_size=input_size, layers=[h1, output_layer], train_set=[train_setX, train_sety], valid_set=[valid_setX, valid_sety], test_set=[test_set.X, test_set.y], error_function=loglikehood, batch_size=20, learning_rate=0.1) print 'start training' mlp.train() #p = plt.plot(mlp.epoch, mlp.valid_error) #plots.append(p) with open('batches_clean.pkl', 'wb') as bat: cPickle.dump(mlp.epoch, bat) with open('errors_clean.pkl', 'wb') as err: cPickle.dump(mlp.valid_error, err) with open('legends_clean.pkl', 'wb') as leg: cPickle.dump(['2000-tanh'], leg)
def model2(): #pdb.set_trace() # train set X has dim (60,000, 784), y has dim (60,000, 10) train_set = MNIST(which_set='train', one_hot=True) # test set X has dim (10,000, 784), y has dim (10,000, 10) test_set = MNIST(which_set='test', one_hot=True) # =====<Create the MLP Model>===== h1_layer = RectifiedLinear(layer_name='h1', dim=1000, irange=0.5) #print h1_layer.get_params() h2_layer = RectifiedLinear(layer_name='h2', dim=1000, sparse_init=15, max_col_norm=1) y_layer = Softmax(layer_name='y', n_classes=train_set.y.shape[1], irange=0.5) mlp = MLP(batch_size=100, input_space=VectorSpace(dim=train_set.X.shape[1]), layers=[h1_layer, h2_layer, y_layer]) # =====<Create the SGD algorithm>===== sgd = SGD(batch_size=100, init_momentum=0.1, learning_rate=0.01, monitoring_dataset={ 'valid': train_set, 'test': test_set }, cost=SumOfCosts(costs=[ MethodCost('cost_from_X'), WeightDecay(coeffs=[0.00005, 0.00005, 0.00005]) ]), termination_criterion=MonitorBased( channel_name='valid_y_misclass', prop_decrease=0.0001, N=5)) #sgd.setup(model=mlp, dataset=train_set) # =====<Extensions>===== ext = [MomentumAdjustor(start=1, saturate=10, final_momentum=0.99)] # =====<Create Training Object>===== save_path = './mlp_model2.pkl' train_obj = Train(dataset=train_set, model=mlp, algorithm=sgd, extensions=ext, save_path=save_path, save_freq=0) #train_obj.setup_extensions() train_obj.main_loop()
def mnist(): #import pdb #pdb.set_trace() ################### #BUILD THE DATASET# ################### print 'build the dataset' train_set = MNIST(which_set='train') test_set = MNIST(which_set='test') train_setX, valid_setX = split(train_set.X, [50000], axis=0) train_sety, valid_sety = split(train_set.y, [50000], axis=0) #import pdb #pdb.set_trace() ################## #BUILD THE LAYERS# ################## print 'build the layers' input_size = len(train_setX[0]) h1 = NoisyRELU(prev_layer_size=input_size, this_layer_size=1000, threshold=5, noise_factor=1) h2 = NoisyRELU(prev_layer_size=h1.get_size(), this_layer_size=1000, threshold=5, noise_factor=1) #h3 = NoisyRELU(prev_layer_size=h2.get_size(), this_layer_size=1000, threshold=5, noise_factor=1) output_layer = Softmax(prev_layer_size=h1.this_layer_size, this_layer_size=10, W_range=[0,0], b_range=[0,0]) #y_layer = Sigmoid(prev_layer_size=h2.this_layer_size, this_layer_size=[10,1]) mlp = MLP(input_size=input_size, layers=[h1, h2, output_layer], train_set=[train_setX, train_sety], valid_set=[valid_setX, valid_sety], test_set=[test_set.X, test_set.y], error_function=loglikehood, batch_size=20, learning_rate=0.1) print 'start training' mlp.train(save_freq=1)
def test_sample_supervised(idxs=None, noisy=True): """ Visualize samples and labels produced by GSN. """ with open("gsn_sup_example.pkl") as f: gsn = pickle.load(f) gsn._corrupt_switch = noisy ds = MNIST(which_set='test') if idxs is None: data = ds.X[100:150] else: data = ds.X[idxs] # change the walkback parameter to make the data fill up rows in image samples = gsn.get_samples([(0, data)], indices=[0, 2], walkback=21, symbolic=False, include_first=True) stacked = vis_samples(samples) tiled = image.tile_raster_images(stacked, img_shape=[28, 28], tile_shape=[50, 50], tile_spacing=(2, 2)) image.save("gsn_sup_example.png", tiled)
def test_decision_function(self): """ Test DenseMulticlassSVM.decision_function. """ dataset = MNIST(which_set='train') X = dataset.X[0:20, :] y = dataset.y[0:20] for i in xrange(10): assert (y == i).sum() > 0 model = DenseMulticlassSVM(kernel='poly', C=1.0).fit(X, y) f = model.decision_function(X) print(f) yhat_f = np.argmax(f, axis=1) yhat = np.cast[yhat_f.dtype](model.predict(X)) print(yhat_f) print(yhat) assert (yhat_f != yhat).sum() == 0
def test_mnist(): """ Test the mnist.yaml file from the maxout paper on random input """ skip_if_no_gpu() train = load_train_file( os.path.join(pylearn2.__path__[0], "scripts/papers/maxout/mnist.yaml")) # Load fake MNIST data init_value = control.load_data control.load_data = [False] train.dataset = MNIST(which_set='train', one_hot=1, axes=['c', 0, 1, 'b'], start=0, stop=100) train.algorithm._set_monitoring_dataset(train.dataset) control.load_data = init_value # Train shortly and prevent saving train.algorithm.termination_criterion = EpochCounter(max_epochs=1) train.extensions.pop(0) train.save_freq = 0 train.main_loop()
def setup(): """ Create pickle file with a simple model. """ # tearDown is guaranteed to run pop_load_data. control.push_load_data(False) with open('dbm.pkl', 'wb') as f: dataset = MNIST(which_set='train', start=0, stop=100, binarize=True) vis_layer = BinaryVector(nvis=784, bias_from_marginals=dataset) hid_layer1 = BinaryVectorMaxPool(layer_name='h1', pool_size=1, irange=.05, init_bias=-2., detector_layer_dim=50) hid_layer2 = BinaryVectorMaxPool(layer_name='h2', pool_size=1, irange=.05, init_bias=-2., detector_layer_dim=10) model = DBM(batch_size=20, niter=2, visible_layer=vis_layer, hidden_layers=[hid_layer1, hid_layer2]) model.dataset_yaml_src = """ !obj:pylearn2.datasets.binarizer.Binarizer { raw: !obj:pylearn2.datasets.mnist.MNIST { which_set: "train", start: 0, stop: 100 } } """ model.layer_to_chains = model.make_layer_to_state(1) cPickle.dump(model, f, protocol=cPickle.HIGHEST_PROTOCOL)
def try_pylearn2_generator(): dataset = MNIST('train') dataset_generator = Pylearn2OldGenerator(dataset, batch_size=1000, num_batches=-1) for b in dataset_generator: print b.shape assert b.shape == (1000, 784)
def load_dataset(self): # TODO: we might need other variables for identifying what kind of # extra preprocessing was done such as features product and number # of features kept based on MI. #base_path = get_data_path(self.state) #self.base_path = base_path #import pdb #pdb.set_trace() if self.state.dataset == 'mnist': self.test_ddm = MNIST(which_set='test', one_hot=True) dataset = MNIST(which_set='train', shuffle=True, one_hot=True) train_X, valid_X = np.split(dataset.X, [50000]) train_y, valid_y = np.split(dataset.y, [50000]) self.train_ddm = DenseDesignMatrix(X=train_X, y=train_y) self.valid_ddm = DenseDesignMatrix(X=valid_X, y=valid_y) elif self.state.dataset == 'svhn': self.train_ddm = SVHN(which_set='splitted_train') self.test_ddm = SVHN(which_set='test') self.valid_ddm = SVHN(which_set='valid') elif self.state.dataset == 'cifar10': self.train_ddm = My_CIFAR10(which_set='train', one_hot=True) self.test_ddm = None self.valid_ddm = My_CIFAR10(which_set='test', one_hot=True) if self.train_ddm is not None: self.nvis = self.train_ddm.X.shape[1] self.nout = self.train_ddm.y.shape[1] print "nvis, nout :", self.nvis, self.nout self.ntrain = self.train_ddm.X.shape[0] print "ntrain :", self.ntrain if self.valid_ddm is not None: self.nvalid = self.valid_ddm.X.shape[0] print "nvalid :", self.nvalid if self.test_ddm is not None: self.ntest = self.test_ddm.X.shape[0] print "ntest :", self.ntest
def get_valid(ds, limit_size=-1, fold=0): if ds == 'mnist': data = MNIST('train', start=50000, stop=60000) return data.X[:limit_size] elif ds == 'tfd': data = TFD('valid', fold=fold, scale=True) return data.X else: raise ValueError("Unknow dataset: {}".format(args.dataet))
def main(): ################### #BUILD THE DATASET# ################### print 'build the dataset' train_set = MNIST(which_set='train', one_hot=False) test_set = MNIST(which_set='test', one_hot=False) train_setX, valid_setX = split(train_set.X, [50000], axis=0) train_sety, valid_sety = split(train_set.y, [50000], axis=0) #import pdb #pdb.set_trace() ################## #BUILD THE LAYERS# ################## print 'build the layers' input_size = len(train_setX[0]) h1 = NoisyRELU(prev_layer_size=input_size, this_layer_size=200, noise_factor=0, activation_threshold=0) output_layer = Softmax(prev_layer_size=h1.this_layer_size, this_layer_size=10, W_range=[0, 0], b_range=[0, 0]) #y_layer = Sigmoid(prev_layer_size=h2.this_layer_size, this_layer_size=[10,1]) # import pdb # pdb.set_trace() print 'build the model' mlp = MLP(input_size=input_size, layers=[h1, output_layer], train_set=[train_setX, train_sety], valid_set=[valid_setX, valid_sety], test_set=[test_set.X, test_set.y], error_function=loglikehood, batch_size=20) print 'start training' mlp.train_batch(100000)
def setUp(self): skip_if_no_h5py() import h5py skip_if_no_data() from pylearn2.datasets.mnist import MNIST # save MNIST data to HDF5 train = MNIST(which_set='train', one_hot=1, start=0, stop=100) for name, dataset in [('train', train)]: with h5py.File("{}.h5".format(name), "w") as f: f.create_dataset('X', data=dataset.get_design_matrix()) f.create_dataset('topo_view', data=dataset.get_topological_view()) f.create_dataset('y', data=dataset.get_targets()) # instantiate Train object self.train = yaml_parse.load(trainer_yaml)
def get_dim_input(state): if state.dataset == 'mnist': dataset = MNIST(which_set='test') dim = dataset.X.shape[1] elif state.dataset == 'svhn': dataset = SVHN(which_set='test') dim = dataset.X.shape[1] elif state.dataset == 'cifar10': dataset = My_CIFAR10(which_set='test') dim = dataset.X.shape[1] else: raise ValueError( 'only mnist, cifar10 and svhn are supported for now in get_dim_input' ) del dataset return dim
def test_topo_c01b(self): """ Tests that a topological batch with axes ('c',0,1,'b') can be dimshuffled back to match the standard ('b',0,1,'c') format. """ batch_size = 100 c01b_test = MNIST(which_set='test', axes=('c', 0, 1, 'b')) c01b_X = c01b_test.X[0:batch_size,:] c01b = c01b_test.get_topological_view(c01b_X) assert c01b.shape == (1, 28, 28, batch_size) b01c = c01b.transpose(3,1,2,0) b01c_X = self.test.X[0:batch_size,:] assert c01b_X.shape == b01c_X.shape assert np.all(c01b_X == b01c_X) b01c_direct = self.test.get_topological_view(b01c_X) assert b01c_direct.shape == b01c.shape assert np.all(b01c_direct == b01c)
def get_valid(ds, limit_size=-1, fold=0): if ds == 'mnist': data = MNIST('train', start=50000, stop=60000) return data.X[:limit_size] elif ds == 'tfd': data = TFD('valid', fold=fold, scale=True) return data.X elif ds == 'lfwcrop': # HACK return LFW( axes=('c', 0, 1, 'b'), gcn=55, lfw_path= '/afs/cs.stanford.edu/u/jgauthie/scr/lfwcrop_color/faces32', filelist_path= '/afs/cs.stanford.edu/u/jgauthie/scr/lfwcrop_color/filelist.dev.ids.txt', embedding_file= '/afs/cs.stanford.edu/u/jgauthie/scr/lfw-lsa/LFW_attributes_30d.npz', img_shape=(3, 32, 32)).X else: raise ValueError("Unknow dataset: {}".format(args.dataet))
def test_topo_c01b(self): """ Tests that a topological batch with axes ('c',0,1,'b') can be dimshuffled back to match the standard ('b',0,1,'c') format. """ batch_size = 100 # TODO: the one_hot=True is only necessary because one_hot=False is # broken, remove it after one_hot=False is fixed. c01b_test = MNIST(which_set='test', axes=('c', 0, 1, 'b'), one_hot=True) c01b_X = c01b_test.X[0:batch_size,:] c01b = c01b_test.get_topological_view(c01b_X) assert c01b.shape == (1, 28, 28, batch_size) b01c = c01b.transpose(3,1,2,0) b01c_X = self.test.X[0:batch_size,:] assert c01b_X.shape == b01c_X.shape assert np.all(c01b_X == b01c_X) b01c_direct = self.test.get_topological_view(b01c_X) assert b01c_direct.shape == b01c.shape assert np.all(b01c_direct == b01c)
def ais_data(fname, do_exact=True): rbm_params = load_rbm_params(fname) # load data to set visible biases to ML solution from pylearn2.datasets.mnist import MNIST dataset = MNIST(which_set='train', one_hot=True) data = numpy.asarray(dataset.X, dtype=config.floatX) # run ais using B=0 model with ML visible biases t1 = time.time() (logz, log_var_dz), aisobj = \ rbm_tools.rbm_ais(rbm_params, n_runs=100, seed=123, data=data) print 'AIS logZ : %f' % logz print ' log_variance : %f' % log_var_dz print 'Elapsed time: ', time.time() - t1 if do_exact: exact_logz = compute_logz(rbm_params) print 'Exact logZ = %f' % exact_logz numpy.testing.assert_almost_equal(exact_logz, logz, decimal=0)
def get_ddm_mnist(self, ddm_id): row = self.db.executeSQL( """ SELECT which_set, center, shuffle, one_hot, binarize, start, stop, axes FROM hps3.ddm_mnist WHERE ddm_id = %s """, (ddm_id, ), self.db.FETCH_ONE) if not row or row is None: raise HPSData("No cifar100 ddm for ddm_id="\ +str(ddm_id)) (which_set, center, shuffle, one_hot, binarize, start, stop, axes_char) = row axes = self.get_axes(axes_char) return MNIST(which_set=which_set, center=center, shuffle=shuffle, binarize=binarize, axes=axes, start=start, stop=stop, one_hot=one_hot)
def mnist_test(model_paths=["dae_l1.pkl", "dae_l2.pkl", "dae_l3.pkl"], sparse_coef=0, sparse_p=0.1): cost = yaml_parse.load( 'cost : !obj:pylearn2.costs.cost.SumOfCosts {costs: [!obj:pylearn2.costs.autoencoder.MeanSquaredReconstructionError {}, !obj:pylearn2.costs.autoencoder.SparseActivation {coeff: ' + str(sparse_coef) + ',p: ' + str(sparse_p) + '}]}')['cost'] model = load_model(model_paths, cost) DUconfig.dataset = MNIST(which_set='train', start=0, stop=50000) #reconstructed_dataset = represent(DUconfig.dataset,model) #print reconstructed_dataset[0] print recon_errors(DUconfig.dataset.X, model) #reconstruct(model,img_path="mnist7.png",out_path="out7.png") ipaths = ["mnist1.png", "mnist4.png", "mnist5.png", "mnist7.png"] imgs = [] for img in ipaths: i = Image.open(img) imgs.append(np.reshape(i, (1, i.size[0] * i.size[1]))) flatimgs = np.concatenate(imgs) out = decode(encode(flatimgs, model), model) scipy.misc.imsave("out_prime.png", out.reshape([flatimgs.shape[0] * 28, 28])) save_as_patches(show_weights(model), [28, 28], out_path="outpatches.png")
def _get_dataset(dataset, no_imgs): """ Dump machine learning dataset into C source file and create a header file with array declaration and few macro definition """ dataset = dataset.lower() if (dataset not in dataset_factory['name']): print('Dataset is not in the factory') sys.exit() if (dataset == 'mnist'): print('Reading MNIST dataset from pylearn2 database') data = MNIST(which_set='test') if (no_imgs > data.X.shape[0]): cprint( 'Only {:d} images are available in this dataset. Dumping only those many' .format(data.X.shape[0]), 'red') data_x = data.X[0:min(no_imgs, data.X.shape[0])] data_y = data.y[0:min(no_imgs, data.y.shape[0])] data_x = np.uint8(data_x * 255) # conversion from 0 -> 0.99 to 0->255 elif (dataset == 'cifar10'): print('Reading CIFAR-10 dataset from pylearn2 database') data = CIFAR10(which_set='test') if (no_imgs > data.X.shape[0]): cprint( 'Only {:d} images are available in this dataset. Dumping only those many' .format(data.X.shape[0]), 'red') data_x = data.X[0:min(no_imgs, data.X.shape[0])] data_y = data.y[0:min(no_imgs, data.y.shape[0])] data_x = np.uint8(data_x) # already in the range 0 -> 255 elif (dataset == 'svhn'): cprint('Not supported', 'red') return return data_x, data_y
def test_classify(): """ See how well a (supervised) GSN performs at classification. """ with open("gsn_sup_example.pkl") as f: gsn = pickle.load(f) gsn = JointGSN.convert(gsn) # turn off corruption gsn._corrupt_switch = False ds = MNIST(which_set='test') mb_data = ds.X y = ds.y for i in xrange(1, 10): y_hat = gsn.classify(mb_data, trials=i) errors = np.abs(y_hat - y).sum() / 2.0 # error indices #np.sum(np.abs(y_hat - y), axis=1) != 0 print(i, errors, errors / mb_data.shape[0])
def main(): #import pdb #pdb.set_trace() ################### #BUILD THE DATASET# ################### print 'build the dataset' train_set = MNIST(which_set='train', one_hot=False) test_set = MNIST(which_set='test', one_hot=False) train_setX, valid_setX = split(train_set.X, [50000], axis=0) train_sety, valid_sety = split(train_set.y, [50000], axis=0) #import pdb #pdb.set_trace() ################## #BUILD THE LAYERS# ################## print 'build the layers' input_size = len(train_setX[0]) h1 = NoisyRELU(prev_layer_size=input_size, this_layer_size=2000, noise_factor=1, threshold=5) output_layer = Softmax(prev_layer_size=h1.this_layer_size, this_layer_size=10, W_range=[0,0], b_range=[0,0]) #y_layer = Sigmoid(prev_layer_size=h2.this_layer_size, this_layer_size=[10,1]) # import pdb # pdb.set_trace() #, 0.1, 0.5, 0.001, 0.005 plots = [] legends = [] batches = [] errors = [] for learning_rate in [0.01, 0.05]: print 'build the model' print 'learning rate', learning_rate mlp = MLP(input_size=input_size, layers=[h1, output_layer], train_set=[train_setX, train_sety], valid_set=[valid_setX, valid_sety], test_set=[test_set.X, test_set.y], error_function=loglikehood, batch_size=20, learning_rate=learning_rate) print 'start training' mlp.train() #p = plt.plot(mlp.epoch, mlp.valid_error) #plots.append(p) batches.append(mlp.epoch) errors.append(mlp.valid_error) legends.append(learning_rate) with open('batches.pkl', 'wb') as bat: cPickle.dump(batches, bat) with open('errors.pkl', 'wb') as err: cPickle.dump(errors, err) with open('legends.pkl', 'wb') as leg: cPickle.dump(legends, leg)
job_name = 'cfa_test' from pylearn2.datasets.mnist import MNIST from pylearn2.pca import CovEigPCA import theano.tensor as T from theano import function from models import expand from pylearn2.utils import serial import time import SkyNet import gc import numpy as N print 'Loading MNIST train set' t1 = time.time() X = MNIST(which_set='train').get_design_matrix() t2 = time.time() print(t2 - t1), ' seconds' num_examples, input_dim = X.shape print 'Training PCA with %d dimensions' % pca_dim t1 = time.time() pca_model = CovEigPCA(num_components=pca_dim) pca_model.train(X) t2 = time.time() print(t2 - t1), ' seconds' print 'Compiling theano PCA function' t1 = time.time() pca_input = T.matrix()
print("batch_size = "+str(batch_size)) # MLP parameters num_units = 4096 print("num_units = "+str(num_units)) n_hidden_layers = 3 print("n_hidden_layers = "+str(n_hidden_layers)) # kernel = "baseline" kernel = "xnor" # kernel = "theano" print("kernel = "+ kernel) print('Loading MNIST dataset...') test_set = MNIST(which_set= 'test', center = False) # Inputs in the range [-1,+1] test_set.X = 2* test_set.X.reshape(-1, 784) - 1. # flatten targets test_set.y = test_set.y.reshape(-1) print('Building the MLP...') # Prepare Theano variables for inputs and targets input = T.matrix('inputs') target = T.vector('targets') mlp = lasagne.layers.InputLayer(shape=(None, 784),input_var=input) # Input layer is not binary -> use baseline kernel in first hidden layer mlp = binary_ops.DenseLayer(
from pylearn2.costs.cost import MethodCost from pylearn2.datasets.mnist import MNIST from pylearn2.models.mlp import MLP, Sigmoid, Softmax from pylearn2.train import Train from pylearn2.training_algorithms.sgd import SGD from pylearn2.training_algorithms.learning_rule import Momentum, MomentumAdjustor from pylearn2.termination_criteria import EpochCounter train_set = MNIST(which_set='train', start=0, stop=50000) valid_set = MNIST(which_set='train', start=50000, stop=60000) test_set = MNIST(which_set='test') model = MLP(nvis=784, layers=[Sigmoid(layer_name='h', dim=500, irange=0.01), Softmax(layer_name='y', n_classes=10, irange=0.01)]) algorithm = SGD(batch_size=100, learning_rate=0.01, learning_rule=Momentum(init_momentum=0.5), monitoring_dataset={'train': train_set, 'valid': valid_set, 'test': test_set}, cost=MethodCost('cost_from_X'), termination_criterion=EpochCounter(10)) train = Train(dataset=train_set, model=model, algorithm=algorithm, save_path="mnist_example.pkl", save_freq=1, extensions=[MomentumAdjustor(start=5, saturate=6, final_momentum=0.95)]) train.main_loop()
from pylearn2.datasets.mnist import MNIST from pylearn2.utils.image import show def explore(X, y): for i in xrange(X.shape[0]): patch = X[i, :].reshape(28, 28) print y[i] show(patch) x = raw_input('waiting...') if x == 'n': return if x == 'q': quit(-1) print 'training set' train = MNIST(which_set='train') explore(train.X, train.y) print 'test set' test = MNIST(which_set='test') explore(test.X, test.y)
grad = T.grad(ll.mean(), X_shared, disconnected_inputs='warn') updates = OrderedDict() lr = T.scalar('lr') is_noise = sharedX(0., 'is_noise') updates[X_shared] = X_shared + lr * (grad + model.prior.theano_rng.normal(size=X_shared.shape)) updates[X_shared] = T.where(M, X, updates[X_shared]) f = theano.function([X, M, lr], [ll.mean()], updates=updates, allow_input_downcast=True) print 'Compiled training function' # Setup for training and display dataset_yaml_src = model.dataset_yaml_src train_set = yaml_parse.load(dataset_yaml_src) test_set = MNIST(which_set='test') dataset = train_set num_samples = n_examples vis_batch = dataset.get_batch_topo(num_samples) rval = tuple(vis_batch.shape[dataset.X_topo_space.axes.index(axis)] for axis in ('b', 0, 1, 'c')) _, patch_rows, patch_cols, channels = rval mapback = hasattr(dataset, 'mapback_for_viewer') pv = PatchViewer((rows, cols*(1+mapback)), (patch_rows, patch_cols), is_color=(channels == 3)) # Get examples and masks x_val = test_set.get_batch_design(num_samples)
def setUp(self): skip_if_no_data() self.train = MNIST(which_set = 'train') self.test = MNIST(which_set = 'test')