Пример #1
0
def reconstruct_changed_activation(device, tokenizer, emb_map, model):
    """Reconstruct the activation for a given sentence after they have been shifted.

  Args:
    device: The device to use for training the model.
    tokenizer: Used to convert between sentences, tokens, and ids.
    emb_map: Map containing all the pretrained embeddings of the model.
    model: BERT model used for the dreaming process.
  """
    data = []
    results = {}
    params = {}
    # Create a folder for this experiment
    layer_dir = os.path.join(FLAGS.output_dir, str(FLAGS.layer_id))
    folder_helper.make_folder_if_not_exists(layer_dir)
    # Actually do the optimization
    deep_dream(data, results, params, device, tokenizer, emb_map, model)
    # If the top k file is to be written, write it
    if FLAGS.write_top_k:
        for i in range(len(data)):
            top_k_path = os.path.join(layer_dir, 'top_k' + str(i) + '.json')
            top_k_file = open(top_k_path, 'w')
            json.dump(data[i], top_k_file)
            top_k_file.close()
    output_helper.write_results(layer_dir, results, params,
                                'reconstruct_changed')
Пример #2
0
def get_dream(device, tokenizer, embedding_map, model, base_path):
  """Obtain a dream from the modle given the parameters passed by the user.

  Args:
    device: The device to use for training the model.
    tokenizer: Used to convert between sentences, tokens, and ids.
    embedding_map: Map containing all the pretrained embeddings of the model.
    model: BERT model used for the dreaming process.
    base_path: Location of where to write the results.
  """
  results = {}
  params = {}
  # Actually do the optimization
  deep_dream(results, params, device, tokenizer, embedding_map, model)
  output_helper.write_results(base_path, results, params, 'dream_mlm')
Пример #3
0
def reconstruct_activation(device, tokenizer, embedding_map, model, base_path):
    """Reconstruct the activation for a given sentence.

  Args:
    device: The device to use for training the model.
    tokenizer: Used to convert between sentences, tokens, and ids.
    embedding_map: Map containing all the pretrained embeddings of the model.
    model: BERT model used for the dreaming process.
    base_path: Location of where to write the results.
  """
    data = []
    results = {}
    params = {}
    # Actually do the optimization
    deep_dream(data, results, params, device, tokenizer, embedding_map, model)
    # If the top k file is to be written, write it
    if FLAGS.write_top_k:
        output_helper.write_top_ks(base_path, data, FLAGS.dream_start, params)
    output_helper.write_results(base_path, results, params, 'reconstruct')