This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import torch | |
def predict(question, passage): | |
sequence = tokenizer.encode_plus(question, passage, return_tensors="pt", | |
max_length=512, truncation=True)['input_ids'].to(device) | |
logits = model(sequence)[0] | |
probabilities = torch.softmax(logits, dim=1).detach().cpu().tolist()[0] | |
vector = logits.detach().cpu().tolist()[0] | |
n = (vector[0]/2.75 + 1)/2 | |
y = (vector[1]/2.75 + 1)/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Passage: The Magic 8-Ball is a plastic sphere, made to look like an eight-ball, that is used for | |
fortune-telling or seeking advice. It was invented in 1950 by Albert C. Carter and Abe Bookman | |
and is currently manufactured by Mattel. The user asks a yes–no question to the ball and then | |
turns it over to reveal an answer in a window on the ball | |
Question: Is the Magic 8-Ball a sphere?, Yes: 0.99, No: 0.01 Conf.: 0.98 | |
Question: Is the Magic 8-Ball a cube?, Yes: 0.11, No: 0.89 Conf.: 0.42 | |
Question: Was the Magic 8-Ball invented in 1940?, Yes: 0.04, No: 0.96 Conf.: 0.64 | |
Question: Was the Magic 8-Ball invented in 1950?, Yes: 0.98, No: 0.02 Conf.: 0.77 | |
Question: Was the Magic 8-Ball invented by Carter and Bookman?, Yes: 0.97, No: 0.03 Conf.: 0.71 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tqdm import tqdm | |
batch_size = 8 | |
epochs = 3 | |
grad_acc_steps = 4 | |
train_loss_values = [] | |
dev_acc_values = [] | |
for _ in tqdm(range(epochs), desc="Epoch"): | |
# Training | |
epoch_train_loss = 0 | |
model.train() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytextrank | |
import spacy | |
nlp = spacy.load("en_core_web_sm") | |
tr = pytextrank.TextRank() | |
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True) | |
question = "Can a computer beat a grandmaster chess player?" | |
query = "" | |
keywords = nlp(question.lower()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wikipedia | |
print("Checking the Wikipedia.") | |
results = wikipedia.search(query, results = 3) | |
wiki_passage = "" | |
for r in results[1:]: | |
try: | |
s = wikipedia.summary(r) | |
except: | |
continue | |
wiki_passage += s.strip() + " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
min_dist = float("inf") | |
pick = 0 | |
if (conf < 0.5): | |
map_conf = 1 + conf * 19 / 2 | |
for i, a in enumerate(answers[:5]): | |
c = a[1][1] | |
distance = abs(map_conf-c) | |
if distance < min_dist: | |
min_dist = distance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
conf = 0 | |
yes = 0.5 | |
passage = "" | |
source = "no source" | |
if (wiki_conf > nyt_conf and wiki_conf > boolq_conf): | |
yes = wiki_yes | |
conf = wiki_conf | |
passage = wiki_passage | |
source = "Wikipedia" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gensim.models import Word2Vec | |
syllModel = Word2Vec.load(syll_model_path) | |
wordModel = Word2Vec.load(word_model_path) | |
import pyphen | |
dic = pyphen.Pyphen(lang='en_US') | |
poem = '''A plow, they say, to plow the snow. | |
They cannot mean to plant it, no– | |
Unless in bitterness to mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import music21 | |
def transpose_notes(notes, new_key): | |
midi_stream = music21.stream.Stream(notes) | |
key = midi_stream.analyze('key') | |
interval = music21.interval.Interval(key.tonic, new_key.tonic) | |
new_stream = midi_stream.transpose(interval) | |
return new_stream.notes | |
# quantize the start times and note durations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
length_song = len(lyrics) | |
cond = [] | |
for i in range(20): | |
if i < length_song: | |
syll2Vec = syllModel.wv[lyrics[i][0]] | |
word2Vec = wordModel.wv[lyrics[i][1]] | |
cond.append(np.concatenate((syll2Vec, word2Vec))) | |
else: | |
cond.append(np.concatenate((syll2Vec, word2Vec))) |