Skip to content

Instantly share code, notes, and snippets.

View roeeaharoni's full-sized avatar

Roee Aharoni roeeaharoni

View GitHub Profile
@roeeaharoni
roeeaharoni / BERT_science.py
Last active November 17, 2020 04:06
Generate the sciences of the future using BERT! (as seen on https://twitter.com/roeeaharoni/status/1089089393745371136)
import torch
from pytorch_pretrained_bert import BertForMaskedLM, BertTokenizer
import random
# Requires pytorch_pretrained_bert: https://github.com/huggingface/pytorch-pretrained-BERT
# returns the probabilities over the vocabulary for the masked words in sent
def get_preds(sent):
tokenized = bert_tokenizer.tokenize(sent)
tokenized = ['[CLS]'] + ['[MASK]' if x == 'mask' else x for x in tokenized] + ['[SEP]']