|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +from caffe2.python import core |
| 7 | +from hypothesis import given |
| 8 | +import caffe2.python.hypothesis_test_util as hu |
| 9 | +import numpy as np |
| 10 | + |
| 11 | + |
| 12 | +class SparseItemwiseDropoutWithReplacementTest(hu.HypothesisTestCase): |
| 13 | + @given(**hu.gcs_cpu_only) |
| 14 | + def test_no_dropout(self, gc, dc): |
| 15 | + X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.int64) |
| 16 | + Lengths = np.array([2, 2, 2, 2, 2]).astype(np.int32) |
| 17 | + replacement_value = -1 |
| 18 | + self.ws.create_blob("X").feed(X) |
| 19 | + self.ws.create_blob("Lengths").feed(Lengths) |
| 20 | + sparse_dropout_op = core.CreateOperator( |
| 21 | + "SparseItemwiseDropoutWithReplacement", ["X", "Lengths"], ["Y", "LY"], |
| 22 | + ratio=0.0, replacement_value=replacement_value) |
| 23 | + self.ws.run(sparse_dropout_op) |
| 24 | + Y = self.ws.blobs["Y"].fetch() |
| 25 | + OutputLengths = self.ws.blobs["LY"].fetch() |
| 26 | + self.assertListEqual(X.tolist(), Y.tolist(), |
| 27 | + "Values should stay unchanged") |
| 28 | + self.assertListEqual(Lengths.tolist(), OutputLengths.tolist(), |
| 29 | + "Lengths should stay unchanged.") |
| 30 | + |
| 31 | + @given(**hu.gcs_cpu_only) |
| 32 | + def test_all_dropout(self, gc, dc): |
| 33 | + X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.int64) |
| 34 | + Lengths = np.array([2, 2, 2, 2, 2]).astype(np.int32) |
| 35 | + replacement_value = -1 |
| 36 | + self.ws.create_blob("X").feed(X) |
| 37 | + self.ws.create_blob("Lengths").feed(Lengths) |
| 38 | + sparse_dropout_op = core.CreateOperator( |
| 39 | + "SparseItemwiseDropoutWithReplacement", ["X", "Lengths"], ["Y", "LY"], |
| 40 | + ratio=1.0, replacement_value=replacement_value) |
| 41 | + self.ws.run(sparse_dropout_op) |
| 42 | + y = self.ws.blobs["Y"].fetch() |
| 43 | + lengths = self.ws.blobs["LY"].fetch() |
| 44 | + for elem in y: |
| 45 | + self.assertEqual(elem, replacement_value, "Expected all \ |
| 46 | + negative elements when dropout ratio is 1.") |
| 47 | + for length in lengths: |
| 48 | + self.assertEqual(length, 2) |
| 49 | + self.assertEqual(sum(lengths), len(y)) |
| 50 | + |
| 51 | + @given(**hu.gcs_cpu_only) |
| 52 | + def test_all_dropout_empty_input(self, gc, dc): |
| 53 | + X = np.array([]).astype(np.int64) |
| 54 | + Lengths = np.array([0]).astype(np.int32) |
| 55 | + replacement_value = -1 |
| 56 | + self.ws.create_blob("X").feed(X) |
| 57 | + self.ws.create_blob("Lengths").feed(Lengths) |
| 58 | + sparse_dropout_op = core.CreateOperator( |
| 59 | + "SparseItemwiseDropoutWithReplacement", ["X", "Lengths"], ["Y", "LY"], |
| 60 | + ratio=1.0, replacement_value=replacement_value) |
| 61 | + self.ws.run(sparse_dropout_op) |
| 62 | + y = self.ws.blobs["Y"].fetch() |
| 63 | + lengths = self.ws.blobs["LY"].fetch() |
| 64 | + self.assertEqual(len(y), 0, "Expected no dropout value") |
| 65 | + self.assertEqual(len(lengths), 1, "Expected single element \ |
| 66 | + in lengths array") |
| 67 | + self.assertEqual(lengths[0], 0, "Expected 0 as sole length") |
| 68 | + self.assertEqual(sum(lengths), len(y)) |
0 commit comments