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

조회 수: 7 (최근 30일)
SHC
SHC 2025년 4월 13일
답변: Angelo Yeo 2025년 4월 13일
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?

채택된 답변

Angelo Yeo
Angelo Yeo 2025년 4월 13일
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()

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by