catniff 0.2.12 → 0.2.13
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/dist/core.d.ts +11 -0
- package/dist/core.js +115 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +15 -0
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -139,9 +139,20 @@ export declare class Tensor {
|
|
|
139
139
|
mm(other: TensorValue | Tensor): Tensor;
|
|
140
140
|
mv(other: TensorValue | Tensor): Tensor;
|
|
141
141
|
matmul(other: TensorValue | Tensor): Tensor;
|
|
142
|
+
static full(shape: number[], num: number, options?: TensorOptions): Tensor;
|
|
142
143
|
static fullLike(tensor: Tensor, num: number, options?: TensorOptions): Tensor;
|
|
144
|
+
static ones(shape?: number[], options?: TensorOptions): Tensor;
|
|
143
145
|
static onesLike(tensor: Tensor, options?: TensorOptions): Tensor;
|
|
146
|
+
static zeros(shape?: number[], options?: TensorOptions): Tensor;
|
|
144
147
|
static zerosLike(tensor: Tensor, options?: TensorOptions): Tensor;
|
|
148
|
+
static rand(shape?: number[], options?: TensorOptions): Tensor;
|
|
149
|
+
static randLike(tensor: Tensor, options?: TensorOptions): Tensor;
|
|
150
|
+
static randn(shape?: number[], options?: TensorOptions): Tensor;
|
|
151
|
+
static randnLike(tensor: Tensor, options?: TensorOptions): Tensor;
|
|
152
|
+
static randint(shape: number[], low: number, high: number, options?: TensorOptions): Tensor;
|
|
153
|
+
static randintLike(tensor: Tensor, low: number, high: number, options?: TensorOptions): Tensor;
|
|
154
|
+
static normal(shape: number[], mean: number, stdDev: number, options?: TensorOptions): Tensor;
|
|
155
|
+
static uniform(shape: number[], low: number, high: number, options?: TensorOptions): Tensor;
|
|
145
156
|
backward(): void;
|
|
146
157
|
val(): TensorValue;
|
|
147
158
|
withGrad(requiresGrad: boolean): Tensor;
|
package/dist/core.js
CHANGED
|
@@ -1101,24 +1101,139 @@ class Tensor {
|
|
|
1101
1101
|
// Too lazy for batched matmul
|
|
1102
1102
|
throw new Error(`Shapes [${this.shape}] and [${other.shape}] are not supported`);
|
|
1103
1103
|
}
|
|
1104
|
+
// Utility to create a new tensor filled with a number
|
|
1105
|
+
static full(shape, num, options = {}) {
|
|
1106
|
+
if (shape.length === 0)
|
|
1107
|
+
return new Tensor(num, options);
|
|
1108
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1109
|
+
const outputValue = new Array(outputSize).fill(num);
|
|
1110
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1111
|
+
}
|
|
1104
1112
|
// Utility to create a new tensor with shape of another tensor, filled with a number
|
|
1105
1113
|
static fullLike(tensor, num, options = {}) {
|
|
1106
1114
|
if (typeof tensor.value === "number")
|
|
1107
1115
|
return new Tensor(num, options);
|
|
1108
1116
|
return new Tensor(new Array(tensor.value.length).fill(num), { shape: tensor.shape, strides: tensor.strides, ...options });
|
|
1109
1117
|
}
|
|
1118
|
+
// Utility to create a new tensor filled with 1
|
|
1119
|
+
static ones(shape, options = {}) {
|
|
1120
|
+
if (typeof shape === "undefined" || shape.length === 0)
|
|
1121
|
+
return new Tensor(1, options);
|
|
1122
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1123
|
+
const outputValue = new Array(outputSize).fill(1);
|
|
1124
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1125
|
+
}
|
|
1110
1126
|
// Utility to create a new tensor with shape of another tensor, filled with 1
|
|
1111
1127
|
static onesLike(tensor, options = {}) {
|
|
1112
1128
|
if (typeof tensor.value === "number")
|
|
1113
1129
|
return new Tensor(1, options);
|
|
1114
1130
|
return new Tensor(new Array(tensor.value.length).fill(1), { shape: tensor.shape, strides: tensor.strides, ...options });
|
|
1115
1131
|
}
|
|
1132
|
+
// Utility to create a new tensor filled with 0
|
|
1133
|
+
static zeros(shape, options = {}) {
|
|
1134
|
+
if (typeof shape === "undefined" || shape.length === 0)
|
|
1135
|
+
return new Tensor(0, options);
|
|
1136
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1137
|
+
const outputValue = new Array(outputSize).fill(0);
|
|
1138
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1139
|
+
}
|
|
1116
1140
|
// Utility to create a new tensor with shape of another tensor, filled with 0
|
|
1117
1141
|
static zerosLike(tensor, options = {}) {
|
|
1118
1142
|
if (typeof tensor.value === "number")
|
|
1119
1143
|
return new Tensor(0, options);
|
|
1120
1144
|
return new Tensor(new Array(tensor.value.length).fill(0), { shape: tensor.shape, strides: tensor.strides, ...options });
|
|
1121
1145
|
}
|
|
1146
|
+
// Utility to create a new tensor filled with a random number with uniform distribution from 0 to 1
|
|
1147
|
+
static rand(shape, options = {}) {
|
|
1148
|
+
if (typeof shape === "undefined" || shape.length === 0)
|
|
1149
|
+
return new Tensor((0, utils_1.randUniform)(), options);
|
|
1150
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1151
|
+
const outputValue = new Array(outputSize);
|
|
1152
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1153
|
+
outputValue[index] = (0, utils_1.randUniform)();
|
|
1154
|
+
}
|
|
1155
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1156
|
+
}
|
|
1157
|
+
// Utility to create a new tensor with shape of another tensor, filled with a random number with uniform distribution from 0 to 1
|
|
1158
|
+
static randLike(tensor, options = {}) {
|
|
1159
|
+
if (typeof tensor.value === "number")
|
|
1160
|
+
return new Tensor((0, utils_1.randUniform)(), options);
|
|
1161
|
+
const outputValue = new Array(tensor.value.length);
|
|
1162
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1163
|
+
outputValue[index] = (0, utils_1.randUniform)();
|
|
1164
|
+
}
|
|
1165
|
+
return new Tensor(outputValue, {
|
|
1166
|
+
shape: tensor.shape, strides: tensor.strides, ...options
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
// Utility to create a new tensor filled with a random number with normal distribution of mean=0 and stddev=1
|
|
1170
|
+
static randn(shape, options = {}) {
|
|
1171
|
+
if (typeof shape === "undefined" || shape.length === 0)
|
|
1172
|
+
return new Tensor((0, utils_1.randNormal)(), options);
|
|
1173
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1174
|
+
const outputValue = new Array(outputSize);
|
|
1175
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1176
|
+
outputValue[index] = (0, utils_1.randNormal)();
|
|
1177
|
+
}
|
|
1178
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1179
|
+
}
|
|
1180
|
+
// Utility to create a new tensor with shape of another tensor, filled with a random number with normal distribution of mean=0 and stddev=1
|
|
1181
|
+
static randnLike(tensor, options = {}) {
|
|
1182
|
+
if (typeof tensor.value === "number")
|
|
1183
|
+
return new Tensor((0, utils_1.randNormal)(), options);
|
|
1184
|
+
const outputValue = new Array(tensor.value.length);
|
|
1185
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1186
|
+
outputValue[index] = (0, utils_1.randNormal)();
|
|
1187
|
+
}
|
|
1188
|
+
return new Tensor(outputValue, {
|
|
1189
|
+
shape: tensor.shape, strides: tensor.strides, ...options
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
// Utility to create a new tensor filled with a random integer between low and high
|
|
1193
|
+
static randint(shape, low, high, options = {}) {
|
|
1194
|
+
if (shape.length === 0)
|
|
1195
|
+
return new Tensor((0, utils_1.randInt)(low, high), options);
|
|
1196
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1197
|
+
const outputValue = new Array(outputSize);
|
|
1198
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1199
|
+
outputValue[index] = (0, utils_1.randInt)(low, high);
|
|
1200
|
+
}
|
|
1201
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1202
|
+
}
|
|
1203
|
+
// Utility to create a new tensor with shape of another tensor, filled with a random integer between low and high
|
|
1204
|
+
static randintLike(tensor, low, high, options = {}) {
|
|
1205
|
+
if (typeof tensor.value === "number")
|
|
1206
|
+
return new Tensor((0, utils_1.randInt)(low, high), options);
|
|
1207
|
+
const outputValue = new Array(tensor.value.length);
|
|
1208
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1209
|
+
outputValue[index] = (0, utils_1.randInt)(low, high);
|
|
1210
|
+
}
|
|
1211
|
+
return new Tensor(outputValue, {
|
|
1212
|
+
shape: tensor.shape, strides: tensor.strides, ...options
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
// Utility to create a new tensor filled with a random number with normal distribution of custom mean and stddev
|
|
1216
|
+
static normal(shape, mean, stdDev, options = {}) {
|
|
1217
|
+
if (shape.length === 0)
|
|
1218
|
+
return new Tensor((0, utils_1.randNormal)(mean, stdDev), options);
|
|
1219
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1220
|
+
const outputValue = new Array(outputSize);
|
|
1221
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1222
|
+
outputValue[index] = (0, utils_1.randNormal)(mean, stdDev);
|
|
1223
|
+
}
|
|
1224
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1225
|
+
}
|
|
1226
|
+
// Utility to create a new tensor filled with a random number with uniform distribution from low to high
|
|
1227
|
+
static uniform(shape, low, high, options = {}) {
|
|
1228
|
+
if (shape.length === 0)
|
|
1229
|
+
return new Tensor((0, utils_1.randUniform)(low, high), options);
|
|
1230
|
+
const outputSize = Tensor.shapeToSize(shape);
|
|
1231
|
+
const outputValue = new Array(outputSize);
|
|
1232
|
+
for (let index = 0; index < outputValue.length; index++) {
|
|
1233
|
+
outputValue[index] = (0, utils_1.randUniform)(low, high);
|
|
1234
|
+
}
|
|
1235
|
+
return new Tensor(outputValue, { shape, ...options });
|
|
1236
|
+
}
|
|
1122
1237
|
// Reverse-mode autodiff call
|
|
1123
1238
|
backward() {
|
|
1124
1239
|
// Build topological order
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export declare function erf(x: number): number;
|
|
2
2
|
export declare function erfc(x: number): number;
|
|
3
3
|
export declare function erfinv(x: number): number;
|
|
4
|
+
export declare function randUniform(low?: number, high?: number): number;
|
|
5
|
+
export declare function randNormal(mean?: number, stdDev?: number): number;
|
|
6
|
+
export declare function randInt(low: number, high: number): number;
|
package/dist/utils.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.erf = erf;
|
|
4
4
|
exports.erfc = erfc;
|
|
5
5
|
exports.erfinv = erfinv;
|
|
6
|
+
exports.randUniform = randUniform;
|
|
7
|
+
exports.randNormal = randNormal;
|
|
8
|
+
exports.randInt = randInt;
|
|
6
9
|
// Error function using Abramowitz and Stegun approximation
|
|
7
10
|
function erf(x) {
|
|
8
11
|
const a1 = 0.254829592;
|
|
@@ -33,3 +36,15 @@ function erfinv(x) {
|
|
|
33
36
|
const sign = x >= 0 ? 1 : -1;
|
|
34
37
|
return sign * Math.sqrt(-part1 + Math.sqrt(part1 * part1 - part2));
|
|
35
38
|
}
|
|
39
|
+
function randUniform(low = 0, high = 1) {
|
|
40
|
+
return Math.random() * (high - low) + low;
|
|
41
|
+
}
|
|
42
|
+
function randNormal(mean = 0, stdDev = 1) {
|
|
43
|
+
const u = 1 - Math.random();
|
|
44
|
+
const v = 1 - Math.random();
|
|
45
|
+
const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
|
46
|
+
return z * stdDev + mean;
|
|
47
|
+
}
|
|
48
|
+
function randInt(low, high) {
|
|
49
|
+
return Math.floor(Math.random() * (high - low) + low);
|
|
50
|
+
}
|