* Internal method used by public APIs for tensor creation. Makes a new * tensor with the provided shape, dtype and values. It always * creates a new data id and writes the values to the underlying backend.
(
values: DataValues, shape: number[], dtype: DataType,
backend?: KernelBackend)
| 801 | * creates a new data id and writes the values to the underlying backend. |
| 802 | */ |
| 803 | makeTensor( |
| 804 | values: DataValues, shape: number[], dtype: DataType, |
| 805 | backend?: KernelBackend): Tensor { |
| 806 | if (values == null) { |
| 807 | throw new Error('Values passed to engine.makeTensor() are null'); |
| 808 | } |
| 809 | dtype = dtype || 'float32'; |
| 810 | backend = backend || this.backend; |
| 811 | let backendVals = values as BackendValues; |
| 812 | if (dtype === 'string' && util.isString(values[0])) { |
| 813 | backendVals = (values as string[]).map(d => util.encodeString(d)); |
| 814 | } |
| 815 | const dataId = backend.write(backendVals, shape, dtype); |
| 816 | const t = new Tensor(shape, dtype, dataId, this.nextTensorId()); |
| 817 | this.trackTensor(t, backend); |
| 818 | |
| 819 | // Count bytes for string tensors. |
| 820 | if (dtype === 'string') { |
| 821 | const info = this.state.tensorInfo.get(dataId); |
| 822 | const newBytes = bytesFromStringArray(backendVals as Uint8Array[]); |
| 823 | this.state.numBytes += newBytes - info.bytes; |
| 824 | info.bytes = newBytes; |
| 825 | } |
| 826 | return t; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Internal method used by backends. Makes a new tensor |
no test coverage detected