-
Notifications
You must be signed in to change notification settings - Fork 214
/
ops_test.py
54 lines (45 loc) · 1.81 KB
/
ops_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
"""Tests for ops."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from tensorflow.python.ops import constant_op
from tensorflow.python.framework import test_util
from tensorflow.python.platform import googletest
from ops import *
class SmoothCosineSimilarityTest(test_util.TensorFlowTestCase):
def testSmoothCosineSimilarity(self):
"""Test code for torch:
th> x=torch.Tensor{{1,2,3},{2,2,2},{3,2,1},{0,2,4}}
th> y=torch.Tensor{2,2,2}
th> c=nn.SmoothCosineSimilarity()
th> c:forward{x,y}
0.9257
0.9999
0.9257
0.7745
[torch.DoubleTensor of size 4]
"""
m = constant_op.constant(
[[1,2,3],
[2,2,2],
[3,2,1],
[0,2,4]], dtype=np.float32)
v = constant_op.constant([2,2,2], dtype=np.float32)
for use_gpu in [True, False]:
with self.test_session(use_gpu=use_gpu):
loss = smooth_cosine_similarity(m, v).eval()
self.assertAllClose(loss, [0.92574867671153,
0.99991667361053,
0.92574867671153,
0.77454667246876])
class CircularConvolutionTest(test_util.TensorFlowTestCase):
def testCircularConvolution(self):
v = constant_op.constant([1,2,3,4,5,6,7], dtype=tf.float32)
k = constant_op.constant([0,0,1], dtype=tf.float32)
for use_gpu in [True, False]:
with self.test_session(use_gpu=use_gpu):
loss = circular_convolution(v, k).eval()
self.assertAllEqual(loss, [7,1,2,3,4,5,6])
if __name__ == "__main__":
googletest.main()