RuntimeWarning: invalid value encountered in greater

前端 未结 2 2093
长情又很酷
长情又很酷 2021-02-11 14:59

I tried to implement soft-max with the following code (out_vec is a numpy vector of floats):

numerator = np.exp(out_vec)
denominator =          


        
相关标签:
2条回答
  • 2021-02-11 15:31

    In my case the warning did not show up when calling this before the comparison (I had NaN values getting compared)

    np.warnings.filterwarnings('ignore')
    
    0 讨论(0)
  • 2021-02-11 15:34

    Your problem is caused by the NaN or Inf elements in your out_vec array. You could use the following code to avoid this problem:

    if np.isnan(np.sum(out_vec)):
        out_vec = out_vec[~numpy.isnan(out_vec)] # just remove nan elements from vector
    out_vec[out_vec > 709] = 709
    ...
    

    or you could use the following code to leave the NaN values in your array:

    out_vec[ np.array([e > 709 if ~np.isnan(e) else False for e in out_vec], dtype=bool) ] = 709
    
    0 讨论(0)
提交回复
热议问题