In [17]:
import argparse
import os
import shutil
import gzip

import torch
import torch.nn as nn
from torch.autograd import Variable

import torch
import torch.nn as nn
from torch.autograd import Variable

from dpp_nets.utils.language import Vocabulary, BeerDataset, custom_collate
from dpp_nets.layers.baselines import NetBaseline, SetNetBaseline, AttentionBaseline

from dpp_nets.utils.language import EvalSet

In [3]:
# Load saved checkpoint
model = 'shortsentsNet1lr0.01best_ckp.pth.tar'
model_dir = '/Users/Max/checkpoints/beer_reviews/baseline/' 
model_path = model_dir + model
model = torch.load(model_path, map_location=lambda storage, loc: storage)

In [11]:
from dpp_nets.utils.language import Vocabulary

embd_path = '/Users/Max/data/beer_reviews/review+wiki.filtered.200.txt.gz'
word_path = '/Users/Max/data/beer_reviews/reviews.all.train.words.txt.gz'

# Set-up Vocabulary
vocab = Vocabulary()
vocab.loadPretrained(embd_path)
vocab.setStops()
vocab.loadCorpus(word_path)
vocab.updateEmbedding()
vocab.setCuda(False)
vocab.EmbeddingBag.load_state_dict(model['embedding'])

In [25]:
EMBD_DIM = 200
KERNEL_DIM = 200
HIDDEN_DIM = 500
ENC_DIM = 200
TARGET_DIM = 3 if model['aspect'] in set(['all', 'short']) else 1

if model['model_type'] == NetBaseline:
    trainer = NetBaseline(EMBD_DIM, HIDDEN_DIM, TARGET_DIM)
if model['model_type'] == SetNetBaseline:
    trainer = NetBaseline(EMBD_DIM, HIDDEN_DIM, KERNEL_DIM, ENC_DIM, TARGET_DIM)
if model['model_type'] == AttentionBaseline:
    trainer = SetNetBaseline(EMBD_DIM, HIDDEN_DIM, ENC_DIM, TARGET_DIM)

trainer.load_state_dict(model['model'])
#trainer.activation = nn.Sigmoid()

rat_path = '/Users/Max/data/beer_reviews/annotations.json'
evalset = EvalSet(rat_path, vocab)

In [37]:
nn.Sigmoid()(trainer(evalset.vocab.returnEmbds(evalset.chunks[6].clean.keys()).unsqueeze(0)))


Out[37]:
Variable containing:
 0.7287  0.6778  0.6752
[torch.FloatTensor of size 1x3]

In [44]:
print('__________________________Training Table__________________________')
for k, v in model['train_loss'].items():
    print(str.join(" | ", ['Epoch: %d' % (k), 'Loss: %.5f' % (v)]))


__________________________Training Table__________________________

In [22]:
from dpp_nets.helper.plotting import plot_floats

# Training Plots
plot_floats(model['train_loss'], xlabel='Epochs', ylabel='MSE + Reg', title='Training MSE + Reg')



In [24]:
print('_________________________Validation Table_________________________')
for k, v in model['val_loss'].items():
    print(str.join(" | ", ['Epoch: %d' % (k), 'Loss: %.5f' % (v)]))


_________________________Validation Table_________________________
Epoch: 0 | Loss: 0.04518 | Pred Loss: 0.04517 | Reg Loss: 0.00001
Epoch: 1 | Loss: 0.03518 | Pred Loss: 0.03518 | Reg Loss: 0.00001
Epoch: 2 | Loss: 0.03550 | Pred Loss: 0.03549 | Reg Loss: 0.00001
Epoch: 3 | Loss: 0.03805 | Pred Loss: 0.03804 | Reg Loss: 0.00001
Epoch: 4 | Loss: 0.03965 | Pred Loss: 0.03965 | Reg Loss: 0.00001
Epoch: 5 | Loss: 0.03997 | Pred Loss: 0.03996 | Reg Loss: 0.00001
Epoch: 6 | Loss: 0.03942 | Pred Loss: 0.03942 | Reg Loss: 0.00001
Epoch: 7 | Loss: 0.03848 | Pred Loss: 0.03847 | Reg Loss: 0.00001
Epoch: 8 | Loss: 0.03742 | Pred Loss: 0.03742 | Reg Loss: 0.00001
Epoch: 9 | Loss: 0.03636 | Pred Loss: 0.03635 | Reg Loss: 0.00001

