code: def crf_forward(
      inputs: TensorLike,
      state: TensorLike,
      transition_params: TensorLike,
      sequence_lengths: TensorLike,
  ) -> tf.Tensor:
      """Computes the alpha values in a linear-chain CRF.
  
      See http://www.cs.columbia.edu/~mcollins/fb.pdf for reference.
  
      Args:
        inputs: A [batch_size, num_tags] matrix of unary potentials.
        state: A [batch_size, num_tags] matrix containing the previous alpha
           values.
        transition_params: A [num_tags, num_tags] matrix of binary potentials.
            This matrix is expanded into a [1, num_tags, num_tags] in preparation
            for the broadcast summation occurring within the cell.
        sequence_lengths: A [batch_size] vector of true sequence lengths.
  
      Returns:
        new_alphas: A [batch_size, num_tags] matrix containing the
            new alpha values.
      """
      sequence_lengths = tf.cast(sequence_lengths, dtype=tf.int32)
  
      last_index = tf.maximum(
          tf.constant(0, dtype=sequence_lengths.dtype),
          sequence_lengths - 1,
      )
      inputs = tf.transpose(inputs, [1, 0, 2])
      transition_params = tf.expand_dims(transition_params, 0)
  
      def _scan_fn(_state, _inputs):
          _state = tf.expand_dims(_state, 2)
          transition_scores = _state + transition_params
          new_alphas = _inputs + tf.reduce_logsumexp(
              transition_scores, [1]
          )
          return new_alphas
  
      all_alphas = tf.transpose(
          tf.scan(_scan_fn, inputs, state), [1, 0, 2]
      )
      # add first state for sequences of length 1
      all_alphas = tf.concat([tf.expand_dims(state, 1), all_alphas], 1)
  
      idxs = tf.stack(
          [tf.range(tf.shape(last_index)[0]), last_index], axis=1
      )
      return tf.gather_nd(all_alphas, idxs)
  
imports: import warnings
  import numpy
  import tensorflow
  from tensorflow_addons.rnn.abstract_rnn_cell import AbstractRNNCell
  from tensorflow_addons.utils.types import TensorLike
  from typeguard import typechecked
  from typing import Optional, Tuple
label_desc: Use image node_id 8 for calling a global function with example usage: crf_forward(inputs, state, transition_params, sequence_lengths) and returns: tf, new_alphas