jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def look_at(eye, center, world_up):
  """Computes camera viewing matrices.

  Functionality mimes gluLookAt (third_party/GL/glu/include/GLU/glu.h).

  Args:
    eye: 2-D float32 tensor with shape [batch_size, 3] containing the XYZ world
        space position of the camera.
    center: 2-D float32 tensor with shape [batch_size, 3] containing a position
        along the center of the camera's gaze.
    world_up: 2-D float32 tensor with shape [batch_size, 3] specifying the
        world's up direction; the output camera will have no tilt with respect
        to this direction.

  Returns:
    A [batch_size, 4, 4] float tensor containing a right-handed camera
    extrinsics matrix that maps points from world space to points in eye space.
  """
  batch_size = center.shape[0].value
  vector_degeneracy_cutoff = 1e-6
  forward = center - eye
  forward_norm = tf.norm(forward, ord='euclidean', axis=1, keepdims=True)
  #tf.assert_greater(
  #    forward_norm,
  #    vector_degeneracy_cutoff,
  #    message='Camera matrix is degenerate because eye and center are close.')
  forward = tf.truediv(forward, forward_norm)

  to_side = tf.linalg.cross(forward, world_up)
  to_side_norm = tf.norm(to_side, ord='euclidean', axis=1, keepdims=True)
  #tf.assert_greater(
  #    to_side_norm,
  #    vector_degeneracy_cutoff,
  #    message='Camera matrix is degenerate because up and gaze are close or'
  #    'because up is degenerate.')
  to_side = tf.truediv(to_side, to_side_norm)
  cam_up = tf.linalg.cross(to_side, forward)

  w_column = tf.constant(
      batch_size * [[0., 0., 0., 1.]], dtype=tf.float32)  # [batch_size, 4]
  w_column = tf.reshape(w_column, [batch_size, 4, 1])
  view_rotation = tf.stack(
      [to_side, cam_up, -forward,
       tf.zeros_like(to_side, dtype=tf.float32)],
      axis=1)  # [batch_size, 4, 3] matrix
  view_rotation = tf.concat(
      [view_rotation, w_column], axis=2)  # [batch_size, 4, 4]

  identity_batch = tf.tile(tf.expand_dims(tf.eye(3), 0), [batch_size, 1, 1])
  view_translation = tf.concat([identity_batch, tf.expand_dims(-eye, 2)], 2)
  view_translation = tf.concat(
      [view_translation,
       tf.reshape(w_column, [batch_size, 1, 4])], 1)
  camera_matrices = tf.matmul(view_rotation, view_translation)
  return camera_matrices
//  [python]
def look_at(eye, center, world_up):
  """Computes camera viewing matrices.

  Functionality mimes gluLookAt (third_party/GL/glu/include/GLU/glu.h).

  Args:
    eye: 2-D float32 tensor with shape [batch_size, 3] containing the XYZ world
        space position of the camera.
    center: 2-D float32 tensor with shape [batch_size, 3] containing a position
        along the center of the camera's gaze.
    world_up: 2-D float32 tensor with shape [batch_size, 3] specifying the
        world's up direction; the output camera will have no tilt with respect
        to this direction.

  Returns:
    A [batch_size, 4, 4] float tensor containing a right-handed camera
    extrinsics matrix that maps points from world space to points in eye space.
  """
  batch_size = center.shape[0].value
  vector_degeneracy_cutoff = 1e-6
  forward = center - eye
  forward_norm = tf.norm(forward, ord='euclidean', axis=1, keepdims=True)
  #tf.assert_greater(
  #    forward_norm,
  #    vector_degeneracy_cutoff,
  #    message='Camera matrix is degenerate because eye and center are close.')
  forward = tf.divide(forward, forward_norm)

  to_side = tf.linalg.cross(forward, world_up)
  to_side_norm = tf.norm(to_side, ord='euclidean', axis=1, keepdims=True)
  #tf.assert_greater(
  #    to_side_norm,
  #    vector_degeneracy_cutoff,
  #    message='Camera matrix is degenerate because up and gaze are close or'
  #    'because up is degenerate.')
  to_side = tf.divide(to_side, to_side_norm)
  cam_up = tf.linalg.cross(to_side, forward)

  w_column = tf.constant(
      batch_size * [[0., 0., 0., 1.]], dtype=tf.float32)  # [batch_size, 4]
  w_column = tf.reshape(w_column, [batch_size, 4, 1])
  view_rotation = tf.stack(
      [to_side, cam_up, -forward,
       tf.zeros_like(to_side, dtype=tf.float32)],
      axis=1)  # [batch_size, 4, 3] matrix
  view_rotation = tf.concat(
      [view_rotation, w_column], axis=2)  # [batch_size, 4, 4]

  identity_batch = tf.tile(tf.expand_dims(tf.eye(3), 0), [batch_size, 1, 1])
  view_translation = tf.concat([identity_batch, tf.expand_dims(-eye, 2)], 2)
  view_translation = tf.concat(
      [view_translation,
       tf.reshape(w_column, [batch_size, 1, 4])], 1)
  camera_matrices = tf.matmul(view_rotation, view_translation)
  return camera_matrices
