@thi.ng/tensors 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -1
- package/README.md +93 -34
- package/abs.d.ts +1 -29
- package/abs.js +2 -11
- package/add.d.ts +4 -35
- package/add.js +2 -11
- package/addn.d.ts +1 -33
- package/addn.js +2 -11
- package/api.d.ts +138 -15
- package/broadcast.d.ts +24 -0
- package/broadcast.js +54 -0
- package/clamp.d.ts +4 -38
- package/clamp.js +2 -11
- package/clampn.d.ts +1 -37
- package/clampn.js +2 -11
- package/cos.d.ts +1 -29
- package/cos.js +2 -11
- package/defopn.d.ts +4 -7
- package/defopn.js +17 -7
- package/defoprt.d.ts +5 -7
- package/defoprt.js +21 -11
- package/defoprtt.d.ts +7 -7
- package/defoprtt.js +30 -26
- package/defopt.d.ts +4 -7
- package/defopt.js +17 -20
- package/defoptn.d.ts +4 -7
- package/defoptn.js +17 -20
- package/defoptnn.d.ts +4 -7
- package/defoptnn.js +20 -16
- package/defoptt.d.ts +5 -6
- package/defoptt.js +36 -27
- package/defopttt.d.ts +5 -7
- package/defopttt.js +43 -37
- package/diagonal.d.ts +16 -0
- package/diagonal.js +18 -0
- package/div.d.ts +4 -35
- package/div.js +2 -11
- package/divn.d.ts +1 -33
- package/divn.js +2 -11
- package/dot.d.ts +4 -26
- package/dot.js +3 -12
- package/errors.d.ts +2 -0
- package/errors.js +3 -0
- package/exp.d.ts +1 -29
- package/exp.js +2 -11
- package/exp2.d.ts +1 -29
- package/exp2.js +2 -11
- package/filtered-indices.d.ts +34 -0
- package/filtered-indices.js +17 -0
- package/identity.d.ts +7 -0
- package/identity.js +3 -2
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/log.d.ts +1 -29
- package/log.js +2 -11
- package/log2.d.ts +1 -29
- package/log2.js +2 -11
- package/mag.d.ts +5 -0
- package/magsq.d.ts +1 -25
- package/magsq.js +3 -12
- package/max.d.ts +5 -30
- package/max.js +2 -11
- package/maxn.d.ts +1 -33
- package/maxn.js +2 -11
- package/min.d.ts +5 -30
- package/min.js +2 -11
- package/minn.d.ts +1 -33
- package/minn.js +2 -11
- package/mul.d.ts +4 -35
- package/mul.js +2 -11
- package/mulm.d.ts +1 -1
- package/mulm.js +21 -20
- package/muln.d.ts +1 -33
- package/muln.js +2 -11
- package/mulv.d.ts +1 -1
- package/mulv.js +16 -15
- package/normalize.d.ts +9 -1
- package/package.json +48 -4
- package/pow.d.ts +5 -30
- package/pow.js +2 -11
- package/pown.d.ts +1 -33
- package/pown.js +2 -11
- package/product.d.ts +1 -25
- package/product.js +5 -12
- package/rand-distrib.d.ts +13 -10
- package/rand-distrib.js +55 -23
- package/range.d.ts +20 -0
- package/range.js +28 -0
- package/relu.d.ts +1 -29
- package/relu.js +2 -11
- package/relun.d.ts +1 -33
- package/relun.js +2 -11
- package/select.d.ts +6 -6
- package/select.js +26 -23
- package/set.d.ts +1 -6
- package/set.js +3 -11
- package/setn.d.ts +1 -6
- package/setn.js +3 -11
- package/sigmoid.d.ts +1 -29
- package/sigmoid.js +2 -11
- package/sin.d.ts +1 -29
- package/sin.js +2 -11
- package/softmax.d.ts +1 -1
- package/softplus.d.ts +1 -33
- package/softplus.js +2 -11
- package/sqrt.d.ts +1 -29
- package/sqrt.js +2 -11
- package/step.d.ts +1 -33
- package/step.js +2 -11
- package/sub.d.ts +4 -35
- package/sub.js +2 -11
- package/subn.d.ts +1 -33
- package/subn.js +2 -11
- package/sum.d.ts +1 -25
- package/sum.js +5 -12
- package/svd.d.ts +33 -0
- package/svd.js +246 -0
- package/swap.d.ts +26 -0
- package/swap.js +15 -0
- package/tan.d.ts +1 -29
- package/tan.js +2 -11
- package/tanh.d.ts +1 -29
- package/tanh.js +2 -11
- package/tensor.d.ts +6 -1
- package/tensor.js +58 -21
- package/top.d.ts +2 -6
package/tanh.d.ts
CHANGED
|
@@ -5,33 +5,5 @@
|
|
|
5
5
|
* @param out - output tensor
|
|
6
6
|
* @param a - input tensor
|
|
7
7
|
*/
|
|
8
|
-
export declare const tanh: import("./api.js").
|
|
9
|
-
/**
|
|
10
|
-
* Same as {@link tanh} for 1D tensors.
|
|
11
|
-
*
|
|
12
|
-
* @param out - output tensor
|
|
13
|
-
* @param a - input tensor
|
|
14
|
-
*/
|
|
15
|
-
export declare const tanh1: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
|
|
16
|
-
/**
|
|
17
|
-
* Same as {@link tanh} for 2D tensors.
|
|
18
|
-
*
|
|
19
|
-
* @param out - output tensor
|
|
20
|
-
* @param a - input tensor
|
|
21
|
-
*/
|
|
22
|
-
export declare const tanh2: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
|
|
23
|
-
/**
|
|
24
|
-
* Same as {@link tanh} for 3D tensors.
|
|
25
|
-
*
|
|
26
|
-
* @param out - output tensor
|
|
27
|
-
* @param a - input tensor
|
|
28
|
-
*/
|
|
29
|
-
export declare const tanh3: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
|
|
30
|
-
/**
|
|
31
|
-
* Same as {@link tanh} for 4D tensors.
|
|
32
|
-
*
|
|
33
|
-
* @param out - output tensor
|
|
34
|
-
* @param a - input tensor
|
|
35
|
-
*/
|
|
36
|
-
export declare const tanh4: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
|
|
8
|
+
export declare const tanh: import("./api.js").MultiTensorOpImpl<import("./api.js").TensorOpT<number>>;
|
|
37
9
|
//# sourceMappingURL=tanh.d.ts.map
|
package/tanh.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { defOpT } from "./defopt.js";
|
|
2
|
-
const
|
|
3
|
-
const tanh = a;
|
|
4
|
-
const tanh1 = b;
|
|
5
|
-
const tanh2 = c;
|
|
6
|
-
const tanh3 = d;
|
|
7
|
-
const tanh4 = e;
|
|
2
|
+
const tanh = defOpT(Math.tanh);
|
|
8
3
|
export {
|
|
9
|
-
tanh
|
|
10
|
-
tanh1,
|
|
11
|
-
tanh2,
|
|
12
|
-
tanh3,
|
|
13
|
-
tanh4
|
|
4
|
+
tanh
|
|
14
5
|
};
|
package/tensor.d.ts
CHANGED
|
@@ -14,11 +14,13 @@ export declare abstract class ATensor<T = number> implements ITensor<T> {
|
|
|
14
14
|
get orderedShape(): number[];
|
|
15
15
|
get orderedStride(): number[];
|
|
16
16
|
abstract [Symbol.iterator](): IterableIterator<T>;
|
|
17
|
+
broadcast<S extends Shape>(shape: S, stride: S): ShapeTensor<S, T>;
|
|
17
18
|
copy(): typeof this;
|
|
18
19
|
empty(storage?: ITensorStorage<T>): typeof this;
|
|
19
20
|
equiv(o: any): boolean;
|
|
20
21
|
eqDelta(o: ITensor<T>, eps?: number): boolean;
|
|
21
22
|
abstract index(pos: NumericArray): number;
|
|
23
|
+
position(index: number): any[];
|
|
22
24
|
abstract get(pos: NumericArray): T;
|
|
23
25
|
abstract set(pos: NumericArray, v: T): this;
|
|
24
26
|
hi(pos: NumericArray): this;
|
|
@@ -42,6 +44,7 @@ export declare class Tensor1<T = number> extends ATensor<T> {
|
|
|
42
44
|
get order(): number[];
|
|
43
45
|
get length(): number;
|
|
44
46
|
index([x]: NumericArray): number;
|
|
47
|
+
position(index: number): number[];
|
|
45
48
|
get([x]: NumericArray): T;
|
|
46
49
|
set([x]: NumericArray, v: T): this;
|
|
47
50
|
pick([x]: NumericArray): Tensor1<T>;
|
|
@@ -122,6 +125,8 @@ export declare function tensorFromArray<T extends NumType, N extends Nested<numb
|
|
|
122
125
|
* @param opts
|
|
123
126
|
*/
|
|
124
127
|
export declare function tensorFromArray<N extends Nested<string>>(data: N, opts?: TensorFromArrayOpts<"str", string>): NestedTensor<N, string>;
|
|
125
|
-
export declare const
|
|
128
|
+
export declare const zeroes: <S extends Shape>(shape: S, type?: NumType, storage?: ITensorStorage<number>) => ShapeTensor<S, number>;
|
|
129
|
+
export declare const ones: <S extends Shape>(shape: S, type?: NumType, storage?: ITensorStorage<number>) => ShapeTensor<S, number>;
|
|
130
|
+
export declare const shapeToStride: (shape: number[]) => any[];
|
|
126
131
|
export declare const strideOrder: (strides: number[]) => number[];
|
|
127
132
|
//# sourceMappingURL=tensor.d.ts.map
|
package/tensor.js
CHANGED
|
@@ -29,6 +29,16 @@ class ATensor {
|
|
|
29
29
|
get orderedStride() {
|
|
30
30
|
return swizzle(this.order)(this.stride);
|
|
31
31
|
}
|
|
32
|
+
broadcast(shape, stride) {
|
|
33
|
+
return new TENSOR_IMPLS[shape.length](
|
|
34
|
+
this.type,
|
|
35
|
+
this.storage,
|
|
36
|
+
this.data,
|
|
37
|
+
shape,
|
|
38
|
+
stride,
|
|
39
|
+
this.offset
|
|
40
|
+
);
|
|
41
|
+
}
|
|
32
42
|
copy() {
|
|
33
43
|
return new this.constructor(
|
|
34
44
|
this.type,
|
|
@@ -54,6 +64,16 @@ class ATensor {
|
|
|
54
64
|
eqDelta(o, eps = 1e-6) {
|
|
55
65
|
return this === o || equiv(this.shape, o.shape) && _eqDelta([...this], [...o], this.length, eps);
|
|
56
66
|
}
|
|
67
|
+
position(index) {
|
|
68
|
+
const { order, stride } = this;
|
|
69
|
+
index -= this.offset;
|
|
70
|
+
const idx = order.map((o) => {
|
|
71
|
+
const i = ~~(index / stride[o]);
|
|
72
|
+
index -= i * stride[o];
|
|
73
|
+
return i;
|
|
74
|
+
});
|
|
75
|
+
return swizzle(order)(idx);
|
|
76
|
+
}
|
|
57
77
|
hi(pos) {
|
|
58
78
|
return new this.constructor(
|
|
59
79
|
this.type,
|
|
@@ -139,11 +159,11 @@ class Tensor1 extends ATensor {
|
|
|
139
159
|
*[Symbol.iterator]() {
|
|
140
160
|
let {
|
|
141
161
|
data,
|
|
142
|
-
|
|
162
|
+
shape: [sx],
|
|
143
163
|
stride: [tx],
|
|
144
164
|
offset
|
|
145
165
|
} = this;
|
|
146
|
-
for (;
|
|
166
|
+
for (; sx-- > 0; offset += tx) yield data[offset];
|
|
147
167
|
}
|
|
148
168
|
get dim() {
|
|
149
169
|
return 1;
|
|
@@ -157,6 +177,9 @@ class Tensor1 extends ATensor {
|
|
|
157
177
|
index([x]) {
|
|
158
178
|
return this.offset + x * this.stride[0];
|
|
159
179
|
}
|
|
180
|
+
position(index) {
|
|
181
|
+
return [~~((index - this.offset) / this.stride[0])];
|
|
182
|
+
}
|
|
160
183
|
get([x]) {
|
|
161
184
|
return this.data[this.offset + x * this.stride[0]];
|
|
162
185
|
}
|
|
@@ -211,8 +234,9 @@ class Tensor2 extends ATensor {
|
|
|
211
234
|
shape: [sx, sy],
|
|
212
235
|
stride: [tx, ty]
|
|
213
236
|
} = this;
|
|
214
|
-
|
|
215
|
-
|
|
237
|
+
let ox, x, y;
|
|
238
|
+
for (ox = this.offset, x = 0; x < sx; x++, ox += tx) {
|
|
239
|
+
for (y = 0; y < sy; y++) {
|
|
216
240
|
yield data[ox + y * ty];
|
|
217
241
|
}
|
|
218
242
|
}
|
|
@@ -246,8 +270,9 @@ class Tensor2 extends ATensor {
|
|
|
246
270
|
stride: [tx, ty]
|
|
247
271
|
} = this;
|
|
248
272
|
const n = min(this.length, newLength);
|
|
249
|
-
|
|
250
|
-
|
|
273
|
+
let ox, x, y, i;
|
|
274
|
+
for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
|
|
275
|
+
for (y = 0; y < sy && i < n; y++, i++) {
|
|
251
276
|
newData[i] = data[ox + y * ty];
|
|
252
277
|
}
|
|
253
278
|
}
|
|
@@ -273,9 +298,10 @@ class Tensor3 extends ATensor {
|
|
|
273
298
|
shape: [sx, sy, sz],
|
|
274
299
|
stride: [tx, ty, tz]
|
|
275
300
|
} = this;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
301
|
+
let ox, oy, x, y, z;
|
|
302
|
+
for (ox = this.offset, x = 0; x < sx; x++, ox += tx) {
|
|
303
|
+
for (oy = ox, y = 0; y < sy; y++, oy += ty) {
|
|
304
|
+
for (z = 0; z < sz; z++) {
|
|
279
305
|
yield data[oy + z * tz];
|
|
280
306
|
}
|
|
281
307
|
}
|
|
@@ -307,9 +333,10 @@ class Tensor3 extends ATensor {
|
|
|
307
333
|
stride: [tx, ty, tz]
|
|
308
334
|
} = this;
|
|
309
335
|
const n = min(this.length, newLength);
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
336
|
+
let ox, oy, x, y, z, i;
|
|
337
|
+
for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
|
|
338
|
+
for (oy = ox, y = 0; y < sy; y++, oy += ty) {
|
|
339
|
+
for (z = 0; z < sz && i < n; z++, i++) {
|
|
313
340
|
newData[i] = data[oy + z * tz];
|
|
314
341
|
}
|
|
315
342
|
}
|
|
@@ -337,10 +364,11 @@ class Tensor4 extends ATensor {
|
|
|
337
364
|
stride: [tx, ty, tz, tw],
|
|
338
365
|
offset
|
|
339
366
|
} = this;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
367
|
+
let ox, oy, oz, x, y, z, w;
|
|
368
|
+
for (ox = offset, x = 0; x < sx; x++, ox += tx) {
|
|
369
|
+
for (oy = ox, y = 0; y < sy; y++, oy += ty) {
|
|
370
|
+
for (oz = oy, z = 0; z < sz; z++, oz += tz) {
|
|
371
|
+
for (w = 0; w < sw; w++) {
|
|
344
372
|
yield data[oz + w * tw];
|
|
345
373
|
}
|
|
346
374
|
}
|
|
@@ -373,10 +401,11 @@ class Tensor4 extends ATensor {
|
|
|
373
401
|
stride: [tx, ty, tz, tw]
|
|
374
402
|
} = this;
|
|
375
403
|
const n = min(this.length, newLength);
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
404
|
+
let ox, oy, oz, x, y, z, w, i;
|
|
405
|
+
for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
|
|
406
|
+
for (oy = ox, y = 0; y < sy; y++, oy += ty) {
|
|
407
|
+
for (oz = oy, z = 0; z < sz; z++, oz += tz) {
|
|
408
|
+
for (w = 0; w < sw && i < n; w++, i++) {
|
|
380
409
|
newData[i] = data[oz + w * tw];
|
|
381
410
|
}
|
|
382
411
|
}
|
|
@@ -445,6 +474,12 @@ function tensorFromArray(data, opts) {
|
|
|
445
474
|
storage: opts?.storage
|
|
446
475
|
});
|
|
447
476
|
}
|
|
477
|
+
const zeroes = (shape, type = "num", storage) => tensor(type, shape, { storage });
|
|
478
|
+
const ones = (shape, type = "num", storage) => {
|
|
479
|
+
const res = tensor(type, shape, { storage });
|
|
480
|
+
res.data.fill(1);
|
|
481
|
+
return res;
|
|
482
|
+
};
|
|
448
483
|
const shapeToStride = (shape) => {
|
|
449
484
|
const n = shape.length;
|
|
450
485
|
const stride = new Array(n);
|
|
@@ -510,8 +545,10 @@ export {
|
|
|
510
545
|
Tensor2,
|
|
511
546
|
Tensor3,
|
|
512
547
|
Tensor4,
|
|
548
|
+
ones,
|
|
513
549
|
shapeToStride,
|
|
514
550
|
strideOrder,
|
|
515
551
|
tensor,
|
|
516
|
-
tensorFromArray
|
|
552
|
+
tensorFromArray,
|
|
553
|
+
zeroes
|
|
517
554
|
};
|
package/top.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Maybe } from "@thi.ng/api";
|
|
2
|
+
import type { MultiTensorOpImpl } from "./api.js";
|
|
2
3
|
/**
|
|
3
4
|
* Specialized / optimized version of
|
|
4
5
|
* [`thi.ng/defmulti`](https://thi.ng/defmulti) for tensor operations. Uses
|
|
@@ -7,10 +8,5 @@ import type { Maybe } from "@thi.ng/api";
|
|
|
7
8
|
*
|
|
8
9
|
* @param dispatch - arg index (default: 1)
|
|
9
10
|
*/
|
|
10
|
-
export declare const top: <T extends Function>(dispatch?: number, fallback?: T, ...optimized: Maybe<T>[]) =>
|
|
11
|
-
(...args: any[]): any;
|
|
12
|
-
add(dim: number, fn: T): T;
|
|
13
|
-
default(fn: T): T;
|
|
14
|
-
impl(dim?: number): Maybe<T>;
|
|
15
|
-
};
|
|
11
|
+
export declare const top: <T extends Function>(dispatch?: number, fallback?: T, ...optimized: Maybe<T>[]) => MultiTensorOpImpl<T>;
|
|
16
12
|
//# sourceMappingURL=top.d.ts.map
|