class_docstr: Test case for the PushOutScanOutput optimizer in the case where the scan
  is used to compute the sum over the dot products between the corresponding
  elements of two list of matrices.
class_docstr_tok: ["Test","case","for","the","PushOutScanOutput","optimizer","in","the","case","where","the","scan","is","used","to","compute","the","sum","over","the","dot","products","between","the","corresponding","elements","of","two","list","of","matrices","."]
code: def test_machine_translation(self):
      # This test case comes from https://github.com/rizar/scan-grad-speed and
      # is an example of actual computation done with scan in the context of
      # machine translation
      #
      # 'dim' has been reduced from 1000 to 5 to make the test run faster
  
      # Parameters from an actual machine tranlation run
      batch_size = 80
      seq_len = 50
      n_words = 80 * 50
      dim = 5
  
      # Weight matrices
      U = theano.shared(
          np.random.normal(size=(dim, dim), scale=0.0001).astype(
              config.floatX
          )
      )
      U.name = "U"
      V = theano.shared(U.get_value())
      V.name = "V"
      W = theano.shared(U.get_value())
      W.name = "W"
  
      # Variables and their values
      x = T.tensor3("x")
      x_value = np.random.normal(
          size=(seq_len, batch_size, dim), scale=0.0001
      ).astype(config.floatX)
  
      ri = T.tensor3("ri")
      ri_value = x_value
  
      zi = T.tensor3("zi")
      zi_value = x_value
  
      init = T.alloc(np.cast[config.floatX](0), batch_size, dim)
  
      def rnn_step1(
          # sequences
          x,
          ri,
          zi,
          # outputs_info
          h,
      ):
          pre_r = ri + h.dot(U)
          pre_z = zi + h.dot(V)
          r = T.nnet.sigmoid(pre_r)
          z = T.nnet.sigmoid(pre_z)
  
          after_r = r * h
          pre_h = x + after_r.dot(W)
          new_h = T.tanh(pre_h)
  
          res_h = z * new_h + (1 - z) * h
          return res_h
  
      # Compile the function twice, once with the optimization and once
      # without
      opt_mode = mode.including("scan")
      h, _ = theano.scan(
          rnn_step1,
          sequences=[x, ri, zi],
          n_steps=seq_len,
          outputs_info=init,
          name="fpass1",
          mode=opt_mode,
      )
      cost = h[-1].sum()
      grad1 = T.grad(cost, [U, V, W])
      f_opt = theano.function(
          inputs=[x, ri, zi], outputs=grad1, mode=opt_mode
      )
  
      no_opt_mode = mode.excluding("scanOp_pushout_output")
      h, _ = theano.scan(
          rnn_step1,
          sequences=[x, ri, zi],
          n_steps=seq_len,
          outputs_info=init,
          name="fpass1",
          mode=no_opt_mode,
      )
      cost = h[-1].sum()
      grad1 = T.grad(cost, [U, V, W])
      f_no_opt = theano.function(
          inputs=[x, ri, zi], outputs=grad1, mode=no_opt_mode
      )
  
      # Validate that the optimization has been applied
      scan_node_grad = [
          node
          for node in f_opt.maker.fgraph.toposort()
          if isinstance(node.op, Scan)
      ][1]
  
      for output in scan_node_grad.op.outputs:
          assert not (
              isinstance(output.owner.op, T.elemwise.Elemwise)
              and any(
                  [isinstance(i, T.Dot) for i in output.owner.inputs]
              )
          )
  
      # Compare the outputs of the two functions on the same input data.
      f_opt_output = f_opt(x_value, ri_value, zi_value)
      f_no_opt_output = f_no_opt(x_value, ri_value, zi_value)
      utt.assert_allclose(f_opt_output, f_no_opt_output)
  
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import unittest
  import theano
  from theano import config
  from theano import tensor
  from theano.scan_module.scan_op import Scan
  from theano.tests import unittest_tools
label_desc: Use image node_id 1 for calling the TestPushOutSumOfDot obj's underlying member method code with example usage: obj.test_machine_translation() and returns: res_h