Skip to content

Instantly share code, notes, and snippets.

@Bachfischer
Last active January 3, 2019 20:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bachfischer/f26e086ad84fd7cc778429d7b86a4ea9 to your computer and use it in GitHub Desktop.
Save Bachfischer/f26e086ad84fd7cc778429d7b86a4ea9 to your computer and use it in GitHub Desktop.
fast.ai - manual prediction
# single prediction on last classifier
learn.load('clas_2')
m = learn.model
#set batch size to 1
m[0].bs=1
#turn off dropout
m.eval()
#reset hidden state
m.reset()
#sentence to be classified
sen = "guten morgen schon novemberlicht da mag man gar nicht glauben was gestern wetter war"
idxs = np.array([[stoi[p] for p in sen.strip().split(" ")]])
#converting sentence into numerical representation
print('numeric representation: ' + str(idxs))
idxs = np.transpose(idxs)
print('Value of idxs: ' + str(idxs))
#get predictions from model
p = m(VV(idxs))
print('Prediction: ' + str(p))
print(sen + ' - classification: ' + str(to_np(torch.topk(p[0],1)[1])[0]))
prob = F.softmax(p[0])
print('Probability rating for sentence: ' + str(prob))
numeric representation: [[ 1611 2295 179 0 97 5670 96 1668 47 2236 180 28919 5816 33]]
Value of idxs: [[ 1611]
[ 2295]
[ 179]
[ 0]
[ 97]
[ 5670]
[ 96]
[ 1668]
[ 47]
[ 2236]
[ 180]
[28919]
[ 5816]
[ 33]]
Prediction: (Variable containing:
0.2814 -0.0482 -0.0967
[torch.FloatTensor of size 1x3]
, [Variable containing:
( 0 ,.,.) =
3.2617e-04 1.3194e-03 3.9376e-06 ... 7.5627e-04 2.5742e-04 4.6105e-04
( 1 ,.,.) =
5.3593e-04 -2.3461e-04 -7.1204e-05 ... 6.6057e-05 1.1183e-06 -7.9078e-05
( 2 ,.,.) =
2.8301e-07 -5.0946e-04 2.8877e-05 ... 4.4297e-04 -1.3625e-05 -6.2073e-07
...
( 11 ,.,.) =
-4.2971e-09 1.4740e-01 -2.1405e-01 ... -1.3160e-01 -3.5512e-04 1.3877e-01
( 12 ,.,.) =
-7.4684e-04 -4.1670e-01 -6.1688e-06 ... -1.2905e-04 -2.9628e-05 3.0681e-02
( 13 ,.,.) =
-1.8500e-05 -5.9534e-04 -2.3576e-07 ... -1.4994e-06 -2.1938e-05 -2.2080e-04
[torch.FloatTensor of size 14x1x1150]
, Variable containing:
( 0 ,.,.) =
-7.5986e-03 8.6798e-04 -6.7351e-03 ... 6.2188e-04 3.4969e-02 -9.1077e-02
( 1 ,.,.) =
-2.3921e-03 2.4249e-03 -1.7347e-03 ... 1.0938e-02 -3.6272e-04 -1.5099e-02
( 2 ,.,.) =
-2.7945e-02 9.5424e-04 -2.8550e-03 ... 9.9346e-05 -4.6923e-03 -1.5932e-02
...
( 11 ,.,.) =
7.7081e-05 1.1043e-04 3.5880e-03 ... 3.4046e-03 -3.2344e-01 -7.6625e-04
( 12 ,.,.) =
3.6866e-03 3.5075e-03 7.5826e-04 ... -2.6639e-02 -2.2028e-01 -2.5222e-03
( 13 ,.,.) =
-1.3276e-02 3.8389e-02 1.6594e-04 ... -2.6982e-03 -1.2232e-01 -6.5385e-03
[torch.FloatTensor of size 14x1x1150]
, Variable containing:
( 0 ,.,.) =
-3.4815e-02 1.2264e-03 2.1081e-01 ... 1.3807e-01 -1.8228e-01 -4.8058e-03
( 1 ,.,.) =
-3.8202e-02 -1.0102e-01 1.4506e-01 ... 7.9288e-03 -1.2543e-01 -4.7196e-03
( 2 ,.,.) =
-2.1076e-02 -5.5695e-02 1.0110e-01 ... 9.5715e-03 -9.4924e-02 -1.6425e-03
...
(11 ,.,.) =
-1.6449e-02 -8.2472e-04 3.1275e-02 ... 2.5666e-02 -9.2902e-02 -1.3512e-01
(12 ,.,.) =
-3.0238e-02 -2.3225e-03 1.2036e-01 ... 1.6667e-02 1.6427e-01 -2.0484e-02
(13 ,.,.) =
-2.2449e-01 -2.6965e-03 3.8396e-02 ... 1.3158e-03 4.0717e-02 -1.3653e-02
[torch.FloatTensor of size 14x1x400]
], [Variable containing:
( 0 ,.,.) =
3.2617e-04 1.3194e-03 3.9376e-06 ... 7.5627e-04 2.5742e-04 4.6105e-04
( 1 ,.,.) =
5.3593e-04 -2.3461e-04 -7.1204e-05 ... 6.6057e-05 1.1183e-06 -7.9078e-05
( 2 ,.,.) =
2.8301e-07 -5.0946e-04 2.8877e-05 ... 4.4297e-04 -1.3625e-05 -6.2073e-07
...
( 11 ,.,.) =
-4.2971e-09 1.4740e-01 -2.1405e-01 ... -1.3160e-01 -3.5512e-04 1.3877e-01
( 12 ,.,.) =
-7.4684e-04 -4.1670e-01 -6.1688e-06 ... -1.2905e-04 -2.9628e-05 3.0681e-02
( 13 ,.,.) =
-1.8500e-05 -5.9534e-04 -2.3576e-07 ... -1.4994e-06 -2.1938e-05 -2.2080e-04
[torch.FloatTensor of size 14x1x1150]
, Variable containing:
( 0 ,.,.) =
-7.5986e-03 8.6798e-04 -6.7351e-03 ... 6.2188e-04 3.4969e-02 -9.1077e-02
( 1 ,.,.) =
-2.3921e-03 2.4249e-03 -1.7347e-03 ... 1.0938e-02 -3.6272e-04 -1.5099e-02
( 2 ,.,.) =
-2.7945e-02 9.5424e-04 -2.8550e-03 ... 9.9346e-05 -4.6923e-03 -1.5932e-02
...
( 11 ,.,.) =
7.7081e-05 1.1043e-04 3.5880e-03 ... 3.4046e-03 -3.2344e-01 -7.6625e-04
( 12 ,.,.) =
3.6866e-03 3.5075e-03 7.5826e-04 ... -2.6639e-02 -2.2028e-01 -2.5222e-03
( 13 ,.,.) =
-1.3276e-02 3.8389e-02 1.6594e-04 ... -2.6982e-03 -1.2232e-01 -6.5385e-03
[torch.FloatTensor of size 14x1x1150]
, Variable containing:
( 0 ,.,.) =
-3.4815e-02 1.2264e-03 2.1081e-01 ... 1.3807e-01 -1.8228e-01 -4.8058e-03
( 1 ,.,.) =
-3.8202e-02 -1.0102e-01 1.4506e-01 ... 7.9288e-03 -1.2543e-01 -4.7196e-03
( 2 ,.,.) =
-2.1076e-02 -5.5695e-02 1.0110e-01 ... 9.5715e-03 -9.4924e-02 -1.6425e-03
...
(11 ,.,.) =
-1.6449e-02 -8.2472e-04 3.1275e-02 ... 2.5666e-02 -9.2902e-02 -1.3512e-01
(12 ,.,.) =
-3.0238e-02 -2.3225e-03 1.2036e-01 ... 1.6667e-02 1.6427e-01 -2.0484e-02
(13 ,.,.) =
-2.2449e-01 -2.6965e-03 3.8396e-02 ... 1.3158e-03 4.0717e-02 -1.3653e-02
[torch.FloatTensor of size 14x1x400]
])
guten morgen schon novemberlicht da mag man gar nicht glauben was gestern wetter war - classification: [0]
Probability rating for sentence: Variable containing:
0.4159 0.2991 0.2850
[torch.FloatTensor of size 1x3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment