|
4 | 4 | import numpy as np |
5 | 5 |
|
6 | 6 |
|
7 | | -class TestSquaredL2DistanceOp(unittest.TestCase): |
| 7 | +class TestSquaredL2DistanceOp_f0(unittest.TestCase): |
8 | 8 | __metaclass__ = OpTestMeta |
9 | 9 |
|
10 | 10 | def setUp(self): |
11 | 11 | self.type = 'squared_l2_distance' |
12 | 12 | self.inputs = { |
13 | | - 'X': np.random.uniform(0.1, 1., (2, 3)).astype('float32'), |
14 | | - 'Y': np.random.uniform(0.1, 1., (2, 3)).astype('float32') |
| 13 | + 'X': np.random.uniform(0.1, 1., (32, 64)).astype('float32'), |
| 14 | + 'Y': np.random.uniform(0.1, 1., (32, 64)).astype('float32') |
15 | 15 | } |
16 | | - subRes = self.inputs['X'] - self.inputs['Y'] |
17 | | - output = subRes * subRes |
| 16 | + sub_res = self.inputs['X'] - self.inputs['Y'] |
| 17 | + output = sub_res * sub_res |
18 | 18 | self.outputs = { |
19 | | - 'sub_result': subRes, |
| 19 | + 'sub_result': sub_res, |
| 20 | + 'Out': np.expand_dims(output.sum(1), 1) |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | +class TestSquaredL2DistanceOp_f1(unittest.TestCase): |
| 25 | + __metaclass__ = OpTestMeta |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.type = 'squared_l2_distance' |
| 29 | + self.inputs = { |
| 30 | + 'X': np.random.uniform(0.1, 1., (32, 64)).astype('float32'), |
| 31 | + 'Y': np.random.uniform(0.1, 1., (1, 64)).astype('float32') |
| 32 | + } |
| 33 | + sub_res = self.inputs['X'] - self.inputs['Y'] |
| 34 | + output = sub_res * sub_res |
| 35 | + self.outputs = { |
| 36 | + 'sub_result': sub_res, |
| 37 | + 'Out': np.expand_dims(output.sum(1), 1) |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +class TestSquaredL2DistanceOp_f2(unittest.TestCase): |
| 42 | + __metaclass__ = OpTestMeta |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + self.type = 'squared_l2_distance' |
| 46 | + self.inputs = { |
| 47 | + 'X': np.random.uniform(0.1, 1., (32, 64, 128)).astype('float32'), |
| 48 | + 'Y': np.random.uniform(0.1, 1., (1, 64, 128)).astype('float32') |
| 49 | + } |
| 50 | + sub_res = self.inputs['X'] - self.inputs['Y'] |
| 51 | + sub_res = sub_res.reshape((32, 64 * 128)) |
| 52 | + output = sub_res * sub_res |
| 53 | + self.outputs = { |
| 54 | + 'sub_result': sub_res, |
20 | 55 | 'Out': np.expand_dims(output.sum(1), 1) |
21 | 56 | } |
22 | 57 |
|
23 | 58 |
|
24 | 59 | class TestSquaredL2DistanceGradOp(GradientChecker): |
25 | | - def test_squared_l2_distance(self): |
| 60 | + def test_squared_l2_distance_b0(self): |
| 61 | + op = create_op("squared_l2_distance") |
| 62 | + inputs = { |
| 63 | + 'X': np.random.uniform(0.1, .6, (2, 3)).astype('float32'), |
| 64 | + 'Y': np.random.uniform(0.1, .6, (2, 3)).astype('float32') |
| 65 | + } |
| 66 | + self.compare_grad(op, inputs) |
| 67 | + self.check_grad(op, inputs, set(["X", "Y"]), "Out") |
| 68 | + |
| 69 | + def test_squared_l2_distance_b1(self): |
| 70 | + op = create_op("squared_l2_distance") |
| 71 | + inputs = { |
| 72 | + 'X': np.random.uniform(0.1, .6, (2, 3)).astype('float32'), |
| 73 | + 'Y': np.random.uniform(0.1, .6, (1, 3)).astype('float32') |
| 74 | + } |
| 75 | + self.compare_grad(op, inputs) |
| 76 | + self.check_grad(op, inputs, set(["X", "Y"]), "Out") |
| 77 | + |
| 78 | + def test_squared_l2_distance_b2(self): |
26 | 79 | op = create_op("squared_l2_distance") |
27 | 80 | inputs = { |
28 | | - 'X': np.random.uniform(0.1, 1., (2, 3)).astype('float32'), |
29 | | - 'Y': np.random.uniform(0.1, 1., (2, 3)).astype('float32') |
| 81 | + 'X': np.random.uniform(0.1, .6, (2, 3, 4)).astype('float32'), |
| 82 | + 'Y': np.random.uniform(0.1, .6, (1, 3, 4)).astype('float32') |
30 | 83 | } |
| 84 | + self.compare_grad(op, inputs) |
31 | 85 | self.check_grad(op, inputs, set(["X", "Y"]), "Out") |
32 | 86 |
|
33 | 87 |
|
|
0 commit comments