-
Notifications
You must be signed in to change notification settings - Fork 41
/
predict_no_batches.py
60 lines (43 loc) · 1.16 KB
/
predict_no_batches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# coding: utf-8
"""
prediction code without batches
see http://fastml.com/how-to-get-predictions-from-pylearn2/
author: Zygmunt Zając
"""
import sys
import os
import numpy as np
import cPickle as pickle
from pylearn2.utils import serial
from theano import tensor as T
from theano import function
try:
model_path = sys.argv[1]
test_path = sys.argv[2]
out_path = sys.argv[3]
except IndexError:
print "Usage: predict.py <model file> <test file> <output file>"
print " predict.py saved_clf.pkl saved_tst.pkl results.csv\n"
quit()
print "loading model..."
try:
model = serial.load(model_path)
except Exception, e:
print model_path + "doesn't seem to be a valid model path, got this error when trying to load it:"
print e
print "setting up symbolic expressions..."
X = model.get_input_space().make_theano_batch()
Y = model.fprop(X)
Y = T.argmax(Y, axis=1)
f = function([X], Y)
print "loading data and predicting..."
x = pickle.load(open(test_path, 'rb'))
y = f(x)
print "writing predictions..."
out = open(out_path, 'w')
out.write('id,label\n')
for i in xrange(y.shape[0]):
p = y[i]
out.write('{},{}\n'.format(i + 1, p))
out.close()