无法将
torch.rand()
的数据类型设置为 int
。设置为 double
时的同一行代码
x = torch.rand(2,2,dtype=torch.double)
没有抛出任何异常并且工作完美。 int
抛出 RuntimeError 的原因是什么?
Python版本:3.9.6
这是代码片段:
import torch
x = torch.rand(2,2,dtype=torch.int)
y = torch.rand(2,2,dtype=torch.int)
print("x is", x)
print("y is", y)
z = torch.add(x,y)
print(z)
错误输出:
x = torch.rand(2,2,dtype=torch.int)
Traceback (most recent call last):
File "/Training/main.py", line 4, in <module>
x = torch.rand(2,2,dtype=torch.int)
RuntimeError: "check_uniform_bounds" not implemented for 'Int'
torch.rand
仅针对 float 类型 实现,如果使用 int
,您的代码将在 PyTorch 中触发运行时错误:
torch.rand(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False)
返回一个张量,其中填充有区间 [0,1) 上均匀分布的随机数
张量的形状由变量参数大小定义
链接: