-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.py
62 lines (52 loc) · 1.84 KB
/
test.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
61
62
import unittest
import tensorflow as tf
import numpy.testing as test
from layers import SpatialPooler
class SPTest(unittest.TestCase):
def test_call(self):
"""
Test forward computation
"""
layer = SpatialPooler(4, pool_density=1, boost_strength=0)
x = tf.placeholder(tf.float32, [None, 4], name='Input')
y = layer(x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Override permenance with a custom value
sess.run(tf.assign(layer.p, [
[1, 0, 1, 1],
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 0, 1, 1]
]))
# Compute
result = sess.run(y, { x: [[1, 1, 0, 1], [1, 1, 0, 0]] })
test.assert_array_equal(result[0], [0, 0, 1, 0])
def test_train(self):
"""
Test forward computation
"""
layer = SpatialPooler(4, lr=0.1, pool_density=1, boost_strength=0)
x = tf.placeholder(tf.float32, [None, 4], name='Input')
layer.build([1, 4])
train = layer.train(x, tf.constant([[0., 1, 0, 1]]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Override permenance with a custom value
sess.run(tf.assign(layer.p, [
[0.6, 0, 0.6, 0.6],
[0, 0.6, 0, 0],
[0.6, 0.6, 0.6, 0],
[0, 0, 0.6, 0.6]
]))
# Compute
result = sess.run(train, { x: [[1, 1, 0, 1]] })
# Check the new permenance
test.assert_allclose(result[0], [
[0.6, 0, 0.6, 0.7],
[0, 0.7, 0, 0 ],
[0.6, 0.5, 0.6, 0 ],
[0, 0, 0.6, 0.7]
])
if __name__ == '__main__':
unittest.main()