Skip to content

Instantly share code, notes, and snippets.

@danyaljj
danyaljj / generation_quality_vs_prompt_peaky-ness.py
Created June 29, 2021 02:40
prompting gpt2 with "soft" prompts
from torch.distributions import Categorical
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch
import torch.nn.functional as F
def embed_inputs(embedding, logits, device='cuda', print_entropy=False):
'''
embeds inputs in a dense representation, before passing them to the model
'''
from transformers import MT5Config, MT5ForConditionalGeneration, MT5Tokenizer
from transformers.models.t5.modeling_t5 import load_tf_weights_in_t5
model_name = "persiannlp/mt5-base-parsinlu-opus-translation_fa_en"
tokenizer = MT5Tokenizer.from_pretrained(model_name)
model = MT5ForConditionalGeneration.from_pretrained(model_name)
def run_model(input_string, **generator_args):
input_ids = tokenizer.encode(input_string, return_tensors="pt")
res = model.generate(input_ids, **generator_args)
#!/usr/bin/env python
from typing import Iterable
from collections import Counter
import os
import logging
import sys
import json
import click
import datasets
import numpy as np
import json
import argparse
from typing import Optional, Union, Tuple
import torch
torch.manual_seed(0)
from transformers import BertModel, BertTokenizer, PreTrainedModel, BertConfig
from transformers.modeling_outputs import MultipleChoiceModelOutput
@danyaljj
danyaljj / sort_map_js
Created July 16, 2015 23:03
Sort map by its keys and return another map in Javascript
sortMapByValue(map) {
var tupleArray = [];
for (var key in map) tupleArray.push([key, map[key]]);
tupleArray.sort(function (a, b) {
return b[1] - a[1]
});
var sortedMap = {};
tupleArray.forEach(function (el) {
sortedMap[el[0]] = el[1]
});
import json
import os
all_types_to_idx = {
'Task': 0,
'Method': 1,
'Material': 2,
'Metric': 3,
'OtherScientificTerm': 4,
'Generic': 5
import random
from collections import Counter
from urllib.parse import urlparse
import json
import os
import re
from tqdm import tqdm
urls_counts = {}
from typing import List
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import scipy
import random
class NormalGammaPrior():
""""
Suppose X is distributed according to a normal distribution: X ~ N(mu, tau^{-1})
import statistics as st
import scipy.stats
import numpy as np
def metric1(scores, row_aggregator, column_aggregator, cell_aggregator):
row_values = []
for row_idx, row1 in enumerate(scores):
diagonal_x = row1[row_idx]
row_values.append(
show_unpublished_scores: true
datasets:
blind_labels: danielk/genie_labels
evaluator:
image: jbragg/genie-evaluator
input_path: /preds/
predictions_filename: predictions.json
label_path: /labels/
output_path: /results
arguments: