|
14 | 14 | * limitations under the License.
|
15 | 15 | * =============================================================================
|
16 | 16 | */
|
| 17 | + |
17 | 18 | import * as tf from '../index';
|
18 | 19 | import {ALL_ENVS, describeWithFlags} from '../jasmine_util';
|
19 |
| -import {Tensor} from '../tensor'; |
20 |
| - |
21 |
| -function countParams(x: Tensor): number { |
22 |
| - const shape = x.shape; |
23 |
| - if (shape.length > 0) { |
24 |
| - return shape.reduce((a: number, b: number) => a * b); |
25 |
| - } else { |
26 |
| - // Scalar. |
27 |
| - return 1; |
28 |
| - } |
29 |
| -} |
| 20 | +import {expectArraysClose} from '../test_util'; |
| 21 | + |
| 22 | +import {tensor1d, tensor2d} from './tensor_ops'; |
30 | 23 |
|
31 | 24 | describeWithFlags('dropout', ALL_ENVS, () => {
|
32 |
| - const dropoutLevels = [0, 0.75]; |
33 |
| - const seed = 23; |
34 |
| - for (const dropoutLevel of dropoutLevels) { |
35 |
| - it(`Level = ${dropoutLevel}`, async () => { |
36 |
| - const x = tf.range(1, 21).reshape([10, 2]); |
37 |
| - const y = tf.dropout(x, tf.scalar(dropoutLevel), null, seed); |
38 |
| - expect(y.dtype).toEqual(x.dtype); |
39 |
| - expect(y.shape).toEqual(x.shape); |
40 |
| - const xValue = await x.data(); |
41 |
| - const yValue = await y.data(); |
42 |
| - let nKept = 0; |
43 |
| - for (let i = 0; i < xValue.length; ++i) { |
44 |
| - if (yValue[i] !== 0) { |
45 |
| - nKept++; |
46 |
| - expect(yValue[i]).toBeCloseTo(1 / (1 - dropoutLevel) * xValue[i]); |
| 25 | + it('x 1d array, rate 0', async () => { |
| 26 | + const x = tensor1d([1, 2, 2, 1]); |
| 27 | + const rate = 0; |
| 28 | + const output = tf.dropout(x, rate); |
| 29 | + expect(output.dtype).toEqual(x.dtype); |
| 30 | + expect(output.shape).toEqual(x.shape); |
| 31 | + expectArraysClose(await x.data(), await output.data()); |
| 32 | + }); |
| 33 | + |
| 34 | + it('x 1d array, rate 0.75', async () => { |
| 35 | + const x = tensor1d([1, 2, 2, 1]); |
| 36 | + const rate = 0.75; |
| 37 | + const output = tf.dropout(x, rate); |
| 38 | + expect(output.dtype).toEqual(x.dtype); |
| 39 | + expect(output.shape).toEqual(x.shape); |
| 40 | + const xValues = await x.data(); |
| 41 | + const outputValues = await output.data(); |
| 42 | + for (let i = 0; i < xValues.length; i++) { |
| 43 | + if (outputValues[i] !== 0) { |
| 44 | + expect(outputValues[i]).toBeCloseTo(1 / (1 - rate) * xValues[i]); |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it('x 2d array, rate 0', async () => { |
| 50 | + const x = tensor2d([1, 5, 2, 4, 3, 6], [2, 3]); |
| 51 | + const rate = 0; |
| 52 | + const output = tf.dropout(x, rate); |
| 53 | + expect(output.dtype).toEqual(x.dtype); |
| 54 | + expect(output.shape).toEqual(x.shape); |
| 55 | + expectArraysClose(await x.data(), await output.data()); |
| 56 | + }); |
| 57 | + |
| 58 | + it('x 2d array, rate 0.75', async () => { |
| 59 | + const x = tensor2d([1, 5, 2, 4, 3, 6], [2, 3]); |
| 60 | + const rate = 0.75; |
| 61 | + const output = tf.dropout(x, rate); |
| 62 | + expect(output.dtype).toEqual(x.dtype); |
| 63 | + expect(output.shape).toEqual(x.shape); |
| 64 | + const xValues = await x.data(); |
| 65 | + const outputValues = await output.data(); |
| 66 | + for (let i = 0; i < xValues.length; i++) { |
| 67 | + if (outputValues[i] !== 0) { |
| 68 | + expect(outputValues[i]).toBeCloseTo(1 / (1 - rate) * xValues[i]); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('x 1d array, rate 0.75, with noise shape length = 1', async () => { |
| 74 | + const x = tensor1d([1, 2, 2, 1]); |
| 75 | + const rate = 0.75; |
| 76 | + const noiseShape = [1]; |
| 77 | + const output = tf.dropout(x, rate, noiseShape); |
| 78 | + expect(output.dtype).toEqual(x.dtype); |
| 79 | + expect(output.shape).toEqual(x.shape); |
| 80 | + const xValues = await x.data(); |
| 81 | + const outputValues = await output.data(); |
| 82 | + const maskedOutput = outputValues[0]; |
| 83 | + for (let i = 0; i < xValues.length; i++) { |
| 84 | + if (maskedOutput === 0) { |
| 85 | + expect(outputValues[i]).toBe(maskedOutput); |
| 86 | + } |
| 87 | + if (outputValues[i] !== 0) { |
| 88 | + expect(outputValues[i]).toBeCloseTo(1 / (1 - rate) * xValues[i]); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('x 2d array, rate 0.75, with noise shape length = 2', async () => { |
| 94 | + const x = tensor2d([1, 5, 2, 4, 3, 6], [2, 3]); |
| 95 | + const rate = 0.75; |
| 96 | + const noiseShape = [2, 1]; |
| 97 | + const output = tf.dropout(x, rate, noiseShape); |
| 98 | + expect(output.dtype).toEqual(x.dtype); |
| 99 | + expect(output.shape).toEqual(x.shape); |
| 100 | + const xValues = await x.data(); |
| 101 | + const outputValues = await output.data(); |
| 102 | + for (let i = 0; i < x.shape[0]; i++) { |
| 103 | + const maskedOutput = outputValues[i * x.shape[1]]; |
| 104 | + if (maskedOutput !== 0) { |
| 105 | + expect(maskedOutput) |
| 106 | + .toBeCloseTo(1 / (1 - rate) * xValues[i * x.shape[1]]); |
| 107 | + } else { |
| 108 | + for (let j = 0; j < x.shape[1]; j++) { |
| 109 | + expect(outputValues[i * x.shape[1] + j]).toBe(maskedOutput); |
47 | 110 | }
|
48 | 111 | }
|
49 |
| - const numel = countParams(x); |
50 |
| - if (dropoutLevel === 0) { |
51 |
| - expect(nKept).toEqual(numel); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('broadcast noise shape', async () => { |
| 116 | + const x = tensor2d([1, 5, 2, 4, 3, 6], [2, 3]); |
| 117 | + const rate = 0.75; |
| 118 | + // broadcast noise shape, same output as using noiseShape [2, 1] |
| 119 | + const noiseShape = [1]; |
| 120 | + const output = tf.dropout(x, rate, noiseShape); |
| 121 | + expect(output.dtype).toEqual(x.dtype); |
| 122 | + expect(output.shape).toEqual(x.shape); |
| 123 | + const xValues = await x.data(); |
| 124 | + const outputValues = await output.data(); |
| 125 | + for (let i = 0; i < x.shape[0]; i++) { |
| 126 | + const maskedOutput = outputValues[i * x.shape[1]]; |
| 127 | + if (maskedOutput !== 0) { |
| 128 | + expect(maskedOutput) |
| 129 | + .toBeCloseTo(1 / (1 - rate) * xValues[i * x.shape[1]]); |
52 | 130 | } else {
|
53 |
| - expect(nKept).toBeLessThan(numel); |
| 131 | + for (let j = 0; j < x.shape[1]; j++) { |
| 132 | + expect(outputValues[i * x.shape[1] + j]).toBe(maskedOutput); |
| 133 | + } |
54 | 134 | }
|
55 |
| - }); |
56 |
| - } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it('x 1d array, rate 0.75, with seed', async () => { |
| 139 | + const x = tensor1d([1, 2, 2, 1]); |
| 140 | + const rate = 0.75; |
| 141 | + const seed = 23; |
| 142 | + const output = tf.dropout(x, rate, null, seed); |
| 143 | + expect(output.dtype).toEqual(x.dtype); |
| 144 | + expect(output.shape).toEqual(x.shape); |
| 145 | + const xValues = await x.data(); |
| 146 | + const outputValues = await output.data(); |
| 147 | + for (let i = 0; i < xValues.length; i++) { |
| 148 | + if (outputValues[i] !== 0) { |
| 149 | + expect(outputValues[i]).toBeCloseTo(1 / (1 - rate) * xValues[i]); |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it('x TensorLike object', async () => { |
| 155 | + const x = [1.0, 2.0, 2.0, 1.0]; |
| 156 | + const rate = 0; |
| 157 | + const output = tf.dropout(x, rate); |
| 158 | + expect(output.dtype).toEqual('float32'); |
| 159 | + expect(output.shape).toEqual([4]); |
| 160 | + expectArraysClose(await output.data(), x); |
| 161 | + }); |
| 162 | + |
| 163 | + it('throws when x.dtype != float32', async () => { |
| 164 | + const x = tensor1d([1, 2, 2, 1], 'int32'); |
| 165 | + const rate = 0.75; |
| 166 | + expect(() => tf.dropout(x, rate)).toThrowError(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('throws when rate is not in the range [0, 1)', async () => { |
| 170 | + const x = tensor1d([1, 2, 2, 1]); |
| 171 | + const rate = 1.5; |
| 172 | + expect(() => tf.dropout(x, rate)).toThrowError(); |
| 173 | + }); |
57 | 174 | });
|
0 commit comments