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

4 ビュー (過去 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 Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by