UNPKG

2.35 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC. 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 */
17import { inferShape } from '../tensor_util_env';
18import { assertNonNull } from '../util';
19import { makeTensor } from './tensor_ops_util';
20/**
21 * Creates rank-5 `tf.Tensor` with the provided values, shape and dtype.
22 *
23 * The same functionality can be achieved with `tf.tensor`, but in general
24 * we recommend using `tf.tensor5d` as it makes the code more readable.
25 *
26 * ```js
27 * // Pass a nested array.
28 * tf.tensor5d([[[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]]]).print();
29 * ```
30 * ```js
31 * // Pass a flat array and specify a shape.
32 * tf.tensor5d([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 2, 2, 1]).print();
33 * ```
34 *
35 * @param values The values of the tensor. Can be nested array of numbers,
36 * or a flat array, or a `TypedArray`.
37 * @param shape The shape of the tensor. Optional. If not provided,
38 * it is inferred from `values`.
39 * @param dtype The data type.
40 *
41 * @doc {heading: 'Tensors', subheading: 'Creation'}
42 */
43export function tensor5d(values, shape, dtype) {
44 assertNonNull(values);
45 if (shape != null && shape.length !== 5) {
46 throw new Error('tensor5d() requires shape to have five numbers');
47 }
48 const inferredShape = inferShape(values, dtype);
49 if (inferredShape.length !== 5 && inferredShape.length !== 1) {
50 throw new Error('tensor5d() requires values to be ' +
51 'number[][][][][] or flat/TypedArray');
52 }
53 if (inferredShape.length === 1 && shape == null) {
54 throw new Error('tensor5d() requires shape to be provided when `values` ' +
55 'are a flat array');
56 }
57 return makeTensor(values, shape, inferredShape, dtype);
58}
59//# sourceMappingURL=tensor5d.js.map
\No newline at end of file