-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_loss.py
60 lines (47 loc) · 1.58 KB
/
test_loss.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
59
from scipy import sparse
import sys
import pywren
import numpy as np
import math
import boto3
import pickle
import time
import random
# Prediction function
def prediction(param_dense, param_sparse, x_dense, x_sparse):
val = -x_dense.dot(param_dense)
val2 = -x_sparse.dot(param_sparse)
val = val + val2
out = 1 / (1 + np.exp(val))
return out
# Log loglikelihood func
def loglikelihood(test_data, model):
xs_dense, xs_sparse, ys = test_data
param_dense, param_sparse = model
preds = prediction(param_dense, param_sparse, xs_dense, xs_sparse)
ys_temp = ys.reshape((-1, 1))
print("preds shape", preds.shape, "ys shape", ys_temp.shape)
cors = np.around(preds)
cors -= ys_temp
cors = np.abs(cors)
print("Accu", np.mean(cors))
tot = np.multiply(ys_temp, np.log(preds)) + np.multiply((1 - ys_temp), np.log(1 - preds))
return np.mean(tot)
def get_local_test_data():
f = open("testset.data", "rb")
f.seek(0)
x_dense_test, x_idx_test, y_test = pickle.load(f)
x_sparse_test = sparse.lil_matrix((x_dense_test.shape[0], 1000000))
for i in range(x_dense_test.shape[0]):
x_sparse_test[i, x_idx_test[i]] = np.ones(len(x_idx_test[i]))
f.close()
return (x_dense_test, x_sparse_test, y_test)
large_test = get_local_test_data()
#outf = open("tmp-0.000500-loss.csv", "w")
with open("tmp-0.000500-loss.pkl", 'rb') as f:
for i in range(300):
t, model = pickle.load(f)
error = loglikelihood(large_test, model)
print("wrote: %f %f" % (t, error))
# outf.write("%f, %f\n" % (t, error))
#outf.close()