Pytorch 运行时错误:“check_uniform_bounds”未针对“Int”实现

问题描述 投票:0回答:2

无法将

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'
python python-3.x deep-learning pytorch
2个回答
4
投票

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) 上均匀分布的随机数

张量的形状由变量参数大小定义

链接:

https://pytorch.org/docs/stable/ generated/torch.set_default_tensor_type.html#torch.set_default_tensor_type

https://pytorch.org/docs/stable/ generated/torch.rand.html


0
投票

如果你想要整数,你可以使用

torch.randint
[1]

[1] https://pytorch.org/docs/stable/ generated/torch.randint.html

© www.soinside.com 2019 - 2024. All rights reserved.