【B-1】Tensorflow的问题汇总

下面是我之前用tensorflow出现的问题:
(1)
使用pycharm,每次涉及到第三方库的升级报错,非常容易解决。

**F:\Anaconda\lib\site-packages\h5py__init__.py:36:
FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters**

问题在于h5py包没有更新,我解决的方法直接在pycharm-file-settings-project interpreter 将h5py更新到2.8.0版本
这里写图片描述

当然也可以在cmd中pip更新包:
pip install h5py==2.8.0rc1

(2)

tf.zeros([3, 4], int32)
Traceback (most recent call last):

File “D:/PycharmProjects/验证码识别小项目.py”, line 40, in
tf.zeros([3, 4], int32)
NameError: name ‘int32’ is not defined
解决:
tensorflow中没有导入int32,应该加入from tensorflow import int32
在tensorflow中一般的数据类型选用float32,不容易出错

(3)

import input_data

ImportError: No module named input_data
新旧的问题, 新的tutorial采用from tensorflow.examples.tutorials.mnist import input_data导入。
import input_data 代码换成 from tensorflow.examples.tutorials.mnist import input_data

(4)

print(“shape of ‘trainimg’ is %s” %(trainimg.shape))

TypeError: not all arguments converted during string formatting
%和后面的参数数量不对应
改为:

print(“shape of ‘trainimg’ is %s” %(trainimg.shape,))

% 操作符只能直接用于字符串(‘123’),列表([1,2,3])、元组
举例:

 >>> print 'str= %s' % (str,)
 str= (1, 2, 3)
也可以用:
 >>> print 'str= %s,%s,%s' % str
 str= 1,2,3 

(5)

sess = tf.Session()

arr = np.array([[31,23,4,24,27,34],
               [18,3,25,0,6,35],
               [28,14,33,22,20,8],
               [13,30,21,19,7,9],
               [16,1,26,32,2,29],
               [17,12,5,11,10,15]])
#rank函数看矩阵维度
#tensorflow中打印东西需要加上evaluate()
tf.rank(arr).eval()

raise ValueError(“Cannot evaluate tensor using eval(): No default ”
ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use with sess.as_default() or pass an explicit session to eval(session=sess)
问题解决见另外一篇博客tensorfolw中eval()和run()的用法和区别:

(6)
AttributeError: module ‘tensorflow’ has no attribute ‘palceholder’
参考网站:
https://stackoverflow.com/questions/37383812/tensorflow-module-object-has-no-attribute-placeholder


原文如下:
Solution: Do not use “tensorflow” as your filename.
Notice that you use tensorflow.py as your filename. And I guess you write code like:
import tensorflow as tf
Then you are actually importing the script file “tensorflow.py” that is under your current working directory, rather than the “real” tensorflow module from Google.
Here is the order in which a module will be searched when importing:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default.


翻译如下:
可能是你的工程文件夹或路径内有一个叫tensorflow的文件或文件夹,与tensorflow这个模块名字有冲突了,系统调用的时候可能调用的是那个文件或文件夹,而不是你想要的tensorflow模块。可以将那个文件或文件夹改个别的名字。
我的问题就出在此,工作路径下面建立一了个系文件夹 tensorflow,将此文件夹重新命名即可。当然了有一次我将“placeholder”拼成了“palceholder”,也会出异常,找了好久才找到。手动微笑

(7)
AttributeError: module ‘tensorflow’ has no attribute ‘mul’
解决方法:
tf.mul已经在新版本中被移除,请使用 tf.multiply 代替

(8)

#反向传播
#Loss and optimiser
#损失 交叉熵函数softmax_cross_entropy_with_logits(pred,y) 输入:第一参数:预测值,即一次前向传播的结果;第二参数实际的label值
#平均的loss reduce_mean 表示最后的结果除以n
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))

ValueError: Only call softmax_cross_entropy_with_logits with named arguments (labels=…, logits=…, …)
softmax_cross_entropy_with_logits函数,不能按以前的方式进行调用了,只能使用命名参数的方式来调用。原来是这样的:
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
现在需要修改为:
tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_))
原程序改为:

#反向传播
#Loss and optimiser
#损失 交叉熵函数softmax_cross_entropy_with_logits(pred,y) 输入:第一参数:预测值,即一次前向传播的结果;第二参数实际的label值
#平均的loss reduce_mean 表示最后的结果除以n
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值