Beispiel #1
0
  def _update_beam(self, beam_state, utt_id, state):
    """Calculate log probability for shift and assignment process based on current beam state."""
    loss = 0
    speaker = utt_id[-4]
    # Convert the state into one-hot vector
    label = np.zeros(self.num_state)
    label[state] = 1

    # Get original ce loss
    logit = np.reshape(self.all_logits[utt_id], [4,])
    loss = utils.cross_entropy(label, logit)

    # RESCORING:

    # An existing state
    if state in np.unique(beam_state.emo_sequence) and speaker in np.unique(beam_state.spk_sequence): 
      # Find last state
      last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
      last_state = beam_state.emo_sequence[last_idx]
      # No shift
      if state == last_state:
        loss -= np.log(1 - self.transition_bias)
      # Shift
      else:
        loss -= np.log(self.transition_bias) + \
                np.log(beam_state.block_counts[state]) - \
                np.log(sum(beam_state.block_counts) + self.crp_alpha)
        beam_state.block_counts[state] += 1

    # A new state
    else: 
      loss -= np.log(self.transition_bias) + \
              np.log(self.crp_alpha) - \
              np.log(sum(beam_state.block_counts) + \
              self.crp_alpha)          
      beam_state.block_counts[state] += 1

    new_beam_state = beam_state.update(speaker, state, loss)

    return new_beam_state
    def _update_beam(self, beam_state, utt_id, state):
        """Calculate log probability for shift and assignment process based on current beam state."""
        loss = 0
        speaker = utt_id[-4]
        # Convert the state into one-hot vector
        label = np.zeros(self.num_state)
        label[state] = 1

        # Get original ce loss
        logit = np.reshape(self.all_logits[utt_id], [
            4,
        ])
        loss = utils.cross_entropy(label, logit)
        # RESCORING:

        # An existing state (song lin original)
        if state in np.unique(
                beam_state.emo_sequence) and speaker in np.unique(
                    beam_state.spk_sequence):
            # Find last state
            last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
            last_state = beam_state.emo_sequence[last_idx]
            # No shift
            if state == last_state:
                loss -= np.log(1 - self.transition_bias)
            # Shift
            else:
                loss -= np.log(self.transition_bias) + \
                        np.log(beam_state.block_counts[state]) - \
                        np.log(sum(beam_state.block_counts) + self.crp_alpha)
                beam_state.block_counts[state] += 1

        # A new state
        else:
            loss -= np.log(self.transition_bias) + \
                    np.log(self.crp_alpha) - \
                    np.log(sum(beam_state.block_counts) + \
                    self.crp_alpha)
            beam_state.block_counts[state] += 1
        '''
    # An existing state (ML model for emotion shift)
    if state in np.unique(beam_state.emo_sequence) and speaker in np.unique(beam_state.spk_sequence): 
      # Find last state
      last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
      last_state = beam_state.emo_sequence[last_idx]
      # No shift
      if state == last_state:
        loss -= np.log(1 - self.ML_model_emo_shift_output.get(str(utt_id)))
      # Shift
      else:
        loss -= np.log(self.ML_model_emo_shift_output.get(str(utt_id))) + \
                np.log(beam_state.block_counts[state]) - \
                np.log(sum(beam_state.block_counts) + self.crp_alpha)
      beam_state.block_counts[state] += 1

    # A new state
    else:     
      loss -= np.log(self.ML_model_emo_shift_output.get(str(utt_id))) + \
                np.log(self.crp_alpha) - \
                np.log(sum(beam_state.block_counts) + \
                self.crp_alpha)  
      beam_state.block_counts[state] += 1
    '''
        '''
    # CRP denominator minus last state
    # Find last state of current dialog (t-1)
    if len(beam_state.emo_sequence) == 0: #first utterance of this dialog
      last_state_seq = None
      #block_counts_last_state = 0
    else:
      #{ang, hap, neu, sad}
      last_state_seq = beam_state.emo_sequence[len(beam_state.emo_sequence)-1]
      #block_counts_last_state = beam_state.block_counts[last_state_seq]
    
    # Find last state of current spk
    last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
    if last_idx != None:
        last_state = beam_state.emo_sequence[last_idx]
        block_counts_last_state = beam_state.block_counts[last_state]
    else:
        block_counts_last_state = 0
    
    # An existing state
    if state in np.unique(beam_state.emo_sequence) and speaker in np.unique(beam_state.spk_sequence): 
      # No shift
      if state == last_state:
        loss -= np.log(1 - self.transition_bias)
      # Shift
      else:
        loss -= np.log(self.transition_bias) + \
                np.log(beam_state.block_counts[state]) - \
                np.log(sum(beam_state.block_counts)-block_counts_last_state + self.crp_alpha)
        beam_state.block_counts[state] += 1

    # A new state
    else: 
      loss -= np.log(self.transition_bias) + \
              np.log(self.crp_alpha) - \
              np.log(sum(beam_state.block_counts)-block_counts_last_state + self.crp_alpha)          
      beam_state.block_counts[state] += 1
    '''
        '''
    # Find last state of this speaker (Bigram) (Intra)
    last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
    if last_idx == None: #this is speaker first utterance
      loss -= np.log(1)
    else:
      #{ang, hap, neu, sad}
      last_state = beam_state.emo_sequence[last_idx]
      if last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['a2a'])
      elif last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['a2h'])
      elif last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['a2n'])
      elif last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['a2s'])

      elif last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['h2a'])
      elif last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['h2h'])
      elif last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['h2n'])
      elif last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['h2s'])

      elif last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['n2a'])
      elif last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['n2h'])
      elif last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['n2n'])
      elif last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['n2s'])

      elif last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['s2a'])
      elif last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['s2h'])
      elif last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['s2n'])
      elif last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['s2s'])
    '''
        '''
    # Find last state of current dialog (Bigram) (Sequential)
    if len(beam_state.emo_sequence) == 0: #first utterance of this dialog
      loss -= np.log(1)
    else:
      #{ang, hap, neu, sad}
      last_state = beam_state.emo_sequence[len(beam_state.emo_sequence)-1]
      if last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['a2a'])
      elif last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['a2h'])
      elif last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['a2n'])
      elif last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['a2s'])

      elif last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['h2a'])
      elif last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['h2h'])
      elif last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['h2n'])
      elif last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['h2s'])

      elif last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['n2a'])
      elif last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['n2h'])
      elif last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['n2n'])
      elif last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['n2s'])

      elif last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['s2a'])
      elif last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['s2h'])
      elif last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['s2n'])
      elif last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['s2s'])
    '''
        '''
    # Find last and the second to last states of this speaker (Trigram) (Intra)
    last_idx = utils.find_last_idx(beam_state.spk_sequence, speaker)
    second_to_last_idx = utils.find_second_to_last_idx(beam_state.spk_sequence, speaker)
    if last_idx == None or second_to_last_idx == None: #this is speaker first or second utterance
      loss -= np.log(1)
    else:
      #{ang, hap, neu, sad}
      last_state = beam_state.emo_sequence[last_idx]
      second_to_last_state = beam_state.emo_sequence[second_to_last_idx]
      if second_to_last_state == 0 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['aaa'])
      elif second_to_last_state == 0 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['aah'])
      elif second_to_last_state == 0 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['aan'])
      elif second_to_last_state == 0 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['aas'])

      elif second_to_last_state == 0 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['aha'])
      elif second_to_last_state == 0 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ahh'])
      elif second_to_last_state == 0 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ahn'])
      elif second_to_last_state == 0 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ahs'])

      elif second_to_last_state == 0 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['ana'])
      elif second_to_last_state == 0 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['anh'])
      elif second_to_last_state == 0 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ann'])
      elif second_to_last_state == 0 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ans'])

      elif second_to_last_state == 0 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['asa'])
      elif second_to_last_state == 0 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ash'])
      elif second_to_last_state == 0 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['asn'])
      elif second_to_last_state == 0 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ass'])
      ###################################################################
      elif second_to_last_state == 1 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['haa'])
      elif second_to_last_state == 1 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hah'])
      elif second_to_last_state == 1 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['han'])
      elif second_to_last_state == 1 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['has'])

      elif second_to_last_state == 1 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hha'])
      elif second_to_last_state == 1 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hhh'])
      elif second_to_last_state == 1 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hhn'])
      elif second_to_last_state == 1 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hhs'])

      elif second_to_last_state == 1 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hna'])
      elif second_to_last_state == 1 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hnh'])
      elif second_to_last_state == 1 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hnn'])
      elif second_to_last_state == 1 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hns'])

      elif second_to_last_state == 1 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hsa'])
      elif second_to_last_state == 1 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hsh'])
      elif second_to_last_state == 1 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hsn'])
      elif second_to_last_state == 1 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hss'])
      ###################################################################
      elif second_to_last_state == 2 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['naa'])
      elif second_to_last_state == 2 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nah'])
      elif second_to_last_state == 2 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nan'])
      elif second_to_last_state == 2 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nas'])

      elif second_to_last_state == 2 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nha'])
      elif second_to_last_state == 2 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nhh'])
      elif second_to_last_state == 2 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nhn'])
      elif second_to_last_state == 2 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nhs'])

      elif second_to_last_state == 2 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nna'])
      elif second_to_last_state == 2 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nnh'])
      elif second_to_last_state == 2 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nnn'])
      elif second_to_last_state == 2 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nns'])

      elif second_to_last_state == 2 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nsa'])
      elif second_to_last_state == 2 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nsh'])
      elif second_to_last_state == 2 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nsn'])
      elif second_to_last_state == 2 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nss'])
      ###################################################################
      elif second_to_last_state == 3 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['saa'])
      elif second_to_last_state == 3 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['sah'])
      elif second_to_last_state == 3 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['san'])
      elif second_to_last_state == 3 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sas'])

      elif second_to_last_state == 3 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['sha'])
      elif second_to_last_state == 3 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['shh'])
      elif second_to_last_state == 3 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['shn'])
      elif second_to_last_state == 3 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['shs'])

      elif second_to_last_state == 3 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['sna'])
      elif second_to_last_state == 3 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['snh'])
      elif second_to_last_state == 3 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['snn'])
      elif second_to_last_state == 3 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sns'])

      elif second_to_last_state == 3 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['ssa'])
      elif second_to_last_state == 3 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ssh'])
      elif second_to_last_state == 3 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ssn'])
      elif second_to_last_state == 3 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sss'])
    '''
        '''
    # Find last state of current dialog (Trigram) (Sequential)
    if len(beam_state.emo_sequence) == 0 or len(beam_state.emo_sequence) == 1: #first or second utterance of this dialog
      loss -= np.log(1)
    else:
      #{ang, hap, neu, sad}
      last_state = beam_state.emo_sequence[len(beam_state.emo_sequence)-1]
      second_to_last_state = beam_state.emo_sequence[len(beam_state.emo_sequence)-2]

      if second_to_last_state == 0 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['aaa'])
      elif second_to_last_state == 0 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['aah'])
      elif second_to_last_state == 0 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['aan'])
      elif second_to_last_state == 0 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['aas'])

      elif second_to_last_state == 0 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['aha'])
      elif second_to_last_state == 0 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ahh'])
      elif second_to_last_state == 0 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ahn'])
      elif second_to_last_state == 0 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ahs'])

      elif second_to_last_state == 0 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['ana'])
      elif second_to_last_state == 0 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['anh'])
      elif second_to_last_state == 0 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ann'])
      elif second_to_last_state == 0 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ans'])

      elif second_to_last_state == 0 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['asa'])
      elif second_to_last_state == 0 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ash'])
      elif second_to_last_state == 0 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['asn'])
      elif second_to_last_state == 0 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['ass'])
      ###################################################################
      elif second_to_last_state == 1 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['haa'])
      elif second_to_last_state == 1 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hah'])
      elif second_to_last_state == 1 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['han'])
      elif second_to_last_state == 1 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['has'])

      elif second_to_last_state == 1 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hha'])
      elif second_to_last_state == 1 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hhh'])
      elif second_to_last_state == 1 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hhn'])
      elif second_to_last_state == 1 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hhs'])

      elif second_to_last_state == 1 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hna'])
      elif second_to_last_state == 1 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hnh'])
      elif second_to_last_state == 1 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hnn'])
      elif second_to_last_state == 1 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hns'])

      elif second_to_last_state == 1 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['hsa'])
      elif second_to_last_state == 1 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['hsh'])
      elif second_to_last_state == 1 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['hsn'])
      elif second_to_last_state == 1 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['hss'])
      ###################################################################
      elif second_to_last_state == 2 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['naa'])
      elif second_to_last_state == 2 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nah'])
      elif second_to_last_state == 2 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nan'])
      elif second_to_last_state == 2 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nas'])

      elif second_to_last_state == 2 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nha'])
      elif second_to_last_state == 2 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nhh'])
      elif second_to_last_state == 2 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nhn'])
      elif second_to_last_state == 2 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nhs'])

      elif second_to_last_state == 2 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nna'])
      elif second_to_last_state == 2 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nnh'])
      elif second_to_last_state == 2 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nnn'])
      elif second_to_last_state == 2 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nns'])

      elif second_to_last_state == 2 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['nsa'])
      elif second_to_last_state == 2 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['nsh'])
      elif second_to_last_state == 2 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['nsn'])
      elif second_to_last_state == 2 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['nss'])
      ###################################################################
      elif second_to_last_state == 3 and last_state == 0 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['saa'])
      elif second_to_last_state == 3 and last_state == 0 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['sah'])
      elif second_to_last_state == 3 and last_state == 0 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['san'])
      elif second_to_last_state == 3 and last_state == 0 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sas'])

      elif second_to_last_state == 3 and last_state == 1 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['sha'])
      elif second_to_last_state == 3 and last_state == 1 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['shh'])
      elif second_to_last_state == 3 and last_state == 1 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['shn'])
      elif second_to_last_state == 3 and last_state == 1 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['shs'])

      elif second_to_last_state == 3 and last_state == 2 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['sna'])
      elif second_to_last_state == 3 and last_state == 2 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['snh'])
      elif second_to_last_state == 3 and last_state == 2 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['snn'])
      elif second_to_last_state == 3 and last_state == 2 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sns'])

      elif second_to_last_state == 3 and last_state == 3 and state == 0:
        loss -= np.log(self.emo_trans_prob_dict['ssa'])
      elif second_to_last_state == 3 and last_state == 3 and state == 1:
        loss -= np.log(self.emo_trans_prob_dict['ssh'])
      elif second_to_last_state == 3 and last_state == 3 and state == 2:
        loss -= np.log(self.emo_trans_prob_dict['ssn'])
      elif second_to_last_state == 3 and last_state == 3 and state == 3:
        loss -= np.log(self.emo_trans_prob_dict['sss'])
    '''
        new_beam_state = beam_state.update(speaker, state, loss)
        return new_beam_state