In [16]:
from dpp_nets.helper.plotting import plot_floats

# Training Plots
plot_floats(model['val_loss'], xlabel='Epochs', ylabel='MSE + Reg', title='Validation MSE + Reg')



In [25]:
# Evaluation on Test Set

loss, pred_loss, reg_loss = evalset.computeLoss(trainer, model['mode'])
print(str.join(" | ", ['Test Set:', 'Loss: %.5f' % (loss), 
                              'Pred Loss: %.5f' % (pred_loss), 'Reg Loss: %.5f' % (reg_loss)]))

prec, extract = evalset.evaluatePrecision(trainer,model['mode'])
print(str.join(" | ", ['Test Set:', 'Precision: %.5f' % (prec), 'Extract: %.5f' % (extract)]))


Test Set: | Loss: 0.02419 | Pred Loss: 0.02419 | Reg Loss: 0.00001
Test Set: | Precision: 0.54628 | Extract: 0.01534

In [30]:
# Random Samples

evalset.sample(trainer, model['mode'])


index is: 398
('warming',) {'2'} [('warming',)]
Precision is: 1.0
Extraction Percentage is: 0.0047169811320754715
[(APPEARANCE: Split a bomber at Poppy's Pizza in Estas Park, Colorado after a fantastic meal. I'm not generally a huge fan of Moylan's labels, but I really liked the brown label on the Old Blarney., {'0'}), (It seemed to fit the style and the beer itself very well., {'0'}), (More importantly, though, once it left the bottle, the Old Blarney poured a deep, copper-red with a thick, foamy and off-white head., {'0'}), (Good looking.		, {'0'}), (SMELL: Dynamite smell with some strong chocolate flavors and a some sweet malty notes as well.		, {'1'}), (TASTE: Quite hoppy for a Barleywine with as much malt as I smelled in it, which is a good thing., set()), (Again, there was plenty of chocolate present along with some sweet notes, but it finished much more dry than most Barleywines I've had.		, set()), (MOUTHFEEL: Silky and smooth on the way down, this had a nice, warming mouthcoating feel. 		, {'2'}), (OVERALL: After debating between two Barleywines for dessert I am very happy we went this direction., set()), (It was an excellent choice to finish off a fantastic meal., set())]

In [31]:
# Random Marginals
evalset.computeMarginals(trainer, model['mode'])


