Skip to content

Instantly share code, notes, and snippets.

@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]']
@Tafkas
Tafkas / rmsle.py
Created November 25, 2013 14:37
Compute the Root Mean Squared Log Error for hypothesis h and targets y
import numpy as np
def rmsle(h, y):
"""
Compute the Root Mean Squared Log Error for hypthesis h and targets y
Args:
h - numpy array containing predictions with shape (n_samples, n_targets)
y - numpy array containing targets with shape (n_samples, n_targets)
"""