How to display PyTorch Tensor's value without its properties?

11 views (last 30 days)
Hello,
When I run the following MATLAB code to define a tensor variable from PyTorch,
clear all; format compact
pe = pyenv();
a_tensor = py.torch.tensor([1, 2]);
a_tensor
MATLAB's output is as below:
a_tensor =
Python Tensor with properties:
T: [1x1 py.torch.Tensor]
data: [1x1 py.torch.Tensor]
device: [1x1 py.torch.device]
dtype: [1x1 py.torch.dtype]
grad: [1x1 py.NoneType]
grad_fn: [1x1 py.NoneType]
is_cpu: 1
is_cuda: 0
is_ipu: 0
is_leaf: 1
is_maia: 0
is_meta: 0
is_mkldnn: 0
is_mps: 0
is_mtia: 0
is_nested: 0
is_quantized: 0
is_sparse: 0
is_sparse_csr: 0
is_vulkan: 0
is_xla: 0
is_xpu: 0
itemsize: [1x1 py.int]
layout: [1x1 py.torch.layout]
name: [1x1 py.NoneType]
names: [1x1 py.tuple]
nbytes: [1x1 py.int]
ndim: [1x1 py.int]
output_nr: [1x1 py.int]
real: [1x1 py.torch.Tensor]
requires_grad: 0
retains_grad: 0
shape: [1x1 py.torch.Size]
volatile: 0
tensor([1., 2.])
The last line of the output shows actual values of the PyTorch tensor variable, but MATLAB shows all the properties of the tensor variable together.
Is there a way not to show the properties when I call the tensor variable name?

Accepted Answer

Angelo Yeo
Angelo Yeo on 13 Apr 2025
You can consider using tolist method. Converting a tensor to list will only leave the values in a list format.
clear all; format compact
pe = pyenv();
a_tensor = py.torch.tensor([1, 2]);
a_tensor.tolist()

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!