类 'bool' 的未解析的特性引用 'to'
时间: 2023-08-06 20:23:28 浏览: 116
这个错误是因为在 PyTorch 中,bool 类型的 tensor 不支持 `to()` 方法。
如果你想将一个 bool 类型的 tensor 转换成 int 类型的 tensor,可以使用 `.type(torch.int)` 方法,如下所示:
```python
(pred.argmax(1) == y).type(torch.int).sum().item()
```
这里的 `type(torch.int)` 方法将 bool 类型的 tensor 转换成了 int 类型的 tensor。然后,你可以继续对这个 int 类型的 tensor 进行 sum() 和 item() 操作,得到 True 的个数。
相关问题
类 'bool' 的未解析的特性引用 'int'
这个错误是因为在 PyTorch 中,bool 类型的 tensor 不支持 int() 方法。
如果你想将一个 bool 类型的 tensor 转换成 int 类型的 tensor,可以使用 `.to(torch.int)` 方法,如下所示:
```python
(pred.argmax(1) == y).to(torch.int).sum().item()
```
这里的 `to(torch.int)` 方法将 bool 类型的 tensor 转换成了 int 类型的 tensor。然后,你可以继续对这个 int 类型的 tensor 进行 sum() 和 item() 操作,得到 True 的个数。
类 'bool' 的未解析的特性引用 'sum'
这个错误是因为在 PyTorch 中,bool 类型的 tensor 不支持 sum() 方法。
如果你想将一个 bool 类型的 tensor 中 True 的个数统计出来,可以先将其转换成 int 类型的 tensor,然后再调用 sum() 方法,如下所示:
```python
(pred.argmax(1) == y).int().sum().item()
```
这里的 `int()` 方法将 bool 类型的 tensor 转换成了 int 类型的 tensor。然后,你可以继续对这个 int 类型的 tensor 进行 sum() 和 item() 操作,得到 True 的个数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)