Python tensorflow.compat.v2.argmax() Examples

The following are 9 code examples of tensorflow.compat.v2.argmax(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.compat.v2 , or try the search function .
Example #1
Source File: td_problem.py    From valan with Apache License 2.0 6 votes vote down vote up
def select_actor_action(self, env_output, agent_output):
    assert self._mode, 'mode must be set for selecting action in actor.'
    oracle_next_action = env_output.observation[
        streetview_constants.ORACLE_NEXT_ACTION]
    if self._mode == 'train':
      if self._loss_type == common.CE_LOSS:
        # This is teacher-forcing mode, so choose action same as oracle action.
        action_idx = oracle_next_action
      elif self._loss_type == common.AC_LOSS:
        action_idx = tfp.distributions.Categorical(
            logits=agent_output.policy_logits).sample()
    else:
      # In non-train modes, choose greedily.
      action_idx = tf.argmax(agent_output.policy_logits, axis=-1)

    # Return ActorAction and the action to be passed to the env step function.
    return common.ActorAction(
        chosen_action_idx=int(action_idx.numpy()),
        oracle_next_action_idx=int(
            oracle_next_action.numpy())), action_idx.numpy() 
Example #2
Source File: ndh_problem.py    From valan with Apache License 2.0 6 votes vote down vote up
def select_actor_action(self, env_output, agent_output):
    oracle_next_action = env_output.observation[constants.ORACLE_NEXT_ACTION]
    oracle_next_action_indices = tf.where(
        tf.equal(env_output.observation[constants.CONN_IDS],
                 oracle_next_action))
    oracle_next_action_idx = tf.reduce_min(oracle_next_action_indices)
    assert self._mode, 'mode must be set.'
    if self._mode == 'train':
      if self._loss_type == common.CE_LOSS:
        # This is teacher-forcing mode, so choose action same as oracle action.
        action_idx = oracle_next_action_idx
      elif self._loss_type == common.AC_LOSS:
        # Choose next pano from probability distribution over next panos
        action_idx = tfp.distributions.Categorical(
            logits=agent_output.policy_logits).sample()
      else:
        raise ValueError('Unsupported loss type {}'.format(self._loss_type))
    else:
      # In non-train modes, choose greedily.
      action_idx = tf.argmax(agent_output.policy_logits, axis=-1)
    action_val = env_output.observation[constants.CONN_IDS][action_idx]
    return common.ActorAction(
        chosen_action_idx=int(action_idx.numpy()),
        oracle_next_action_idx=int(oracle_next_action_idx.numpy())), int(
            action_val.numpy()) 
Example #3
Source File: r2r_problem.py    From valan with Apache License 2.0 6 votes vote down vote up
def select_actor_action(self, env_output, agent_output):
    oracle_next_action = env_output.observation[constants.ORACLE_NEXT_ACTION]
    oracle_next_action_indices = tf.where(
        tf.equal(env_output.observation[constants.CONN_IDS],
                 oracle_next_action))
    oracle_next_action_idx = tf.reduce_min(oracle_next_action_indices)
    assert self._mode, 'mode must be set.'
    if self._mode == 'train':
      if self._loss_type == common.CE_LOSS:
        # This is teacher-forcing mode, so choose action same as oracle action.
        action_idx = oracle_next_action_idx
      elif self._loss_type == common.AC_LOSS:
        # Choose next pano from probability distribution over next panos
        action_idx = tfp.distributions.Categorical(
            logits=agent_output.policy_logits).sample()
      else:
        raise ValueError('Unsupported loss type {}'.format(self._loss_type))
    else:
      # In non-train modes, choose greedily.
      action_idx = tf.argmax(agent_output.policy_logits, axis=-1)
    action_val = env_output.observation[constants.CONN_IDS][action_idx]
    return common.ActorAction(
        chosen_action_idx=int(action_idx.numpy()),
        oracle_next_action_idx=int(oracle_next_action_idx.numpy())), int(
            action_val.numpy()) 
Example #4
Source File: math_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def argmax(a, axis=None):
  return _argminmax(tf.argmax, a, axis) 
Example #5
Source File: util_test.py    From language with Apache License 2.0 5 votes vote down vote up
def config_model_evaluation(self, model, labels_ph, params=None):
    model.accuracy = tf1.metrics.accuracy(
        tf.argmax(input=model.labels, axis=1),
        tf.argmax(input=model.predicted_y.tf, axis=1))
    model.top_labels = util.labels_of_top_ranked_predictions_in_batch(
        model.labels, model.predicted_y.tf)
    model.precision_at_one = tf1.metrics.mean(model.top_labels)
    model.evaluations = {
        "accuracy": model.accuracy,
        "precision@1": model.precision_at_one
    } 
Example #6
Source File: gumbel_softmax.py    From agents with Apache License 2.0 5 votes vote down vote up
def convert_to_one_hot(self, samples):
    return tf.one_hot(
        tf.argmax(samples, axis=-1),
        self.distribution.event_size, dtype=self._output_dtype) 
Example #7
Source File: uniform_noise.py    From compression with Apache License 2.0 5 votes vote down vote up
def _quantization_offset(self):
    # Picks the "peakiest" of the component quantization offsets.
    offsets = helpers.quantization_offset(self.components_distribution)
    rank = self.batch_shape.rank
    transposed_offsets = tf.transpose(offsets, [rank] + list(range(rank)))
    component = tf.argmax(self.log_prob(transposed_offsets), axis=0)
    return tf.gather(offsets, component, axis=-1, batch_dims=rank) 
Example #8
Source File: language_reward_net.py    From valan with Apache License 2.0 5 votes vote down vote up
def predict_class(self, text_token_ids, action_panos):
    """Takes in an instruction and action and returns classifier outputs.

    Args:
      text_token_ids: Tensor of token indices for the input instruction.
      action_panos: Tensor of concatenated image panoramas.

    Returns:
      (class_outputs, predictions): Output of last layer of MLP and prediction.
    """
    text_enc_outputs, current_lstm_state = self.encode_instruction(
        text_token_ids)
    lstm_output, next_lstm_state = self.encode_action(current_lstm_state,
                                                      action_panos)
    lstm_output = tf.expand_dims(lstm_output, axis=1)
    batch_size = text_enc_outputs.shape[0]

    # c_text has shape [batch_size, 1, self._text_attention_size]
    c_text = self._text_attention([
        self._text_attention_project_hidden(lstm_output),
        self._text_attention_project_text(text_enc_outputs)
    ])
    # convert ListWrapper output of next_lstm_state to tuples
    result_state = []
    for one_state in next_lstm_state:
      result_state.append((one_state[0], one_state[1]))

    hidden_state = lstm_output
    c_visual = self._visual_attention([
        self._visual_attention_project_ctext(c_text),
        self._visual_attention_project_feature(action_panos),
    ])

    input_feature = tf.concat([hidden_state, c_text, c_visual], axis=2)
    class_outputs = self._mlp_layer(input_feature)
    class_outputs = tf.reshape(class_outputs, (batch_size, 2))
    predictions = tf.argmax(class_outputs, axis=-1)
    return (class_outputs, predictions) 
Example #9
Source File: language_reward_net.py    From valan with Apache License 2.0 5 votes vote down vote up
def accuracy(self, true_labels, predictions):
    """Takes in predictions and true labels and returns accuracy.

    Args:
      true_labels: a tensor of shape [batch_size, n_classes]
      predictions: a tensor of shape [batch_size, 1].

    Returns:
      loss: a scalar cross entropy loss.
    """
    true_labels = tf.keras.backend.argmax(true_labels)
    metric = tf.keras.metrics.Accuracy()
    accuracy = metric.update_state(true_labels, predictions)
    return accuracy