code: def test_crf_with_loss_op(self, num_tags, num_words):
      model = ModelHelper(name="external")
      embeddings_dim = 200
      embeddings = np.random.randn(num_words, embeddings_dim).astype(
          np.float32
      )
      transitions = np.random.uniform(
          low=-1, high=1, size=(num_tags + 2, num_tags + 2)
      ).astype(np.float32)
      labels = np.random.randint(num_tags, size=(num_words)).astype(
          np.int64
      )
      (
          embeddings_blob,
          labels_blob,
          transitions_blob,
      ) = model.net.AddExternalInputs(
          "embeddings_blob", "labels_blob", "crf_transitions"
      )
      workspace.FeedBlob(str(embeddings_blob), embeddings)
      workspace.FeedBlob(str(labels_blob), labels)
      workspace.FeedBlob(str(transitions_blob), transitions)
      predictions_blob = brew.fc(
          model,
          embeddings_blob,
          "fc_0",
          embeddings_dim,
          num_tags,
          ("UniformFill", {"min": -1.0}, {"max": 1.0}),
          ("UniformFill", {"min": -1.0}, {"max": 1.0}),
      )
      crf_layer = crf.CRFWithLoss(model, num_tags, transitions_blob)
      crf_loss = crf_layer.crf_loss(predictions_blob, labels_blob)
      model.net.AddGradientOperators([crf_loss])
      workspace.RunNetOnce(model.param_init_net)
      workspace.RunNetOnce(model.net)
      loss = workspace.FetchBlob(str(crf_loss))
      predictions = workspace.FetchBlob(str(predictions_blob))
      np.testing.assert_allclose(
          loss,
          self._compute_loss_manual(
              predictions, num_tags, labels, transitions
          ),
          atol=0.001,
          rtol=0.001,
          err_msg="CRF LOSS is not matching the reference",
      )
  
file_path: pytorch/caffe2/python/operator_test/crf_test.py
imports: from caffe2.python import workspace, crf, brew
  from caffe2.python.model_helper import ModelHelper
  import numpy
  from scipy.special import logsumexp
  import caffe2.python.hypothesis_test_util
  import hypothesis.strategies
  from hypothesis import given, settings
label_desc: Use image node_id 1 for calling the TestCRFOp obj's underlying member method code with example usage: obj.test_crf_with_loss_op(num_tags, num_words) without return types