* Copy data from another Tensor or javascript array. * The number of elements must match. * * @param data The source data array. * @returns this
(
data: Tensor | Array<number> | Float32Array | Float64Array |
Int32Array | Int8Array | Uint8Array | Uint8ClampedArray
)
| 605 | * @returns this |
| 606 | */ |
| 607 | copyFrom( |
| 608 | data: Tensor | Array<number> | Float32Array | Float64Array | |
| 609 | Int32Array | Int8Array | Uint8Array | Uint8ClampedArray |
| 610 | ): this { |
| 611 | if (data instanceof Tensor) { |
| 612 | this.ctx.tensorCopyFromTo(data, this); |
| 613 | return this; |
| 614 | } else { |
| 615 | const size = this.shape.reduce((a, b) => { |
| 616 | return a * b; |
| 617 | }, 1); |
| 618 | if (data.length != size) { |
| 619 | throw new Error( |
| 620 | "data size and shape mismatch data.length" + |
| 621 | data.length + |
| 622 | " vs " + |
| 623 | size |
| 624 | ); |
| 625 | } |
| 626 | let buffer: ArrayBuffer; |
| 627 | if (this.dtype === "float32") { |
| 628 | buffer = Float32Array.from(data).buffer; |
| 629 | } else if (this.dtype === "float64") { |
| 630 | buffer = Float64Array.from(data).buffer; |
| 631 | } else if (this.dtype === "int32") { |
| 632 | buffer = Int32Array.from(data).buffer; |
| 633 | } else if (this.dtype === "int8") { |
| 634 | buffer = Int8Array.from(data).buffer; |
| 635 | } else if (this.dtype === "uint8") { |
| 636 | buffer = Uint8Array.from(data).buffer; |
| 637 | } else if (this.dtype === "uint32") { |
| 638 | buffer = Uint32Array.from(data).buffer; |
| 639 | } else { |
| 640 | throw new Error("Unsupported data type " + this.dtype); |
| 641 | } |
| 642 | return this.copyFromRawBytes(new Uint8Array(buffer)); |
| 643 | } |
| 644 | } |
| 645 | /** |
| 646 | * Copy data from raw bytes. |
| 647 | * @param data Uint8Array of bytes. |
no test coverage detected