我想知道如何获得torch::Tensor数据类型的字节大小。
我在网上看到了sizeof(tensor.dtype())应该能工作的地方,但是对于float32张量,它打印出1。
发布于 2022-02-14 09:08:24
我自己终于找到了。
torch::elementSize()返回给定ScalarType的大小。
要从具有caffe2::MetaType类型的caffe2::MetaType转换为Scalar,我必须使用此转换器torch::typeMetaToScalarType(tensor.dtype())
因此,可以这样计算内存中张量的大小:
tensor.numel() * torch::elementSize(torch::typeMetaToScalarType(tensor.dtype()))
文档中没有提到这些功能,因此很难找到它们。
https://stackoverflow.com/questions/71097047
复制相似问题