code: def testSymbolModalityTargetsFactored(self):
      batch_size = 10
      num_datashards = 5
      length = 6
      height = 7
      hidden_size = 9
      vocab_size = 11
      model_hparams = common_hparams.basic_params1()
      model_hparams.factored_logits = True
      model_hparams.hidden_size = hidden_size
      model_hparams.mode = tf_estimator.ModeKeys.TRAIN
      body_output = np.random.randint(
          100, size=(batch_size, length, height, hidden_size)
      )
      targets = np.random.randint(
          vocab_size, size=(batch_size, length, height, 1)
      )
      data_parallelism = expert_utils.Parallelism(
          ["/device:CPU:0"] * num_datashards
      )
      with self.test_session() as session:
          sharded_body_output = tf.split(
              tf.to_float(body_output), num_datashards
          )
          sharded_targets = tf.split(targets, num_datashards)
          sharded_logits = data_parallelism(
              modalities.get_top(modalities.ModalityType.SYMBOL),
              sharded_body_output,
              sharded_targets,
              model_hparams,
              vocab_size,
          )
          sharded_loss_num, sharded_loss_den = data_parallelism(
              modalities.get_loss(modalities.ModalityType.SYMBOL),
              sharded_logits,
              sharded_targets,
              model_hparams,
              vocab_size,
              modalities.get_weights_fn(modalities.ModalityType.SYMBOL),
          )
          train_loss = tf.add_n(sharded_loss_num) / tf.maximum(
              1.0, tf.add_n(sharded_loss_den)
          )
          logits = tf.concat(sharded_logits, 0)
          session.run(tf.global_variables_initializer())
          res1, res2 = session.run((logits, train_loss))
      self.assertEqual(
          res1.shape, (batch_size, length, height, 1, vocab_size)
      )
      self.assertEqual(res2.shape, ())
  
file_path: tensor2tensor/tensor2tensor/layers/modalities_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  from tensor2tensor.layers import common_hparams
  from tensor2tensor.layers import modalities
  from tensor2tensor.utils import expert_utils
  from tensor2tensor.utils import test_utils
  import tensorflow.compat.v1
  from tensorflow.compat.v1 import estimator
label_desc: Use image node_id 4 for calling the ModalityTest obj's underlying member method code with example usage: obj.testSymbolModalityTargetsFactored() without return types