class_docstr: Trainer for an agreement model that always predicts the correct value.
class_docstr_tok: ["Trainer","for","an","agreement","model","that","always","predicts","the","correct","value","."]
docstr: Predict class labels using agreement with other labeled samples.
  
  Uses the agreement model to compute the agreement of a test sample with a
  subset of the labeled samples. Then it calculates the label distribution
  as a weighted average of the labeled samples, using the predicted agreement
  scores as weights.
  
  Args:
    indices: A list of integers representing the indices of the test samples
      to label.
    num_neighbors: An integer representing the number of labeled samples to
      compare each test sample with. The higher this number, the more accurate
      the predictions, but also the more expensive.
    **unused_kwargs: Other keyword arguments that may be provided just to have
      a simiar interface as TrainerAgreement, when calling
      predict_label_by_agreement from the classification model.
  
  Returns:
    acc: The accuracy of this agreement based classifier on the provided
      sample indices.
code: def predict_label_by_agreement(
      self, indices, num_neighbors=100, **unused_kwargs
  ):
      """Predict class labels using agreement with other labeled samples.
  
      Uses the agreement model to compute the agreement of a test sample with a
      subset of the labeled samples. Then it calculates the label distribution
      as a weighted average of the labeled samples, using the predicted agreement
      scores as weights.
  
      Args:
        indices: A list of integers representing the indices of the test samples
          to label.
        num_neighbors: An integer representing the number of labeled samples to
          compare each test sample with. The higher this number, the more accurate
          the predictions, but also the more expensive.
        **unused_kwargs: Other keyword arguments that may be provided just to have
          a simiar interface as TrainerAgreement, when calling
          predict_label_by_agreement from the classification model.
  
      Returns:
        acc: The accuracy of this agreement based classifier on the provided
          sample indices.
      """
      # Limit the number of labeled samples to compare with, for efficiency
      # reasons. At the moment we pick a random subset of labeled samples, but
      # perhaps there better ways (e.g. the closest samples in embedding space).
      train_indices = np.asarray(list(self.data.get_indices_train()))
      if len(train_indices) > num_neighbors:
          np.random.shuffle(train_indices)
          train_indices = train_indices[:num_neighbors]
      num_labeled = train_indices.shape[0]
      train_labels = self.data.get_labels(train_indices)
      train_labels_1hot = np.zeros((num_labeled, self.data.num_classes))
      train_labels_1hot[np.arange(num_labeled), train_labels] = 1
      train_labels_original = self.data.get_original_labels(
          train_indices
      )
      # For each sample for which we want to make predictions, we compute the
      # agreement with all selected labeled samples.
      acc = 0.0
      for index_u in indices:
          label_u = self.data.get_original_labels(index_u)
          agreement = train_labels_original == label_u
          # Cummulate the agreement weights per label.
          vote_per_label = np.sum(train_labels_1hot[agreement], axis=0)
          is_correct = np.argmax(
              vote_per_label
          ) == self.data.get_original_labels(index_u)
          acc += is_correct
      if indices:
          acc /= len(indices)
      logging.info("Majority vote accuracy: %.2f.", acc)
      return acc
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import logging
  import os
  from ..data.preprocessing import split_train_val
  import numpy
  import tensorflow
  from .trainer_base import batch_iterator
  from .trainer_base import Trainer
label_desc: Use image node_id 5 for calling the TrainerPerfectAgreement obj's underlying member method code with example usage: obj.predict_label_by_agreement(indices, num_neighbors) and returns: acc