-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy patheye.ts
82 lines (78 loc) · 2.88 KB
/
eye.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {Tensor2D} from '../tensor';
import {DataType} from '../types';
import {buffer} from './buffer';
import {expandDims} from './expand_dims';
import {op} from './operation';
import {reshape} from './reshape';
import {tile} from './tile';
/**
* Create an identity matrix.
*
* @param numRows Number of rows.
* @param numColumns Number of columns. Defaults to `numRows`.
* @param batchShape If provided, will add the batch shape to the beginning
* of the shape of the returned `tf.Tensor` by repeating the identity
* matrix.
* @param dtype Data type.
* @returns Identity matrix of the specified size and data type, possibly
* with batch repetition if `batchShape` is specified.
*
* @doc {heading: 'Tensors', subheading: 'Creation'}
*/
function eye_(
numRows: number, numColumns?: number,
batchShape?:
[
number
]|[number,
number]|[number, number, number]|[number, number, number, number],
dtype: DataType = 'float32'): Tensor2D {
if (numColumns == null) {
numColumns = numRows;
}
const buff = buffer([numRows, numColumns], dtype);
const n = numRows <= numColumns ? numRows : numColumns;
for (let i = 0; i < n; ++i) {
buff.set(1, i, i);
}
const out: Tensor2D = reshape(buff.toTensor(), [numRows, numColumns]);
if (batchShape == null) {
return out;
} else {
if (batchShape.length === 1) {
return tile(expandDims(out, 0), [batchShape[0], 1, 1]) as Tensor2D;
} else if (batchShape.length === 2) {
// tslint:disable-next-line:no-unnecessary-type-assertion
return tile(
expandDims(expandDims(out, 0), 0),
[batchShape[0], batchShape[1], 1, 1]) as Tensor2D;
} else if (batchShape.length === 3) {
// tslint:disable-next-line:no-unnecessary-type-assertion
return tile(expandDims(expandDims(expandDims(out, 0), 0), 0), [
batchShape[0], batchShape[1], batchShape[2], 1, 1
]) as Tensor2D;
} else {
throw new Error(
`eye() currently supports only 1D and 2D ` +
// tslint:disable-next-line:no-any
`batchShapes, but received ${(batchShape as any).length}D.`);
}
}
}
export const eye = /* @__PURE__ */ op({eye_});