torch.Tensor.Tensor.toString
tensor.toString(): stringReturns a string representation of the tensor.
Formats the tensor data for display, respecting the global print options
set by torch.set_printoptions(). For CPU tensors, shows the actual data.
For WebGPU tensors, shows a placeholder (data is on GPU).
Formatting by dtype:
bool: Showstrueorfalseint32,int64: Shows integers without decimal placesfloat32,float16: Shows with configurable precisioncomplex64: Shows asreal+imag*jformat
Print options:
precision: Number of decimal places for floats (default: 4)threshold: Max elements before summarizing with ellipsis (default: 1000)edgeitems: Elements to show at each edge when summarizing (default: 3)
Returns
string– String representation of the tensorExamples
const x = torch.tensor([[1.5, 2.5], [3.5, 4.5]]);
console.log(x.toString());
// tensor([[1.5000, 2.5000],
// [3.5000, 4.5000]])
const y = torch.tensor([true, false, true], { dtype: 'bool' });
console.log(y.toString());
// tensor([true, false, true], dtype=bool)
// Configure print options
torch.set_printoptions({ precision: 2 });
console.log(x.toString());
// tensor([[1.50, 2.50],
// [3.50, 4.50]])