def AddCrossEntropy(batch_size, n):
 cross_entropies = []
 def _Pass():
  return tf.constant(0, dtype=tf.float32, shape=[1])
 for beam_id in range(batch_size):
  beam_gold_slot = tf.reshape(tf.slice(n['gold_slot'], [beam_id], [1]), [1])
  def _ComputeCrossEntropy():
   'Adds ops to compute cross entropy of the gold path in a beam.'
   idx = tf.cast(tf.reshape(tf.where(tf.equal(n['beam_ids'], beam_id)), [(-1)]), tf.int32)
   beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, (-1)])
   num = tf.shape(idx)
   return tf.nn.softmax_cross_entropy_with_logits(beam_scores, tf.expand_dims(tf.sparse_to_dense(beam_gold_slot, num, [1.0], 0.0), 0))
  cross_entropies.append(cf.cond((beam_gold_slot[0] >= 0), _ComputeCrossEntropy, _Pass))
 return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}

def AddCrossEntropy ( batch_size , n ) : cross_entropies = [ ] def _Pass ( ) : return tf . constant ( 0 , dtype = tf . float32 , shape = [ 1 ] ) for beam_id in range ( batch_size ) : beam_gold_slot = tf . reshape ( tf . slice ( n [ 'gold_slot' ] , [ beam_id ] , [ 1 ] ) , [ 1 ] ) def _ComputeCrossEntropy ( ) : idx = tf . cast ( tf . reshape ( tf . where ( tf . equal ( n [ 'beam_ids' ] , beam_id ) ) , [ ( - 1 ) ] ) , tf . int32 ) beam_scores = tf . reshape ( tf . gather ( n [ 'all_path_scores' ] , idx ) , [ 1 , ( - 1 ) ] ) num = tf . shape ( idx ) return tf . nn . softmax_cross_entropy_with_logits ( beam_scores , tf . expand_dims ( tf . sparse_to_dense ( beam_gold_slot , num , [ 1.0 ] , 0.0 ) , 0 ) ) cross_entropies . append ( cf . cond ( ( beam_gold_slot [ 0 ] >= 0 ) , _ComputeCrossEntropy , _Pass ) ) return { 'cross_entropy' : tf . div ( tf . add_n ( cross_entropies ) , batch_size ) }
adds a cross entropy cost function .