//  [python]
def test_prefetch_tensors_with_fully_defined_shapes(self):
    with self.test_session() as sess:
      batch_size = 10
      image_size = 32
      num_batches = 5
      examples = tf.Variable(tf.constant(0, dtype=tf.int64))
      counter = examples.count_up_to(num_batches)
      image = tf.random_normal([batch_size, image_size,
                                image_size, 3],
                               dtype=tf.float32,
                               name='images')
      label = tf.random_uniform([batch_size, 1], 0, 10,
                                dtype=tf.int32, name='labels')

      prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter,
                                                        'image': image,
                                                        'label': label},
                                           capacity=100)
      tensor_dict = prefetch_queue.dequeue()

      self.assertAllEqual(tensor_dict['image'].get_shape().as_list(),
                          [batch_size, image_size, image_size, 3])
      self.assertAllEqual(tensor_dict['label'].get_shape().as_list(),
                          [batch_size, 1])

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        for _ in range(num_batches):
          results = sess.run(tensor_dict)
          self.assertEquals(results['image'].shape,
                            (batch_size, image_size, image_size, 3))
          self.assertEquals(results['label'].shape, (batch_size, 1))
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(tensor_dict)
//  [python]
def test_prefetch_tensors_with_fully_defined_shapes(self):
    with self.test_session() as sess:
      batch_size = 10
      image_size = 32
      num_batches = 5
      examples = tf.Variable(tf.constant(0, dtype=tf.int64))
      counter = examples.count_up_to(num_batches)
      image = tf.random_normal([batch_size, image_size,
                                image_size, 3],
                               dtype=tf.float32,
                               name='images')
      label = tf.random_uniform([batch_size, 1], 0, 10,
                                dtype=tf.int32, name='labels')

      prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter,
                                                        'image': image,
                                                        'label': label},
                                           capacity=100)
      tensor_dict = prefetch_queue.dequeue()

      self.assertAllEqual(tensor_dict['image'].get_shape().as_list(),
                          [batch_size, image_size, image_size, 3])
      self.assertAllEqual(tensor_dict['label'].get_shape().as_list(),
                          [batch_size, 1])

      tf.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        for _ in range(num_batches):
          results = sess.run(tensor_dict)
          self.assertEquals(results['image'].shape,
                            (batch_size, image_size, image_size, 3))
          self.assertEquals(results['label'].shape, (batch_size, 1))
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(tensor_dict)
//  [python]
def testCapture(self):
    g = tf.Graph()
    with g.as_default():
      w = tf.Variable(tf.constant([[1.0]]))
      b = tf.Variable(tf.constant([2.0]))

      # Foo() captures w and b.
      @function.Defun(tf.float32)
      def Foo(x):

        # Plus() captures b.
        @function.Defun(tf.float32)
        def Plus(y):
          return y + b

        return Plus(tf.matmul(w, x))

      y = Foo(tf.constant([[10.]]))

    with self.test_session(graph=g):
      tf.initialize_all_variables().run()
      self.assertAllEqual(y.eval(), [[12.0]])
//  [python]
def testCapture(self):
    g = tf.Graph()
    with g.as_default():
      w = tf.Variable(tf.constant([[1.0]]))
      b = tf.Variable(tf.constant([2.0]))

      # Foo() captures w and b.
      @function.Defun(tf.float32)
      def Foo(x):

        # Plus() captures b.
        @function.Defun(tf.float32)
        def Plus(y):
          return y + b

        return Plus(tf.matmul(w, x))

      y = Foo(tf.constant([[10.]]))

    with self.test_session(graph=g):
      tf.global_variables_initializer().run()
      self.assertAllEqual(y.eval(), [[12.0]])
