Anon / Aug 23 2019

HTML Display - NextJournal

pth = '/.nextjournal/data-named/QmXM1SAUDr39KzBUo4rkFn9VTti8hSGTneAsShAdL6VAcG/'
file = 'Headlines.csv'
D_COL = 'headline'
WEIGHTS = 
Headlines_Glove_Weights.pkl
PADDING = 64
exec(open(
dweNet.py
).read())
from fastai import *
from fastai.text import *
from fastai.callbacks import *
import torch.utils.data as data_utils
import numpy
defaults.device = torch.device('cuda')
def pad_to(x:Collection[str], pad_til = PADDING) -> Collection[str]:
    res = []
    count = 0
    for t in x:
        res.append(t)
        count += 1
    while count < pad_til:
        res.append(PAD)
        count +=1
    return res
tokenizer = Tokenizer(SpacyTokenizer, 'en', pre_rules=
                      [fix_html, replace_rep, replace_wrep, spec_add_spaces, 
                       rm_useless_spaces],
                      post_rules=[replace_all_caps, deal_caps, pad_to], n_cpus=1)
processor = [TokenizeProcessor(tokenizer=tokenizer), NumericalizeProcessor()]
data = (TextList.from_csv(pth, file, cols=D_COL, processor=processor)).split_from_df(col='valid').label_from_df(cols=0).databunch()
batch = data.show_batch()

# HTML Output works for Jupyter kernel but not for NextJournal. Expect to see a batch of the data below.
batch
weights_matrix = pickle.load(open(WEIGHTS, 'rb'))
net =  DenseNet(weights_matrix) 
net.to(DEVICE);
DenseNet( (...tput_size=1) )
learn = Learner(data, net, wd=0.1, loss_func=CrossEntropyFlat(), 
                metrics=[accuracy, FBeta(average='micro',beta=1)])
learn.fit_one_cycle(1, 1e-03, moms=(0.8,0.7))

# HTML Output works for NextJournal but not Jupyter
preds,y,losses = learn.get_preds(with_loss=True)
interp = ClassificationInterpretation(learn, preds, y, losses)
interp.plot_confusion_matrix()

# This has an issue in both Jupyter and NextJournal. Doesn't plot automatically - I usually just run this line in Jupyter on my end and it renders the plot. I can get it to be returned as a figure, and then try plot it manually, which works for NJ but not Jupyter.
fig = interp.plot_confusion_matrix(return_fig=True)
fig