python调试程序中遇到的不常见bug汇总

在调试程序中,除了一些常见的语法错误,也有各种各样的安装包的兼容性问题。此博客也将不断进行更新!

Bug 1

运行文件出现如下错误:

Unable to open '_objects.pyx': File not found

初步怀疑是import h5py时出错,查看安装包时发现已经安装了h5py包,查找资料有人在安装TensorFlow时也出现过类似问题,其中的一个包prtobuf版本不兼容问题,更改版本为3.6.0即可,然后还是出现同样的错误。

#查看安装清单

最后重新利用conda重新安装了h5py,系统自动安装了其他的几个包,亲测可用,程序运行没问题了。

Bug 2:

出现警告的源代码段:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y))

WARNING:tensorflow:From e:\吴恩达深度学习笔记\编程练习\Convolution model.py:163: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

故障原因函数升级引起的兼容性问题,根据提示,更改如下:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Z3, labels=Y))

Bug 3:出现大量的pylint错误,更改pylint文件。方法:文件---首选项---设置---python.linting.pylintPath,

出现如下搜索框,原位置为pylint,更改为D:\Anaconda\pkgs\pylint-2.2.2-py36_0\Scripts(在anaconda文件夹中寻找pylint应用程序的安装位置)

 

Bug 4:报错from __future__ imports must occur at the beginning of the file,此时把

from __future__ import print_function​​​​​​​

放在第一行即可!

Bug 5:'float' object cannot be interpreted as an integer

num_batchs = num_samples//batch_size
    model.train()
    for k in range(num_batchs):

python3中用双//就可以了,在python中使用单/

Bug 6 :

invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor

#原语句:  
train_loss+=loss.data[0]  
#修改后:  
train_loss+=loss.item()  
#bingo  

Bug 7 :

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('images\eye.jpg',0) #直接读为灰度图像
#opencv方法读取-cv2.calcHist(速度最快)
#图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围
hist_cv = cv2.calcHist([img],[0],None,[256],[0,256])
#numpy方法读取-np.histogram()
hist_np,bins = np.histogram(img.ravel(),256,[0,256])
#numpy的另一种方法读取-np.bincount()(速度=10倍法2)
hist_np2 = np.bincount(img.ravel(),minlength=256)
plt.subplot(221),plt.imshow(img,'gray')
plt.subplot(222),plt.plot(hist_cv)
plt.subplot(223),plt.plot(hist_np)
plt.subplot(224),plt.plot(hist_np2)

运行上段代码时报错:MatplotlibDeprecationWarning: 
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  "Adding an axes using the same arguments as a previous axes "

解决方案:在最后加上plt.show()即可!

plt.show()


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值