如何使用iInstance断言torch.float32/64?

2024-04-26 01:06:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的深度学习项目编写一个管道,作为一个好的实践,我尝试做一些断言以确保数据类型匹配,以便以后更容易调试!但是,我不知道如何使用isinstance来断言torch.Tensor's{}

例如:

    assert isinstance(image, torch.Tensor) and isinstance(target['boxes'], torch.Tensor)
    assert isinstance(image.dtype, (torch.float32, torch.float64)) and isinstance(target['boxes'].dtype, (torch.float32, torch.float64)) 

使用assert image.dtype in [torch.float32, torch.float64]是这里唯一的方法吗?有没有一种优雅的方式可以做到这一点


Tags: and项目imagetarget管道torchassert断言
1条回答
网友
1楼 · 发布于 2024-04-26 01:06:59

您可以使用^{}

assert torch.is_floating_point(image) and torch.is_floating_point(target['boxes'])

如果输入不是张量,该函数将引发异常。因此,没有必要对torch.Tensor进行独立检查

相关问题 更多 >