//  [python]
def test_get_synaptic_update_forward(self):
    tf.reset_default_graph()
    inp, out, w = random_dense()
    env = blur_env.tf_env
    pre, post, synapse, genome, _, = get_blur_state(env, inp, out, w)

    _, update_fwd = blur.get_synaptic_update(
        pre,
        post,
        synapse,
        input_transform_gn=genome.neuron.transform,
        update_type=synapse_util.UpdateType.FORWARD,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    inp_with_bias = tf.concat([inp[Ellipsis, 0], [[[[1]]]]], axis=-1)
    exp_results = inp_with_bias @ ww

    with tf.Session() as s:
      s.run(tf.initialize_all_variables())
      self.assertAllClose(update_fwd[Ellipsis, 0], exp_results)
      self.assertAllClose(update_fwd[Ellipsis, 1], exp_results)
//  [python]
def test_get_synaptic_update_forward(self):
    tf.reset_default_graph()
    inp, out, w = random_dense()
    env = blur_env.tf_env
    pre, post, synapse, genome, _, = get_blur_state(env, inp, out, w)

    _, update_fwd = blur.get_synaptic_update(
        pre,
        post,
        synapse,
        input_transform_gn=genome.neuron.transform,
        update_type=synapse_util.UpdateType.FORWARD,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    inp_with_bias = tf.concat([inp[Ellipsis, 0], [[[[1]]]]], axis=-1)
    exp_results = inp_with_bias @ ww

    with tf.Session() as s:
      s.run(tf.global_variables_initializer())
      self.assertAllClose(update_fwd[Ellipsis, 0], exp_results)
      self.assertAllClose(update_fwd[Ellipsis, 1], exp_results)
//  [python]
def test_prefetch_tensors_with_partially_defined_shapes(self):
    with self.test_session() as sess:
      batch_size = 10
      image_size = 32
      num_batches = 5
      examples = tf.Variable(tf.constant(0, dtype=tf.int64))
      counter = examples.count_up_to(num_batches)
      image = tf.random_normal([batch_size,
                                tf.Variable(image_size),
                                tf.Variable(image_size), 3],
                               dtype=tf.float32,
                               name='image')
      image.set_shape([batch_size, None, None, 3])
      label = tf.random_uniform([batch_size, tf.Variable(1)], 0,
                                10, dtype=tf.int32, name='label')
      label.set_shape([batch_size, None])

      prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter,
                                                        'image': image,
                                                        'label': label},
                                           capacity=100)
      tensor_dict = prefetch_queue.dequeue()

      self.assertAllEqual(tensor_dict['image'].get_shape().as_list(),
                          [batch_size, None, None, 3])
      self.assertAllEqual(tensor_dict['label'].get_shape().as_list(),
                          [batch_size, None])

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        for _ in range(num_batches):
          results = sess.run(tensor_dict)
          self.assertEquals(results['image'].shape,
                            (batch_size, image_size, image_size, 3))
          self.assertEquals(results['label'].shape, (batch_size, 1))
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(tensor_dict)
//  [python]
def test_prefetch_tensors_with_partially_defined_shapes(self):
    with self.test_session() as sess:
      batch_size = 10
      image_size = 32
      num_batches = 5
      examples = tf.Variable(tf.constant(0, dtype=tf.int64))
      counter = examples.count_up_to(num_batches)
      image = tf.random_normal([batch_size,
                                tf.Variable(image_size),
                                tf.Variable(image_size), 3],
                               dtype=tf.float32,
                               name='image')
      image.set_shape([batch_size, None, None, 3])
      label = tf.random_uniform([batch_size, tf.Variable(1)], 0,
                                10, dtype=tf.int32, name='label')
      label.set_shape([batch_size, None])

      prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter,
                                                        'image': image,
                                                        'label': label},
                                           capacity=100)
      tensor_dict = prefetch_queue.dequeue()

      self.assertAllEqual(tensor_dict['image'].get_shape().as_list(),
                          [batch_size, None, None, 3])
      self.assertAllEqual(tensor_dict['label'].get_shape().as_list(),
                          [batch_size, None])

      tf.compat.v1.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        for _ in range(num_batches):
          results = sess.run(tensor_dict)
          self.assertEquals(results['image'].shape,
                            (batch_size, image_size, image_size, 3))
          self.assertEquals(results['label'].shape, (batch_size, 1))
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(tensor_dict)
//  [python]
def test_get_synaptic_update_backward(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, _ = get_blur_state(env, inp, out, w)

    update_bwd, _ = blur.get_synaptic_update(
        pre,
        post,
        synapse,
        input_transform_gn=genome.neuron.transform,
        update_type=synapse_util.UpdateType.BACKWARD,
        env=env)

    out = tf.constant(out.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    exp_results = out[Ellipsis, 0] @ tf.transpose(ww, (0, 1, 3, 2))

    with tf.Session() as s:
      s.run(tf.initialize_all_variables())
      self.assertAllClose(update_bwd[Ellipsis, 0], tf.zeros((1, 1, 1, n_in)))
      self.assertAllClose(update_bwd[Ellipsis, 1], exp_results[Ellipsis, :-1])
//  [python]
def test_get_synaptic_update_backward(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, _ = get_blur_state(env, inp, out, w)

    update_bwd, _ = blur.get_synaptic_update(
        pre,
        post,
        synapse,
        input_transform_gn=genome.neuron.transform,
        update_type=synapse_util.UpdateType.BACKWARD,
        env=env)

    out = tf.constant(out.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    exp_results = out[Ellipsis, 0] @ tf.transpose(ww, (0, 1, 3, 2))

    with tf.Session() as s:
      s.run(tf.global_variables_initializer())
      self.assertAllClose(update_bwd[Ellipsis, 0], tf.zeros((1, 1, 1, n_in)))
      self.assertAllClose(update_bwd[Ellipsis, 1], exp_results[Ellipsis, :-1])
//  [python]
def _testSimpleModel(self, use_forward_func):

    def _Model(x):
      w = tf.get_variable(
          "w", (64, 64), initializer=tf.random_uniform_initializer(seed=312))
      b = tf.get_variable("b", (64), initializer=tf.zeros_initializer),
      return tf.sigmoid(tf.matmul(x, w) + b)

    @function.Defun()
    def Model(x):
      return _Model(x)

    cvars = []

    @function.Defun()
    def Grad(x, y0):
      if use_forward_func:
        y = Model(x)
      else:
        y = _Model(x)
      loss = tf.reduce_mean(tf.reduce_sum(y0 * tf.log(y), 1), 0)
      dw, db = tf.gradients(loss, function.get_extra_args())
      cvars.extend(function.get_extra_vars())
      return loss, dw, db

    g = tf.Graph()
    with g.as_default():
      x = tf.random_normal([64, 64], seed=100)
      y0 = tf.random_normal([64, 64], seed=200)
      with tf.variable_scope("Foo"):
        loss, dw, db = Grad(x, y0)

    self.assertEqual(2, len(cvars))
    w, b = cvars[:2]
    self.assertEqual("Foo/w", w.op.name)
    self.assertEqual("Foo/b", b.op.name)

    with self.test_session(graph=g) as sess:
      sess.run(tf.initialize_all_variables())
      w, b, x, y0, loss, dw, db = sess.run([w, b, x, y0, loss, dw, db])

    self.assertAllEqual(w.shape, (64, 64))
    self.assertAllClose(np.sum(w), 2050.44)
    self.assertAllEqual(b.shape, (64,))
    self.assertAllClose(np.sum(b), 0.0)
    self.assertAllClose(loss, -2.27, rtol=1e-2)
    self.assertAllEqual(dw.shape, (64, 64))
    self.assertAllClose(np.sum(dw), -1.04, rtol=1e-2)
    self.assertAllEqual(db.shape, (64,))
    self.assertAllClose(np.sum(db), 0.509, rtol=1e-2)
//  [python]
def _testSimpleModel(self, use_forward_func):

    def _Model(x):
      w = tf.get_variable(
          "w", (64, 64), initializer=tf.random_uniform_initializer(seed=312))
      b = tf.get_variable("b", (64), initializer=tf.zeros_initializer),
      return tf.sigmoid(tf.matmul(x, w) + b)

    @function.Defun()
    def Model(x):
      return _Model(x)

    cvars = []

    @function.Defun()
    def Grad(x, y0):
      if use_forward_func:
        y = Model(x)
      else:
        y = _Model(x)
      loss = tf.reduce_mean(tf.reduce_sum(y0 * tf.log(y), 1), 0)
      dw, db = tf.gradients(loss, function.get_extra_args())
      cvars.extend(function.get_extra_vars())
      return loss, dw, db

    g = tf.Graph()
    with g.as_default():
      x = tf.random_normal([64, 64], seed=100)
      y0 = tf.random_normal([64, 64], seed=200)
      with tf.variable_scope("Foo"):
        loss, dw, db = Grad(x, y0)

    self.assertEqual(2, len(cvars))
    w, b = cvars[:2]
    self.assertEqual("Foo/w", w.op.name)
    self.assertEqual("Foo/b", b.op.name)

    with self.test_session(graph=g) as sess:
      sess.run(tf.global_variables_initializer())
      w, b, x, y0, loss, dw, db = sess.run([w, b, x, y0, loss, dw, db])

    self.assertAllEqual(w.shape, (64, 64))
    self.assertAllClose(np.sum(w), 2050.44)
    self.assertAllEqual(b.shape, (64,))
    self.assertAllClose(np.sum(b), 0.0)
    self.assertAllClose(loss, -2.27, rtol=1e-2)
    self.assertAllEqual(dw.shape, (64, 64))
    self.assertAllClose(np.sum(dw), -1.04, rtol=1e-2)
    self.assertAllEqual(db.shape, (64,))
    self.assertAllClose(np.sum(db), 0.509, rtol=1e-2)
//  [python]
def test_neuron_update_fwd(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, network_spec = get_blur_state(env, inp, out, w)
    pre_fwd, post_fwd = blur.dense_neuron_update(
        pre,
        post,
        synapse,
        inp_act=None,
        out_act=sigmoid_with_grad,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.FORWARD,
        global_spec=network_spec,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    inp_with_bias = tf.concat([inp[Ellipsis, 0], [[[[1]]]]], axis=-1)
    exp_results = inp_with_bias @ ww

    with tf.Session() as s:
      s.run(tf.initialize_all_variables())
      self.assertAllClose(pre_fwd, pre)
      self.assertAllClose(post_fwd[Ellipsis, 0], tf.math.sigmoid(exp_results))
      self.assertAllClose(post_fwd[Ellipsis, 1],
                          d_sigmoid(out[Ellipsis, 0] + exp_results))
//  [python]
def test_neuron_update_fwd(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, network_spec = get_blur_state(env, inp, out, w)
    pre_fwd, post_fwd = blur.dense_neuron_update(
        pre,
        post,
        synapse,
        inp_act=None,
        out_act=sigmoid_with_grad,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.FORWARD,
        global_spec=network_spec,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)
    inp_with_bias = tf.concat([inp[Ellipsis, 0], [[[[1]]]]], axis=-1)
    exp_results = inp_with_bias @ ww

    with tf.Session() as s:
      s.run(tf.global_variables_initializer())
      self.assertAllClose(pre_fwd, pre)
      self.assertAllClose(post_fwd[Ellipsis, 0], tf.math.sigmoid(exp_results))
      self.assertAllClose(post_fwd[Ellipsis, 1],
                          d_sigmoid(out[Ellipsis, 0] + exp_results))
//  [python]
def test_get_hebbian_update(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, _, genome, network_spec = get_blur_state(env, inp, out, w)

    hebbian_update = blur.get_hebbian_update(pre, post,
                                             genome.synapse.transform,
                                             network_spec, env)
    hebbian_update_submatrix = synapse_util.synapse_submatrix(
        hebbian_update, n_in, synapse_util.UpdateType.FORWARD)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    out = tf.constant(out.astype(blur_env.NP_FLOATING_TYPE))
    inp_transpose = tf.transpose(inp[Ellipsis, 0], (0, 1, 3, 2))
    exp_result = env.concat_row(inp_transpose) @ out[Ellipsis, 0]

    with tf.Session() as s:
      s.run(tf.initialize_all_variables())
      self.assertAllClose(hebbian_update_submatrix[Ellipsis, 0], exp_result)
//  [python]
def test_get_hebbian_update(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, _, genome, network_spec = get_blur_state(env, inp, out, w)

    hebbian_update = blur.get_hebbian_update(pre, post,
                                             genome.synapse.transform,
                                             network_spec, env)
    hebbian_update_submatrix = synapse_util.synapse_submatrix(
        hebbian_update, n_in, synapse_util.UpdateType.FORWARD)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    out = tf.constant(out.astype(blur_env.NP_FLOATING_TYPE))
    inp_transpose = tf.transpose(inp[Ellipsis, 0], (0, 1, 3, 2))
    exp_result = env.concat_row(inp_transpose) @ out[Ellipsis, 0]

    with tf.Session() as s:
      s.run(tf.global_variables_initializer())
      self.assertAllClose(hebbian_update_submatrix[Ellipsis, 0], exp_result)
//  [python]
def test_neuron_update_bwd(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, network_spec = get_blur_state(env, inp, out, w)

    inp_act_fn = lambda x: x
    out_act_fn = sigmoid_with_grad

    pre_fwd, post_fwd = blur.dense_neuron_update(
        pre,
        post,
        synapse,
        inp_act=None,
        out_act=out_act_fn,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.FORWARD,
        global_spec=network_spec,
        env=env)

    pre_bkw, _ = blur.dense_neuron_update(
        pre_fwd,
        post_fwd,
        synapse=synapse,
        inp_act=inp_act_fn,
        out_act=out_act_fn,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.BACKWARD,
        global_spec=network_spec,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)

    exp_result = post_fwd[Ellipsis, 1] @ tf.transpose(ww, (0, 1, 3, 2))

    with tf.Session() as s:
      s.run(tf.initialize_all_variables())
      self.assertAllClose(pre_bkw[Ellipsis, 0], pre_fwd[Ellipsis, 0])
      self.assertAllClose(pre_bkw[Ellipsis, 1],
                          exp_result[Ellipsis, :-1] * pre_fwd[Ellipsis, 1])
//  [python]
def test_neuron_update_bwd(self):
    tf.reset_default_graph()
    tf.disable_v2_behavior()
    n_in, n_out = 10, 5
    inp, out, w = random_dense(n_in, n_out)
    env = blur_env.tf_env
    pre, post, synapse, genome, network_spec = get_blur_state(env, inp, out, w)

    inp_act_fn = lambda x: x
    out_act_fn = sigmoid_with_grad

    pre_fwd, post_fwd = blur.dense_neuron_update(
        pre,
        post,
        synapse,
        inp_act=None,
        out_act=out_act_fn,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.FORWARD,
        global_spec=network_spec,
        env=env)

    pre_bkw, _ = blur.dense_neuron_update(
        pre_fwd,
        post_fwd,
        synapse=synapse,
        inp_act=inp_act_fn,
        out_act=out_act_fn,
        neuron_genome=genome.neuron,
        update_type=synapse_util.UpdateType.BACKWARD,
        global_spec=network_spec,
        env=env)

    inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE))
    ww = w.astype(blur_env.NP_FLOATING_TYPE)

    exp_result = post_fwd[Ellipsis, 1] @ tf.transpose(ww, (0, 1, 3, 2))

    with tf.Session() as s:
      s.run(tf.global_variables_initializer())
      self.assertAllClose(pre_bkw[Ellipsis, 0], pre_fwd[Ellipsis, 0])
      self.assertAllClose(pre_bkw[Ellipsis, 1],
                          exp_result[Ellipsis, :-1] * pre_fwd[Ellipsis, 1])
//  [python]
def create_svm(m, optimize_model):
    batch_size = 1
    model_name = f"svm{m}"


    with tf.Session() as sess:
        x = tf.placeholder(tf.float32, shape=(m,), name='x')
        y = tf.placeholder(tf.float32, shape=(1,), name='y')

        w = tf.placeholder(tf.float32, shape=(m,), name='W')
        input_shapes = {"x:0": x.shape, "W:0": w.shape, "y:0": y.shape}

        mu = tf.constant(1, dtype=tf.float32, name="mu")

        h = tf.reduce_sum(w * x)
        c = y * h
        ny = 0 - y
        p = tf.cast((c > y), dtype=ny.dtype) * ny

        g = p * x

        w = tf.subtract(w, mu * g, name="W_out")
        sess.run(tf.initialize_all_variables())

        input_names = ['x:0', 'y:0']
        output_names = ['W_out:0']

        onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=input_names, output_names=output_names)
        model_proto = onnx_graph.make_model(model_name)
        model_proto = optimizer.optimize(model_proto, ['eliminate_identity'])
        if optimize_model:
            model_proto, check = simplify(model_proto, input_shapes=input_shapes)
            assert check
        with open(f"./{model_name}.onnx", "wb") as f:
            f.write(model_proto.SerializeToString())
//  [python]
def create_svm(m, optimize_model):
    batch_size = 1
    model_name = f"svm{m}"


    with tf.Session() as sess:
        x = tf.placeholder(tf.float32, shape=(m,), name='x')
        y = tf.placeholder(tf.float32, shape=(1,), name='y')

        w = tf.placeholder(tf.float32, shape=(m,), name='W')
        input_shapes = {"x:0": x.shape, "W:0": w.shape, "y:0": y.shape}

        mu = tf.constant(1, dtype=tf.float32, name="mu")

        h = tf.reduce_sum(w * x)
        c = y * h
        ny = 0 - y
        p = tf.cast((c > y), dtype=ny.dtype) * ny

        g = p * x

        w = tf.subtract(w, mu * g, name="W_out")
        sess.run(tf.global_variables_initializer())

        input_names = ['x:0', 'y:0']
        output_names = ['W_out:0']

        onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=input_names, output_names=output_names)
        model_proto = onnx_graph.make_model(model_name)
        model_proto = optimizer.optimize(model_proto, ['eliminate_identity'])
        if optimize_model:
            model_proto, check = simplify(model_proto, input_shapes=input_shapes)
            assert check
        with open(f"./{model_name}.onnx", "wb") as f:
            f.write(model_proto.SerializeToString())
//  [python]
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# important step
sess = tf.Session()
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
//  [python]
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# important step
sess = tf.Session()
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.global_variables_initializer()
else:
    init = tf.global_variables_initializer()
//  [python]
def main(argv=None):
    if FLAGS.non_linearity == 'tanh':
      non_linearity = tf.nn.tanh
    elif FLAGS.non_linearity == 'sigmoid':
      non_linearity = tf.nn.sigmoid
    else:
      non_linearity = tf.nn.relu

    nvdm = NVDM(vocab_size=FLAGS.vocab_size,
                n_hidden=FLAGS.n_hidden,
                n_topic=FLAGS.n_topic, 
                n_sample=FLAGS.n_sample,
                learning_rate=FLAGS.learning_rate, 
                batch_size=FLAGS.batch_size,
                non_linearity=non_linearity)
    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)

    train_url = os.path.join(FLAGS.data_dir, 'train.feat')
    test_url = os.path.join(FLAGS.data_dir, 'test.feat')

    train(sess, nvdm, train_url, test_url, FLAGS.batch_size)
//  [python]
def main(argv=None):
    if FLAGS.non_linearity == 'tanh':
      non_linearity = tf.nn.tanh
    elif FLAGS.non_linearity == 'sigmoid':
      non_linearity = tf.nn.sigmoid
    else:
      non_linearity = tf.nn.relu

    nvdm = NVDM(vocab_size=FLAGS.vocab_size,
                n_hidden=FLAGS.n_hidden,
                n_topic=FLAGS.n_topic, 
                n_sample=FLAGS.n_sample,
                learning_rate=FLAGS.learning_rate, 
                batch_size=FLAGS.batch_size,
                non_linearity=non_linearity)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)

    train_url = os.path.join(FLAGS.data_dir, 'train.feat')
    test_url = os.path.join(FLAGS.data_dir, 'test.feat')

    train(sess, nvdm, train_url, test_url, FLAGS.batch_size)
//  [python]
def create_backprop(l1, l2, l3, optimize_model):
    batch_size = 1
    model_name = f"backprop{l1}_{l2}_{l3}"
    with tf.Session() as sess:
        x = tf.placeholder(tf.float32, shape=(batch_size, l1), name='x')
        y = tf.placeholder(tf.float32, shape=(batch_size, l3), name='y')

        w1 = tf.placeholder(tf.float32, shape=(l1, l2), name='W1')
        w2 = tf.placeholder(tf.float32, shape=(l2, l3), name='W2')
        input_shapes = {"x:0": x.shape, "W1:0": w1.shape, "W2:0": w2.shape, "y:0": y.shape}

        mu = tf.constant(1, dtype=tf.float32)
        #     _ = tf.Variable(initial_value=np.random.rand(1))

        a1 = tf.math.sigmoid(tf.matmul(x, w1))
        a2 = tf.math.sigmoid(tf.matmul(a1, w2))

        d3 = a2 - y
        d2 = tf.matmul(d3, tf.transpose(w2)) * (a1 * (mu - a1))
        g1 = tf.transpose(x) * d2
        g2 = tf.transpose(a1) * d3

        w1 = tf.subtract(w1, mu * g1, name='W1_out')
        w2 = tf.subtract(w2, mu * g2, name='W2_out')
        sess.run(tf.initialize_all_variables())

        input_names = ['x:0', 'y:0']
        output_names = ['W1_out:0', 'W2_out:0']

        onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=input_names, output_names=output_names)
        model_proto = onnx_graph.make_model(model_name)
        onnx.checker.check_model(model_proto)
        model_proto = optimizer.optimize(model_proto, ['eliminate_identity'])
        if optimize_model:
            model_proto, check = simplify(model_proto, input_shapes=input_shapes)
            assert check
        with open(f"./{model_name}.onnx", "wb") as f:
            f.write(model_proto.SerializeToString())
//  [python]
def create_backprop(l1, l2, l3, optimize_model):
    batch_size = 1
    model_name = f"backprop{l1}_{l2}_{l3}"
    with tf.Session() as sess:
        x = tf.placeholder(tf.float32, shape=(batch_size, l1), name='x')
        y = tf.placeholder(tf.float32, shape=(batch_size, l3), name='y')

        w1 = tf.placeholder(tf.float32, shape=(l1, l2), name='W1')
        w2 = tf.placeholder(tf.float32, shape=(l2, l3), name='W2')
        input_shapes = {"x:0": x.shape, "W1:0": w1.shape, "W2:0": w2.shape, "y:0": y.shape}

        mu = tf.constant(1, dtype=tf.float32)
        #     _ = tf.Variable(initial_value=np.random.rand(1))

        a1 = tf.math.sigmoid(tf.matmul(x, w1))
        a2 = tf.math.sigmoid(tf.matmul(a1, w2))

        d3 = a2 - y
        d2 = tf.matmul(d3, tf.transpose(w2)) * (a1 * (mu - a1))
        g1 = tf.transpose(x) * d2
        g2 = tf.transpose(a1) * d3

        w1 = tf.subtract(w1, mu * g1, name='W1_out')
        w2 = tf.subtract(w2, mu * g2, name='W2_out')
        sess.run(tf.global_variables_initializer())

        input_names = ['x:0', 'y:0']
        output_names = ['W1_out:0', 'W2_out:0']

        onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=input_names, output_names=output_names)
        model_proto = onnx_graph.make_model(model_name)
        onnx.checker.check_model(model_proto)
        model_proto = optimizer.optimize(model_proto, ['eliminate_identity'])
        if optimize_model:
            model_proto, check = simplify(model_proto, input_shapes=input_shapes)
            assert check
        with open(f"./{model_name}.onnx", "wb") as f:
            f.write(model_proto.SerializeToString())
//  [python]
sess = tf.Session()
merged = tf.summary.merge_all()
# summary writer goes in here
train_writer = tf.summary.FileWriter("logs/train", sess.graph)
test_writer = tf.summary.FileWriter("logs/test", sess.graph)

# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
//  [python]
sess = tf.Session()
merged = tf.summary.merge_all()
# summary writer goes in here
train_writer = tf.summary.FileWriter("logs/train", sess.graph)
test_writer = tf.summary.FileWriter("logs/test", sess.graph)

init = tf.global_variables_initializer()
//  [python]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension(self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      boxes = tf.tile(
          tf.reshape(tf.range(4), [1, 4]), tf.stack([counter, tf.constant(1)]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'boxes': boxes},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, 4], tensor.get_shape().as_list())

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.tile(np.arange(4), (i, 1)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension(self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      boxes = tf.tile(
          tf.reshape(tf.range(4), [1, 4]), tf.stack([counter, tf.constant(1)]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'boxes': boxes},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, 4], tensor.get_shape().as_list())

      tf.compat.v1.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.tile(np.arange(4), (i, 1)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_all_dimensions(
      self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      image = tf.reshape(
          tf.range(counter * counter), tf.stack([counter, counter]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, None], tensor.get_shape().as_list())

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_all_dimensions(
      self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      image = tf.reshape(
          tf.range(counter * counter), tf.stack([counter, counter]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, None], tensor.get_shape().as_list())

      tf.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_variable_replace_getter(self):
    with self.test_session() as sess:
      context = variable_replace.VariableReplaceGetter()
      mod = snt.Linear(1, custom_getter=context)

      inp_data = tf.ones([10, 1])
      with context.use_variables():
        y1 = mod(inp_data)
        sess.run(tf.initialize_all_variables())
        np_y1 = sess.run(y1)

      values = context.get_variable_dict()

      new_values = {k: v + 1 for k, v in values.items()}

      with context.use_value_dict(new_values):
        np_y2 = mod(inp_data).eval()

      self.assertNear((np_y2 - np_y1)[0], 2, 1e-8)

      for v in values.values():
        v.assign(v + 1).eval()

      with context.use_variables():
        np_y3 = mod(inp_data).eval()
        self.assertNear((np_y3 - np_y2)[0], 0, 1e-8)
//  [python]
def test_variable_replace_getter(self):
    with self.test_session() as sess:
      context = variable_replace.VariableReplaceGetter()
      mod = snt.Linear(1, custom_getter=context)

      inp_data = tf.ones([10, 1])
      with context.use_variables():
        y1 = mod(inp_data)
        sess.run(tf.global_variables_initializer())
        np_y1 = sess.run(y1)

      values = context.get_variable_dict()

      new_values = {k: v + 1 for k, v in values.items()}

      with context.use_value_dict(new_values):
        np_y2 = mod(inp_data).eval()

      self.assertNear((np_y2 - np_y1)[0], 2, 1e-8)

      for v in values.values():
        v.assign(v + 1).eval()

      with context.use_variables():
        np_y3 = mod(inp_data).eval()
        self.assertNear((np_y3 - np_y2)[0], 0, 1e-8)
//  [python]
def test_batcher_when_batch_size_is_one(self):
    with self.test_session() as sess:
      batch_size = 1
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      image = tf.reshape(
          tf.range(counter * counter), tf.stack([counter, counter]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, None], tensor.get_shape().as_list())

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batcher_when_batch_size_is_one(self):
    with self.test_session() as sess:
      batch_size = 1
      num_batches = 2
      examples = tf.Variable(tf.constant(2, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 2)
      image = tf.reshape(
          tf.range(counter * counter), tf.stack([counter, counter]))
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([None, None], tensor.get_shape().as_list())

      tf.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        i = 2
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions(self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(1, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 1)
      image = tf.reshape(tf.range(1, 13), [4, 3]) * counter
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([4, 3], tensor.get_shape().as_list())

      tf.initialize_all_variables().run()
      with slim.queues.QueueRunners(sess):
        i = 1
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(1, 13).reshape((4, 3)) * i)
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions(self):
    with self.test_session() as sess:
      batch_size = 3
      num_batches = 2
      examples = tf.Variable(tf.constant(1, dtype=tf.int32))
      counter = examples.count_up_to(num_batches * batch_size + 1)
      image = tf.reshape(tf.range(1, 13), [4, 3]) * counter
      batch_queue = batcher.BatchQueue(
          tensor_dict={'image': image},
          batch_size=batch_size,
          batch_queue_capacity=100,
          num_batch_queue_threads=1,
          prefetch_queue_capacity=100)
      batch = batch_queue.dequeue()

      for tensor_dict in batch:
        for tensor in tensor_dict.values():
          self.assertAllEqual([4, 3], tensor.get_shape().as_list())

      tf.global_variables_initializer().run()
      with slim.queues.QueueRunners(sess):
        i = 1
        for _ in range(num_batches):
          batch_np = sess.run(batch)
          for tensor_dict in batch_np:
            for tensor in tensor_dict.values():
              self.assertAllEqual(tensor, np.arange(1, 13).reshape((4, 3)) * i)
              i += 1
        with self.assertRaises(tf.errors.OutOfRangeError):
          sess.run(batch)
//  [python]
def create_dummy_saved_model(path):
  """Creates minimal saved model for testing purposes."""

  class ImageModel(tf.train.Checkpoint):
    """Dummy image model."""

    def __init__(self):
      super(ImageModel, self).__init__()
      self.v = tf.Variable(1., use_resource=True)

    @tf.function(input_signature=[
        tf.TensorSpec(name="input", shape=[32, 224, 224, 3], dtype=tf.float32),
        tf.TensorSpec(name="training", shape=None, dtype=tf.bool),
    ])
    def __call__(self, x, training):
      return tf.reduce_mean(x, axis=[1, 2]) + self.v

  with tf.Session() as sess:
    model = ImageModel()

    # Explicitly reference save_counter so that is initialized before saving
    # the model.
    model.save_counter  # pylint: disable=pointless-statement

    model.trainable_variables = [model.v]
    init = tf.initialize_all_variables()
    sess.run(init)
    tf.compat.v2.saved_model.save(model, path)
//  [python]
def create_dummy_saved_model(path):
  """Creates minimal saved model for testing purposes."""

  class ImageModel(tf.train.Checkpoint):
    """Dummy image model."""

    def __init__(self):
      super(ImageModel, self).__init__()
      self.v = tf.Variable(1., use_resource=True)

    @tf.function(input_signature=[
        tf.TensorSpec(name="input", shape=[32, 224, 224, 3], dtype=tf.float32),
        tf.TensorSpec(name="training", shape=None, dtype=tf.bool),
    ])
    def __call__(self, x, training):
      return tf.reduce_mean(x, axis=[1, 2]) + self.v

  with tf.Session() as sess:
    model = ImageModel()

    # Explicitly reference save_counter so that is initialized before saving
    # the model.
    model.save_counter  # pylint: disable=pointless-statement

    model.trainable_variables = [model.v]
    init = tf.global_variables_initializer()
    sess.run(init)
    tf.compat.v2.saved_model.save(model, path)