今天看代码看到的这两个函数,不多数废话,直接上Demo:
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。
参考:
https://pytorch.org/docs/master/generated/torch.isfinite.html
https://blog.csdn.net/iteye_2245/article/details/82448165