
How to display PyTorch Tensor's value without its properties?
9 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!