@@ -0,0 +1,3079 @@
+%22%22%22%0AThis script is a starting point for new Blocks users already familiar with%0A Machine Learning and Theano.%0A%0AWe demonstrate how to use blocks to train a generic set of parameters (theano%0A shared variables) that influence some arbitrary cost function (theano%0A symbolic variable), so you can start using blocks features (e.g. monitoring,%0A extensions, training algorithms) with your Theano code today.%0A%0ATo run an experiment, we simply construct a main_loop.MainLoop and call its%0A run() method.  It suffices to pass the MainLoop a blocks.model.Model%0A (which needs the cost), a blocks.algorithms.TrainingAlgorithm (which needs the%0A cost and parameters), and a fuel.streams.DataStream*%0A%0AAs it is the script will run indefinitely, with no output.  You can interrupt %0A training training anytime with Ctrl+C, or termination conditions can be added%0A via extensions.%0A%0A*The DataStream object is part of the partner library Fuel%0A (https://github.com/mila-udem/fuel).%0A%22%22%22%0A%0Aimport numpy%0Anp = numpy%0Aimport theano%0Aimport theano.tensor as T%0A%0A# Define %22data_stream%22 (TODO: understand and simplify (?))%0A# (We use a toy dataset of two 2D gaussians with different means.)%0Anum_examples = 1000%0Abatch_size = 100%0Ameans = np.array(%5B%5B-1., -1.%5D, %5B1, 1%5D%5D)%0Astd = 0.5%0Alabels = np.random.randint(size=num_examples, low=0, high=1)%0Afeatures = means%5Blabels, :%5D + std * np.random.normal(size=(num_examples, 2))%0Alabels = labels.reshape((num_examples, 1)).astype(theano.config.floatX)%0Afeatures = features.astype(theano.config.floatX)%0Afrom collections import OrderedDict%0Afrom fuel.datasets import IndexableDataset%0A# The names here (e.g. 'name1') need to match the names of the variables which%0A#  are the leaves of the computational graph for the cost.%0Adataset = IndexableDataset(%0A              OrderedDict(%5B('name1', features), ('name2', labels)%5D))%0Afrom fuel.streams import DataStream, ForceFloatX%0Afrom fuel.schemes import SequentialScheme%0Adata_stream = ForceFloatX(DataStream(dataset,%0A                                     iteration_scheme=SequentialScheme(%0A                                       dataset.num_examples, batch_size)))%0A%0A%0A# Define %22cost%22 and %22parameters%22%0A# (We use logistic regression)%0Ainputs = T.matrix('name1')%0Atargets = T.matrix('name2')%0Aninp, nout = 2, 1%0AW = theano.shared(.01*np.random.uniform(%0A                        size=((ninp, nout))).astype(theano.config.floatX))%0Ab = theano.shared(np.zeros(nout).astype(theano.config.floatX))%0Aoutput = T.nnet.sigmoid(T.dot(inputs, W) + b)%0A# a theano symbolic expression%0Acost = T.mean(T.nnet.binary_crossentropy(output, targets))%0A# a list of theano.shared variables%0Aparameters = %5BW, b%5D%0A%0A%0A# wrap everything in Blocks objects and run!%0Afrom blocks.model import Model%0Amodel = Model(%5Bcost%5D)%0Afrom blocks.algorithms import GradientDescent, Scale%0Aalgorithm = GradientDescent(cost=cost,%0A                            parameters=parameters,%0A                            step_rule=Scale(learning_rate=.01))%0Afrom blocks.main_loop import MainLoop%0Amy_loop = MainLoop(model=model,%0A                   data_stream=data_stream,%0A                   algorithm=algorithm)%0Amy_loop.run()%0A

add script