index is: 566
0 0.0321089721478 ('beer',)
1 0.0301501835829 ('well',)
2 0.0289074557399 ('glass',)
3 0.0287432763671 ('dark',)
4 0.0285714722791 ('past',)
5 0.0281547184646 ('slowly',)
6 0.0278983182832 ('not',)
7 0.0276435325499 ('dunkel',)
8 0.0276310050741 ('bitter',)
9 0.0272011825588 ('foamy',)
10 0.0271149779038 ('sweet',)
11 0.0270507410922 ('average',)
12 0.0268991972972 ('rounded',)
13 0.0268183677794 ('smooth',)
14 0.0267459773739 ('ultimately',)
15 0.0265797391114 ("'m",)
16 0.0262425629607 ('finish',)
17 0.0259093635043 ('decent',)
18 0.0258904284311 ('combination',)
19 0.0258516595406 ('style',)
20 0.0255656166061 ('original',)
21 0.0253490805678 ('overall',)
22 0.0252626188986 ('lacing',)
23 0.0252447199226 ('sure',)
24 0.0251121449743 ('perfectly',)
25 0.0251038665038 ('one',)
26 0.025017682948 ('flavor',)
27 0.0249640673322 ('detect',)
28 0.0248376633827 ('good',)
29 0.0248372171674 ('since',)
30 0.0241694743005 ('fairly',)
31 0.0240776539127 ('thick',)
32 0.0240373135915 ('thin',)
33 0.0240007084318 ('scent',)
34 0.0236788012152 ('nose',)
35 0.0234249498745 ('mouthfeel',)
36 0.0229235778101 ('time', 'time')
37 0.0229235778101 ('time',)
38 0.0225482938988 ('clean', 'smooth')
39 0.0223775280026 ('definitely',)
40 0.021762525481 ('aroma',)
41 0.0217327694354 ('sweet', 'bitter')
42 0.0210721297343 ('sweet', 'malts')
43 0.0196122154855 ('malty', 'sweet', 'bitter')
44 0.0194763386178 ('hard', 'detect')
45 0.0193894285787 ('average', 'rating')
46 0.0189943287446 ('perfectly', 'honest')
47 0.0189374933077 ('finish', 'clean', 'smooth')
48 0.0186503127314 ('average', 'style')
49 0.0186200202832 ('pick', 'scent')
50 0.0181158261205 ('drinking', 'time', 'time')
51 0.0180976663485 ('thin', 'film')
52 0.0178555866146 ('aroma', 'malty', 'sweet', 'bitter')
53 0.0173293610266 ('original', 'lager')
54 0.0171624042 ('altered', 'sweet', 'malts')
55 0.0165237258689 ('enjoy', 'drinking', 'time', 'time')
56 0.0160814097901 ('since', 'not', 'see', 'glass')
57 0.0160643861946 ('categories', 'average', 'style')
58 0.015739384934 ('ultimately', 'turns', 'thin', 'film')
59 0.0156644728558 ('order', 'pick', 'scent')
60 0.0153684538816 ('continue', 'enjoy', 'drinking', 'time', 'time')
61 0.0152455059704 ('appears', 'altered', 'sweet', 'malts')
62 0.0151376258015 ('thick', 'foamy', 'head', 'ultimately', 'turns', 'thin', 'film')
63 0.0151306333367 ('overall', 'taste', 'one')
64 0.01505866589 ('aroma', 'hard', 'detect')
65 0.0147894735633 ('enjoyed', 'beer', 'past', 'continue', 'enjoy', 'drinking', 'time', 'time')
66 0.0147586724491 ('produces', 'thick', 'foamy', 'head', 'ultimately', 'turns', 'thin', 'film')
67 0.014592072953 ('not', 'thin', 'finish', 'clean', 'smooth')
68 0.0145241807618 ('not', 'able', 'determine', 'carbonation', 'since', 'not', 'see', 'glass')
69 0.0143495031307 ('fairly', 'solid', 'categories', 'average', 'style')
70 0.0142930088086 ('well', 'rounded', 'beer', 'style')
71 0.0141342660685 ('shove', 'nose', 'glass', 'order', 'pick', 'scent')
72 0.0140464630209 ('able', 'determine', 'carbonation', 'since', 'not', 'see', 'glass')
73 0.014039453268 ('mouthfeel', 'decent', 'not', 'thin', 'finish', 'clean', 'smooth')
74 0.0139915426651 ('carbonation', 'since', 'not', 'see', 'glass')
75 0.013985303382 ('average', 'dunkel', 'fairly', 'solid', 'categories', 'average', 'style')
76 0.013951111723 ('determine', 'carbonation', 'since', 'not', 'see', 'glass')
77 0.0139250402137 ('dark', 'dunkel', 'produces', 'thick', 'foamy', 'head', 'ultimately', 'turns', 'thin', 'film')
78 0.0137671288766 ('sure', 'enjoy', 'overall', 'taste', 'one')
79 0.0132862112891 ('combination', 'good', 'sure', 'enjoy', 'overall', 'taste', 'one')
80 0.0132454564176 ('lacing', 'slowly', 'creeps', 'glass', 'not', 'able', 'determine', 'carbonation', 'since', 'not', 'see', 'glass')
81 0.0131885180579 ('perfectly', 'honest', 'average', 'dunkel', 'fairly', 'solid', 'categories', 'average', 'style')
82 0.0130817876469 ('aroma', 'hard', 'detect', "'m", 'forced', 'shove', 'nose', 'glass', 'order', 'pick', 'scent')
83 0.0125855799459 ('gave', 'average', 'rating', 'aroma', 'hard', 'detect', "'m", 'forced', 'shove', 'nose', 'glass', 'order', 'pick', 'scent')
84 0.0125738338456 ('flavor', 'definitely', 'based', 'original', 'lager', 'appears', 'altered', 'sweet', 'malts')