code: def test_mlp():
      """
      Demonstrate stochastic gradient descent optimization for a multilayer
      perceptron
  
      This is demonstrated on MNIST.
  
      :type learning_rate: float
      :param learning_rate: learning rate used (factor for the stochastic
      gradient
  
      :type n_epochs: int
      :param n_epochs: maximal number of epochs to run the optimizer
  
      :type dataset: string
      :param dataset: the path of the MNIST dataset file from
                           http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
  
  
      """
      datasets = gen_data()
  
      train_set_x, train_set_y = datasets[0]
      valid_set_x, valid_set_y = datasets[1]
      test_set_x, test_set_y = datasets[2]
  
      batch_size = 100  # size of the minibatch
  
      # compute number of minibatches for training, validation and testing
      n_train_batches = (
          train_set_x.get_value(borrow=True).shape[0] / batch_size
      )
      n_valid_batches = (
          valid_set_x.get_value(borrow=True).shape[0] / batch_size
      )
      n_test_batches = (
          test_set_x.get_value(borrow=True).shape[0] / batch_size
      )
  
      ######################
      # BUILD ACTUAL MODEL #
      ######################
      # print '... building the model'
  
      # allocate symbolic variables for the data
      index = T.lscalar()  # index to a [mini]batch
      x = T.matrix("x")  # the data is presented as rasterized images
      y = T.ivector("y")  # the labels are presented as 1D vector of
      # [int] labels
  
      rng = np.random.RandomState(1234)
  
      # construct the MLP class
      classifier = MLP(
          rng=rng, input=x, n_in=28 * 28, n_hidden=500, n_out=10
      )
  
      # the cost we minimize during training is the negative log likelihood of
      # the model.
      # We take the mean of the cost over each minibatch.
      cost = classifier.negative_log_likelihood(y).mean()
  
      # compute the gradient of cost with respect to theta (stored in params)
      # the resulting gradients will be stored in a list gparams
      gparams = []
      for param in classifier.params:
          gparam = T.grad(cost, param)
          gparams.append(gparam)
  
      # Some optimizations needed are tagged with 'fast_run'
      # TODO: refine that and include only those
      mode = theano.compile.get_default_mode().including("fast_run")
  
      updates2 = OrderedDict()
  
      updates2[classifier.hiddenLayer.params[0]] = T.grad(
          cost, classifier.hiddenLayer.params[0]
      )
      train_model = theano.function(
          inputs=[index],
          updates=updates2,
          givens={
              x: train_set_x[
                  index * batch_size : (index + 1) * batch_size
              ],
              y: train_set_y[
                  index * batch_size : (index + 1) * batch_size
              ],
          },
          mode=mode,
      )
      # print 'MODEL 1'
      # theano.printing.debugprint(train_model, print_type=True)
      assert any(
          [
              isinstance(i.op, T.nnet.CrossentropySoftmax1HotWithBiasDx)
              for i in train_model.maker.fgraph.toposort()
          ]
      )
  
      # Even without FeatureShape
      train_model = theano.function(
          inputs=[index],
          updates=updates2,
          mode=mode.excluding("ShapeOpt"),
          givens={
              x: train_set_x[
                  index * batch_size : (index + 1) * batch_size
              ],
              y: train_set_y[
                  index * batch_size : (index + 1) * batch_size
              ],
          },
      )
      # print
      # print 'MODEL 2'
      # theano.printing.debugprint(train_model, print_type=True)
      assert any(
          [
              isinstance(i.op, T.nnet.CrossentropySoftmax1HotWithBiasDx)
              for i in train_model.maker.fgraph.toposort()
          ]
      )
  
file_path: Theano/theano/tensor/tests/mlp_test.py
imports: from __future__ import absolute_import, print_function, division
  from collections import OrderedDict
  import numpy
  import theano
  import theano.tensor
label_desc: Use image node_id 2 for calling a global function with example usage: test_mlp() without return types