code: def test_known_grads():
      # Tests that the grad method with no known_grads
      # matches what happens if you put its own known_grads
      # in for each variable
  
      full_range = theano.tensor.arange(10)
      x = theano.tensor.scalar("x")
      t = theano.tensor.iscalar("t")
      ft = full_range[t]
      ft.name = "ft"
      coeffs = theano.tensor.vector("c")
      ct = coeffs[t]
      ct.name = "ct"
      p = x**ft
      p.name = "p"
      y = ct * p
      y.name = "y"
      cost = theano.tensor.sqr(y)
      cost.name = "cost"
  
      layers = [
          [cost],
          [y],
          [ct, p],
          [ct, x, ft],
          [coeffs, t, full_range, x],
      ]
  
      inputs = [coeffs, t, x]
  
      rng = np.random.RandomState([2012, 11, 15])
      values = [rng.randn(10), rng.randint(10), rng.randn()]
      values = [
          np.cast[ipt.dtype](value)
          for ipt, value in zip(inputs, values)
      ]
  
      true_grads = theano.tensor.grad(
          cost, inputs, disconnected_inputs="ignore"
      )
      true_grads = theano.function(inputs, true_grads)
      true_grads = true_grads(*values)
  
      for layer in layers:
          first = theano.tensor.grad(
              cost, layer, disconnected_inputs="ignore"
          )
          known = OrderedDict(izip(layer, first))
          full = theano.tensor.grad(
              cost=None,
              known_grads=known,
              wrt=inputs,
              disconnected_inputs="ignore",
          )
          full = theano.function(inputs, full)
          full = full(*values)
          assert len(true_grads) == len(full)
          for a, b, var in zip(true_grads, full, inputs):
              if not np.allclose(a, b):
                  print("Failure")
                  print(a)
                  print(b)
                  print(var)
                  print(layer)
                  for v in known:
                      print(
                          v,
                          ":",
                          theano.function(inputs, known[v])(*values),
                      )
                  assert False
  
imports: from __future__ import absolute_import, print_function, division
  from collections import OrderedDict
  import unittest
  import numpy
  from six.moves import xrange
  import theano
  from theano import gof, change_flags
  from theano.compat import izip
  from theano.tests import unittest_tools
  from theano import gradient
  from theano import config
  from theano.gof.null_type import NullType
  from theano.sandbox.rng_mrg import MRG_RandomStreams
label_desc: Use image node_id 2 for calling a global function with example usage: test_known_grads() without return types