code: def test_to_sequence_backprop(device_id):
      dev = cntk_device(device_id)
      input_vocab_size = 3
      emb_dim = 2
      hidden_dim = 2
      num_labels = 2
      x_seq_input = C.sequence.input_variable(
          input_vocab_size, is_sparse=True, name="features"
      )
      with C.default_options(initial_state=0.1):
          model = C.layers.Embedding(emb_dim, name="embed")(x_seq_input)
          model = C.layers.Recurrence(
              C.layers.LSTM(hidden_dim), go_backwards=False
          )(model)
          model = C.layers.Dense(num_labels, name="classify")(model)
  
      z = model
      label_seq_input = C.sequence.input_variable(
          num_labels, is_sparse=True, name="labels"
      )
      ce = C.cross_entropy_with_softmax(z, label_seq_input)
  
      seq1_data = [[0, 1, 1], [0, 1, 0], [1, 0, 0]]
      seq2_data = [[0, 0, 1], [0, 1, 1]]
      seq1_label_data = [[0, 1], [0, 1], [1, 0]]
      seq2_label_data = [[1, 0], [0, 1]]
      label_seq_data = [
          _to_csr(seq1_label_data),
          _to_csr(seq2_label_data),
      ]
      param_grads_1, loss_result_1 = ce.grad(
          {
              x_seq_input: [_to_csr(seq1_data), _to_csr(seq2_data)],
              label_seq_input: label_seq_data,
          },
          wrt=ce.parameters,
          outputs=[ce],
          as_numpy=False,
      )
  
      # Create a clone of the model that uses a non-sequence input
      # and converts it to a sequence using to_sequence
      x_non_seq_input = C.input_variable(
          (C.FreeDimension, input_vocab_size),
          is_sparse=True,
          name="non_seq_features",
      )
      x_seq_lens = C.input_variable((), name="sequence_lengths")
      x_seq = C.to_sequence(x_non_seq_input, x_seq_lens)
      x_seq = C.reconcile_dynamic_axes(
          C.times(x_seq, np.eye(input_vocab_size, dtype=np.float32)),
          label_seq_input,
      )
      ce_clone = ce.clone("share", {x_seq_input: x_seq})
  
      x_non_seq_data = C.NDArrayView.from_csr(
          _to_csr([seq1_data, seq2_data + [[0, 0, 0]]]), shape=(2, 3, 3)
      )
      x_seq_lens_data = np.asarray([3, 2], dtype=np.float32)
  
      x_non_seq_input = next(
          argument
          for argument in ce_clone.arguments
          if argument.name == "non_seq_features"
      )
      label_seq_input = next(
          argument
          for argument in ce_clone.arguments
          if argument.name == "labels"
      )
      x_seq_lens = next(
          argument
          for argument in ce_clone.arguments
          if argument.name == "sequence_lengths"
      )
      param_grads_2, loss_result_2 = ce_clone.grad(
          {
              x_non_seq_input: x_non_seq_data,
              x_seq_lens: x_seq_lens_data,
              label_seq_input: label_seq_data,
          },
          wrt=ce_clone.parameters,
          outputs=[ce_clone],
          as_numpy=False,
      )
  
      assert np.array_equal(
          loss_result_1.as_sequences()[0],
          loss_result_2.as_sequences()[0],
      )
      assert np.array_equal(
          loss_result_1.as_sequences()[1],
          loss_result_2.as_sequences()[1],
      )
  
      for param in param_grads_1:
          if not param_grads_1[param].is_sparse:
              reference_grad_value = param_grads_1[param].asarray()
              grad_value = param_grads_2[param].asarray()
              assert np.array_equal(reference_grad_value, grad_value)
  
imports: from __future__ import division
  import numpy
  import pytest
  import cntk
  from .ops_test_utils import cntk_device
  from cntk.tests.test_utils import _to_dense, _to_csr
  from .ops_test_utils import unittest_helper, _test_unary_op, _test_binary_op, AA, precision, PRECISION_TO_TYPE, cntk_device
  from cntk import Value
  from cntk.axis import Axis
  from cntk.internal import sanitize_dtype_cntk
  from ..None import constant
label_desc: Use image node_id 12 for calling a global function with example usage: test_to_sequence_backprop(device_id) without return types