|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import * as tf from '../index'; |
| 19 | +import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; |
| 20 | +import {expectArraysClose} from '../test_util'; |
| 21 | + |
| 22 | +describeWithFlags('conv3dTranspose', ALL_ENVS, () => { |
| 23 | + // Reference Python TensorFlow code |
| 24 | + // ```python |
| 25 | + // import numpy as np |
| 26 | + // import tensorflow as tf |
| 27 | + // tf.enable_eager_execution() |
| 28 | + // x = np.array([2], dtype = np.float32).reshape(1, 1, 1, 1, 1) |
| 29 | + // w = np.array([5, 4, 8, 7, 1, 2, 6, 3], dtype = np.float32).reshape(2, 2, 2, |
| 30 | + // 1, 1) |
| 31 | + // tf.nn.conv3d_transpose(x, w, output_shape=[1, 2, 2, 2, 1], padding='VALID') |
| 32 | + // ``` |
| 33 | + it('input=2x2x2x1,d2=1,f=2,s=1,p=valid', async () => { |
| 34 | + const origInputDepth = 1; |
| 35 | + const origOutputDepth = 1; |
| 36 | + const inputShape: [number, number, number, number] = |
| 37 | + [1, 1, 1, origOutputDepth]; |
| 38 | + const fSize = 2; |
| 39 | + const origPad = 'valid'; |
| 40 | + const origStride = 1; |
| 41 | + |
| 42 | + const x = tf.tensor4d([2], inputShape); |
| 43 | + const w = tf.tensor5d( |
| 44 | + [5, 4, 8, 7, 1, 2, 6, 3], |
| 45 | + [fSize, fSize, fSize, origInputDepth, origOutputDepth]); |
| 46 | + |
| 47 | + const result = tf.conv3dTranspose(x, w, [2, 2, 2, 1], origStride, origPad); |
| 48 | + const expected = [10, 8, 16, 14, 2, 4, 12, 6]; |
| 49 | + |
| 50 | + expect(result.shape).toEqual([2, 2, 2, 1]); |
| 51 | + expectArraysClose(await result.data(), expected); |
| 52 | + }); |
| 53 | + |
| 54 | + // Reference Python TensorFlow code |
| 55 | + // ```python |
| 56 | + // import numpy as np |
| 57 | + // import tensorflow as tf |
| 58 | + // tf.enable_eager_execution() |
| 59 | + // x = np.array([2, 3], dtype = np.float32).reshape(2, 1, 1, 1, 1, 1) |
| 60 | + // w = np.array([5, 4, 8, 7, 1, 2, 6, 3], dtype = np.float32).reshape(2, |
| 61 | + // 2, 2, 1, 1) |
| 62 | + // tf.nn.conv3d_transpose(x, w, output_shape=[2, 2, 2, 2, 1], padding='VALID') |
| 63 | + // ``` |
| 64 | + it('input=2x2x2x1,d2=1,f=2,s=1,p=valid, batch=2', async () => { |
| 65 | + const origInputDepth = 1; |
| 66 | + const origOutputDepth = 1; |
| 67 | + const inputShape: [number, number, number, number, number] = |
| 68 | + [2, 1, 1, 1, origOutputDepth]; |
| 69 | + const fSize = 2; |
| 70 | + const origPad = 'valid'; |
| 71 | + const origStride = 1; |
| 72 | + |
| 73 | + const x = tf.tensor5d([2, 3], inputShape); |
| 74 | + const w = tf.tensor5d( |
| 75 | + [5, 4, 8, 7, 1, 2, 6, 3], |
| 76 | + [fSize, fSize, fSize, origInputDepth, origOutputDepth]); |
| 77 | + |
| 78 | + const result = |
| 79 | + tf.conv3dTranspose(x, w, [2, 2, 2, 2, 1], origStride, origPad); |
| 80 | + const expected = [10, 8, 16, 14, 2, 4, 12, 6, 15, 12, 24, 21, 3, 6, 18, 9]; |
| 81 | + |
| 82 | + expect(result.shape).toEqual([2, 2, 2, 2, 1]); |
| 83 | + expectArraysClose(await result.data(), expected); |
| 84 | + }); |
| 85 | +}); |
0 commit comments