-
Notifications
You must be signed in to change notification settings - Fork 5
/
tensor.ts
208 lines (197 loc) · 3.59 KB
/
tensor.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
Array1D,
Array2D,
Array3D,
Array4D,
Array5D,
Array6D,
Rank,
Shape,
Shape1D,
Shape2D,
Shape3D,
Shape4D,
Shape5D,
Shape6D,
} from "../api/shape.ts";
import { inferShape, length } from "./util.ts";
/**
* A generic N-dimensional tensor.
*/
export class Tensor<R extends Rank> {
shape: Shape[R];
data: Float32Array;
constructor(data: Float32Array, shape: Shape[R]) {
this.shape = shape;
this.data = data;
}
/**
* Creates an empty tensor.
*/
static zeroes<R extends Rank>(shape: Shape[R]): Tensor<R> {
return new Tensor(new Float32Array(length(shape)), shape);
}
/**
* Serialise a tensor into JSON.
*/
toJSON() {
const data = new Array(this.data.length).fill(1);
this.data.forEach((value, i) => data[i] = value);
return { data, shape: this.shape };
}
}
/**
* Create an nth rank tensor from the given nthD array and shape.
* ```ts
* tensor([1, 2, 3, 4], [2, 2]);
* ```
*/
export function tensor<R extends Rank>(values: Float32Array, shape: Shape[R]) {
return new Tensor(values, shape);
}
/**
* Create a 1D tensor from the given 1D array.
*
* ```ts
* tensor1D([1, 2, 3, 4]);
* ```
*/
export function tensor1D(values: Array1D) {
const shape = inferShape(values) as Shape1D;
return new Tensor(new Float32Array(values), shape);
}
/**
* Create a 2D tensor from the given 2D array.
*
* ```ts
* tensor2D([
* [1, 2, 3, 4],
* [5, 6, 7, 8],
* ]);
* ```
*/
export function tensor2D(values: Array2D) {
const shape = inferShape(values) as Shape2D;
return new Tensor(new Float32Array(values.flat(1)), shape);
}
/**
* Create a 3D tensor from the given 3D array.
*
* ```ts
* tensor3D([
* [
* [1, 2, 3, 4],
* [5, 6, 7, 8],
* ],
* [
* [1, 2, 3, 4],
* [5, 6, 7, 8],
* ],
* ]);
* ```
*/
export function tensor3D(values: Array3D) {
const shape = inferShape(values) as Shape3D;
return new Tensor(new Float32Array(values.flat(2)), shape);
}
/**
* Create a 4D tensor from the given 4D array.
*
* ```ts
* tensor4D([
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* ],
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ]
* ],
* ]);
* ```
*/
export function tensor4D(values: Array4D) {
const shape = inferShape(values) as Shape4D;
return new Tensor(new Float32Array(values.flat(3)), shape);
}
/**
* Create a 5D tensor from the given 5D array.
*
* ```ts
* tensor5D([
* [
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* ],
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* ],
* ],
* ]);
* ```
*/
export function tensor5D(values: Array5D) {
const shape = inferShape(values) as Shape5D;
return new Tensor(new Float32Array(values.flat(4)), shape);
}
/**
* Create a 6D tensor from the given 6D array.
* ```ts
* tensor6D([
* [
* [
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* ],
* [
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* [
* [1, 2, 3],
* [4, 5, 6],
* ],
* ],
* ]
* ]
* ]);
* ```
*/
export function tensor6D(values: Array6D) {
const shape = inferShape(values) as Shape6D;
return new Tensor(new Float32Array(values.flat(5)), shape);
}