pytorch中torch.isnan()和torch.isfinite()

1.torch.isfinite()

import torch
num = torch.tensor(1)  # 数字1
res = torch.isfinite(num)
print(res)
'''
输出:
tensor(True)
'''

这个num必须是tensor

import torch
num = torch.tensor(float('inf')) # 正无穷大
res = torch.isfinite(num)
print(res)
'''
输出:
tensor(False)
'''
import torch
num = torch.tensor(float('-inf')) # 负无穷大
res = torch.isfinite(num)
print(res)
'''
输出:
tensor(False)
'''
import torch
num = torch.tensor(float('nan')) # 空
res = torch.isfinite(num)
print(res)
'''
输出:
tensor(False)
'''

2.torch.isnan()

import torch
res=torch.isnan(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
print(res)
'''
输出:
tensor([False, False, False, False,  True])
'''

可以看出torch.isnan()是用来判断输入的张量是否为空的函数,当输入为空是,返回True。

承接Matlab、Python和C++的编程,机器学习、计算机视觉的理论实现及辅导,本科和硕士的均可,咸鱼交易,专业回答请走知乎,详谈请联系QQ号757160542,非诚勿扰。

猜你喜欢

转载自blog.csdn.net/weixin_36670529/article/details/113903534