def compute_tcav_score()

in tcav/tcav.py [0:0]


  def compute_tcav_score(mymodel,
                         target_class,
                         concept,
                         cav,
                         class_acts,
                         examples,
                         run_parallel=True,
                         num_workers=20):
    """Compute TCAV score.

    Args:
      mymodel: a model class instance
      target_class: one target class
      concept: one concept
      cav: an instance of cav
      class_acts: activations of the examples in the target class where
        examples[i] corresponds to class_acts[i]
      examples: an array of examples of the target class where examples[i]
        corresponds to class_acts[i]
      run_parallel: run this parallel fashion
      num_workers: number of workers if we run in parallel.

    Returns:
        TCAV score (i.e., ratio of pictures that returns negative dot product
        wrt loss).
    """
    count = 0
    class_id = mymodel.label_to_id(target_class)
    if run_parallel:
      pool = multiprocessing.Pool(num_workers)
      directions = pool.map(
          lambda i: TCAV.get_direction_dir_sign(
              mymodel, np.expand_dims(class_acts[i], 0),
              cav, concept, class_id, examples[i]),
          range(len(class_acts)))
      pool.close()
      return sum(directions) / float(len(class_acts))
    else:
      for i in range(len(class_acts)):
        act = np.expand_dims(class_acts[i], 0)
        example = examples[i]
        if TCAV.get_direction_dir_sign(
            mymodel, act, cav, concept, class_id, example):
          count += 1
      return float(count) / float(len(class_acts))