首页 > 其他分享 >x为False却不执行if x is False?numpy.bool_的坑!

x为False却不执行if x is False?numpy.bool_的坑!

时间:2023-03-03 11:13:39浏览次数:30  
标签:False Python prec bool numpy best

话不多说,上代码:

is_best = prec > best_prec
if is_best is False:
    if epoch - best_ep > 30:
        return

乍一看,Early Stopping丐版实现嘛。但是实际运行的时候,is_best永远不会为False(包括print(is_best)为False的时候)!

为什么?因为is_best是numpy.bool_类型!!!不是原生的Boolean!

解决方案也很简单,换成if not is_best即可。Python不显式指定类型,隐患就体现于此。

标签:False,Python,prec,bool,numpy,best
From: https://www.cnblogs.com/gloomy1999/p/17174825.html

相关文章

  • Pandas使用时间索引筛选时报错 AssertionError: <class 'numpy.ndarray'>
    Icameacrossthesimilarproblem,mysolutionwas:makesuretheindexistypeof'DatetimeIndex',Idothis:df.index=pd.to_datetime(df.index)sortedth......
  • torch&numpy常用函数总结
    np.dot()-向量内积np.matmul()&@运算符-矩阵乘法np.multiply()&*运算符-针对标量的运算(各个维度均可)&np.square()-等价于np.multiply(a,a)链接np.c_()a=n......
  • python numpy中的cumsum的用法
    1、函数作用该函数定义在multiarray.py中有定义defcumsum(self,axis=None,dtype=None,out=None):#realsignatureunknown;restoredfrom__doc__"""......
  • Python - Numpy 学习笔记
     #python-Numpylearningimportnumpyasnp#---Numpy学习笔记---(第四章)---#切片,浅拷贝a=np.arange(10)print(a)b=a[2:7]print(b)a[5]=1000print(a)p......
  • numpy.linspace
    numpy.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)start:序列的起始点.stop:序列的结束点num:生成的样本数,默认是50,非负。endpoint......
  • Numpy
    https://i-beta.cnblogs.com/posts/edit;postId=118585931、简介:NumPy(NumericalPython)是Python语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组......
  • JavaScript Boolean(布尔) 对象
    JavaScript Boolean(布尔) 对象Boolean(布尔)对象用于将非布尔值转换为布尔值(true或者false)。在线实例检查布尔值检查布尔对象是true还是false。完整的Boolea......
  • 用dask并行把大量文本数据读入numpy并分批保存
    导入包importnumpyasnpimportosimportdask看看文件格式和file_list=os.listdir('train_data')print(len(file_list))print(file_list[:100])delayed读......
  • numpy手撕
    importnumpyasnpdefgelu(x):return0.5*x*(1+np.tanh(np.sqrt(2/np.pi)*(x+0.044715*x**3)))defsoftmax(x):exp_x=np.exp(x-np.max......
  • 2023-02-13 Set `"volar.inlayHints.eventArgumentInInlineHandlers": false` to hide
    Set`"volar.inlayHints.eventArgumentInInlineHandlers":false`tohideEventArgumentinInlineHandlers.设置`“volar.inlayHints.eventArgumentInInlineHandlers......