'Builds an inference loop for this component.
Repeatedly evaluates the network and advances the underlying state according
to the predicted scores.
Args:
state: MasterState from the \'AdvanceMaster\' op that advances the
underlying master to this component.
network_states: NetworkState object containing component TensorArrays.
during_training: whether the graph is being constructed during training
Returns:
Handle to the state once inference is complete for this Component.'

def build_greedy_inference(self, state, network_states, during_training=False):

    logging.info('Building    component:    %s', self.spec.name)
    if during_training:
        stride = (state.current_batch_size * self.training_beam_size)
    else:
        stride = (state.current_batch_size * self.inference_beam_size)
    def cond(handle, *_):
        all_final = dragnn_ops.emit_all_final(handle, component=self.name)
        return tf.logical_not(tf.reduce_all(all_final))
    def body(handle, *arrays):
        'Runs    the    network    and    advances    the    state    by    a    step.'
        with tf.control_dependencies(([handle] + [x.flow for x in arrays])):
            updated_state = MasterState(handle, state.current_batch_size)
            network_tensors = self._feedforward_unit(updated_state, arrays, network_states, stride, during_training=during_training)
            next_arrays = update_tensor_arrays(network_tensors, arrays)
            with tf.control_dependencies([x.flow for x in next_arrays]):
                logits = self.network.get_logits(network_tensors)
                logits = tf.cond(self.locally_normalize, (lambda : tf.nn.log_softmax(logits)), (lambda : logits))
                handle = dragnn_ops.advance_from_prediction(handle, logits, component=self.name)
            return ([handle] + next_arrays)
    with tf.name_scope((self.name + '/inference_state')):
        init_arrays = []
        for layer in self.network.layers:
            init_arrays.append(layer.create_array(stride))
    output = tf.while_loop(cond, body, ([state.handle] + init_arrays), name=('inference_%s' % self.name))
    state.handle = output[0]
    arrays = output[1:]
    network_state = network_states[self.name]
    with tf.name_scope((self.name + '/stored_act')):
        for (index, layer) in enumerate(self.network.layers):
            network_state.activations[layer.name] = network_units.StoredActivations(array=arrays[index])
    with tf.control_dependencies([x.flow for x in arrays]):
        return tf.identity(state.handle)
