catniff 0.8.4 → 0.8.5

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 CHANGED
@@ -224,6 +224,7 @@ export declare class Tensor {
224
224
  zeroGrad?: boolean;
225
225
  }): void;
226
226
  val(): TensorValue;
227
+ toString(): string;
227
228
  detach(): Tensor;
228
229
  clone(): Tensor;
229
230
  replace(other: Tensor | TensorValue): Tensor;
package/dist/core.js CHANGED
@@ -2396,6 +2396,53 @@ class Tensor {
2396
2396
  }
2397
2397
  return buildNested(this.value, this.shape, this.strides, this.offset);
2398
2398
  }
2399
+ // Returns the nicely Pytorch-like formatted string form
2400
+ toString() {
2401
+ const val = this.val();
2402
+ // Format a single number (integers get trailing dot)
2403
+ const formatNum = (n) => {
2404
+ if (Number.isInteger(n) && Math.abs(n) < 1e8) {
2405
+ return n.toFixed(0) + ".";
2406
+ }
2407
+ return n.toString();
2408
+ };
2409
+ // Handle scalar
2410
+ if (typeof val === "number") {
2411
+ return `tensor(${formatNum(val)})`;
2412
+ }
2413
+ // Collect all numbers to find max width for alignment
2414
+ const collectNumbers = (v) => {
2415
+ if (typeof v === "number")
2416
+ return [v];
2417
+ return v.flatMap(collectNumbers);
2418
+ };
2419
+ const allNumbers = collectNumbers(val);
2420
+ const maxWidth = Math.max(...allNumbers.map((n) => formatNum(n).length));
2421
+ const ndim = this.shape.length;
2422
+ const baseIndent = "tensor(".length; // 7
2423
+ const formatNested = (v, depth) => {
2424
+ if (typeof v === "number") {
2425
+ return formatNum(v).padStart(maxWidth);
2426
+ }
2427
+ const arr = v;
2428
+ // Innermost dimension: format as single line [x, y, z]
2429
+ if (arr.length > 0 && typeof arr[0] === "number") {
2430
+ const elements = arr.map((x) => formatNum(x).padStart(maxWidth));
2431
+ return `[${elements.join(", ")}]`;
2432
+ }
2433
+ // Number of blank lines between elements at this depth
2434
+ // Deeper = fewer blank lines (0 between rows, 1 between 2D blocks, etc.)
2435
+ const blankLines = Math.max(0, ndim - depth - 2);
2436
+ const separator = ",\n" + "\n".repeat(blankLines);
2437
+ const innerIndent = " ".repeat(baseIndent + depth + 1);
2438
+ const formatted = arr.map((item, i) => {
2439
+ const str = formatNested(item, depth + 1);
2440
+ return i === 0 ? "[" + str : innerIndent + str;
2441
+ });
2442
+ return formatted.join(separator) + "]";
2443
+ };
2444
+ return "tensor(" + formatNested(val, 0) + ")";
2445
+ }
2399
2446
  // Returns a view of the tensor with gradient turned off and detaches from autograd
2400
2447
  detach() {
2401
2448
  return new Tensor(this.value, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "description": "Torch-like deep learning framework for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {