code: def test_training_3d_sparse_sequence_with_recurrence(device_id):
      from .. import times, times_transpose, reshape
      from cntk.losses import cross_entropy_with_softmax
  
      dev = cntk_device(device_id)
      vocab_size = 3
      additional_axis_dim1 = 2
      additional_axis_dim2 = 3
      out_dim = 2
      w_init_i = np.float32(np.random.rand(vocab_size, out_dim))
      w_init_h = np.float32(np.random.rand(out_dim, out_dim))
      input_shape = (
          additional_axis_dim1,
          additional_axis_dim2,
          vocab_size,
      )
      label_shape = (
          additional_axis_dim1 * additional_axis_dim2,
          out_dim,
      )
  
      def create_trainer(use_sparse, device):
          a = C.sequence.input_variable(
              shape=input_shape, is_sparse=use_sparse, name="input"
          )
          w_i = C.parameter(init=w_init_i, device=dev)
          a_projection = times(a, w_i)
  
          p_o = C.placeholder()
          h = C.sequence.past_value(p_o)
          w_h = C.parameter(init=w_init_h, device=dev)
          h_projection = times(h, w_h)
          z = a_projection + h_projection
          z = z.replace_placeholder(z)
          z = reshape(z, label_shape)
  
          l = C.sequence.input_variable(
              shape=label_shape, is_sparse=use_sparse, name="label"
          )
          loss = cross_entropy_with_softmax(z, l, axis=-1)
          trainer = C.Trainer(
              z,
              (loss, None),
              C.sgd(
                  z.parameters,
                  lr=C.learning_parameter_schedule_per_sample(0.7),
              ),
          )
          return (a, l, w_i, w_h, trainer)
  
      # Run with sparse inputs
      input_sparse_indices = [
          [1, 0, 1, 2, 0, 1, 0, 2, 1, 1, 0, 2],
          [2, 0, 1, 1, 2, 0],
      ]
      input_data = C.Value.one_hot(
          input_sparse_indices, input_shape, device=dev
      )
      label_sparse_indices = [
          [1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0],
          [1, 1, 0, 0, 0, 1],
      ]
      label_data = C.Value.one_hot(
          label_sparse_indices, label_shape, device=dev
      )
  
      (
          input_var,
          label_var,
          weights_i,
          weights_h,
          trainer,
      ) = create_trainer(use_sparse=True, device=dev)
      trainer.train_minibatch(
          {input_var: input_data, label_var: label_data}, device=dev
      )
      weights_with_sparse_input_i = weights_i.value
      weights_with_sparse_input_h = weights_h.value
  
      # Run with dense inputs
      i1 = np.eye(vocab_size, dtype=np.float32)
      dense_input = [
          i1[input_sparse_indices[0]],
          i1[input_sparse_indices[1]],
      ]
      i2 = np.eye(out_dim, dtype=np.float32)
      dense_label = [
          i2[label_sparse_indices[0]],
          i2[label_sparse_indices[1]],
      ]
  
      dense_input_data = [
          np.reshape(dense_input[0], (-1,) + input_shape),
          np.reshape(dense_input[1], (-1,) + input_shape),
      ]
      dense_label_data = [
          np.reshape(dense_label[0], (-1,) + label_shape),
          np.reshape(dense_label[1], (-1,) + label_shape),
      ]
  
      (
          input_var,
          label_var,
          weights_i,
          weights_h,
          trainer,
      ) = create_trainer(use_sparse=False, device=dev)
      trainer.train_minibatch(
          {input_var: dense_input_data, label_var: dense_label_data},
          device=dev,
      )
      weights_with_dense_input_i = weights_i.value
      weights_with_dense_input_h = weights_h.value
  
      assert np.allclose(
          weights_with_sparse_input_i, weights_with_dense_input_i
      )
      assert np.allclose(
          weights_with_sparse_input_h, weights_with_dense_input_h
      )
  
imports: import numpy
  import scipy
  import pytest
  import cntk
  from .ops_test_utils import cntk_device
  from cntk.axis import Axis
  from ..None import constant
  from cntk.tests.test_utils import _to_dense
label_desc: Use image node_id 4 for calling a global function with example usage: test_training_3d_sparse_sequence_with_recurrence(device_id) and returns: a,  l,  w_i,  w_h,  trainer