-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_numpy_logreg.py
60 lines (42 loc) · 1.46 KB
/
test_numpy_logreg.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
60
import numpy as np
import theano
from numpy_impl import LogisticRegression
from logreg import LogisticRegression as TheanoLogisticRegression
from test_util import assert_matrix_eq
#########################
# NUMPY PART
#########################
# 5 labels and 10 inputs
W = np.random.rand(10, 5)
b = np.random.rand(5)
x = np.random.rand(3, 10)
y = np.asarray(np.random.randint(5, size = 3),
dtype=np.int32
)
np_l = LogisticRegression(W, b)
#########################
# THEANO PART
#########################
x_symbol = theano.tensor.dmatrix('x')
y_symbol = theano.tensor.ivector('y')
th_l = TheanoLogisticRegression(rng = np.random.RandomState(1234),
input = x_symbol,
n_in = 10,
n_out = 5,
W = theano.shared(value = W,
name = "W"),
b = theano.shared(value = b,
name = "b")
)
f1 = theano.function(inputs = [x_symbol, y_symbol],
outputs = th_l.nnl(y_symbol)
)
actual = np_l.nnl(x, y)
expected = f1(x, y)
assert_matrix_eq(actual, expected, "nnl")
f2 = theano.function(inputs = [x_symbol, y_symbol],
outputs = th_l.errors(y_symbol)
)
actual = np_l.errors(x, y)
expected = f2(x, y)
assert_matrix_eq(actual, expected, "errors")