python oserror 除零_RuntimeWarning:除以零错误:如何避免? PYTHON,NUMPY

I am running in to RuntimeWarning: Invalid value encountered in divide

import numpy

a = numpy.random.rand((1000000, 100))

b = numpy.random.rand((1,100))

dots = numpy.dot(b,a.T)/numpy.dot(b,b)

norms = numpy.linalg.norm(a, axis =1)

angles = dots/norms ### Basically I am calculating angle between 2 vectors

There are some vectors in my a which have norm as 0. so while calculating angles it is giving runtime warning.

Is there a one line pythonic way to compute angles while taking into account norms which are 0?

angles =[i/j if j!=0 else -2 for i,j in zip(dots, norms)] # takes 10.6 seconds

But it takes a lot of time. Since all angles will have values between 1 and -1 and I need only 10 max values this will help me. This takes around 10.6 seconds which is insane.

解决方案

you can ignore warings with the np.errstate context manager and later replace nans with what you want:

import numpy as np

angle = np.arange(-5., 5.)

norm = np.arange(10.)

with np.errstate(divide='ignore'):

print np.where(norm != 0., angle / norm, -2)

# or:

with np.errstate(divide='ignore'):

res = angle/norm

res[np.isnan(res)] = -2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OSError: libtorch_cuda.so: cannot open shared object file: No such file or directory是一个错误消息,它表示在运行过程中找不到libtorch_cuda.so共享对象文件。该文件是与CUDA相关的Torch库的一部分。根据引用和引用的信息,这个错误可能是由于缺少libtorch_cuda_cu.so或libtorch_cuda_cpp.so这些文件导致的。 为了解决这个问题,你可以尝试以下步骤: 1. 确保你已经正确安装了CUDA和Torch。这些库需要在系统中正确配置,并且路径也需要正确设置。 2. 检查你的系统中是否存在libtorch_cuda_cu.so和libtorch_cuda_cpp.so这两个文件。如果没有,你可能需要重新安装或更新Torch来获取这些文件。 3. 确保你的系统中存在所需的依赖库。有时候缺少其他依赖库也会导致无法找到共享对象文件的错误。你可以通过安装缺失的依赖库来解决这个问题。 4. 检查你的环境变量设置。确保你的LD_LIBRARY_PATH和LD_PRELOAD等环境变量正确设置,以使系统能够正确找到共享对象文件。 5. 如果你使用的是虚拟环境,请确保你在虚拟环境中正确安装了所有必要的库和依赖项。 如果你遵循上述步骤仍然无法解决问题,你可能需要进一步调查和调试。你可以查看更多关于libtorch_cuda.so缺失的信息,并尝试在相关的社区或论坛上查找帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [libtorch_cuda_cu.so: cannot open shared object file](https://blog.csdn.net/wudi_cn/article/details/126737202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [OSError: libtorch_cuda_cpp.so: cannot open shared object file: No such file or directory(已解决)](https://blog.csdn.net/weixin_42278880/article/details/125770868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [darknet-ocr文字检测部分单独实现(darknet_ocr)](https://download.csdn.net/download/weixin_38551187/14855354)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值