tensorflow初学遇到的报错积累

@Tensorflow 学习中遇到的错误汇总

运行环境 win7_64 python3.6 jupyter notebook tensorflow1.2.0

TypeError Traceback (most recent call last)
in
1 k1 = tf.constant([2, 5])
2 k2 = tf.constant([1.0, 4.0])
----> 3 result = tf.add(k1, k2)

TypeError: Input ‘y’ of ‘Add’ Op has type float32 that does not match type int32 of argument ‘x’.

类型错误,k1与k2的类型必须保持一致。

InvalidArgumentError Traceback (most recent call last)
in
4 sess = tf.Session()
5 with sess.as_default():
----> 6 print(result.eval())

InvalidArgumentError (see above for traceback): Cannot assign a device for operation ‘Add_17’: Operation was explicitly assigned to /device:GPU:3 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
[[Node: Add_17 = Add[T=DT_INT32, _device="/device:GPU:3"](Const_39, Const_40)]]

这个错误应该是gpu指定不合法造成的,通过论坛查看他人的解决方法
第一种
添加语句:with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True)) as sess:
未能成功解决我的问题
第二种:
添加语句:with tf.device(None):
还是未能成功解决我的问题
第三种:
import tensorflow as tf
c = tf.zeros([2, 2], tf.float32)
d = tf.zeros_like(a, optimize=True)

加上下面一行就可以使用 个gpu了

config = tf.ConfigProto(allow_soft_placement=True)

这一行设置 gpu 随使用增长

config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
print(sess.run©)
print(sess.run(d))

问题成功解决,但是后面的代码必须添加tf.Session(config=config),否则就会继续报上面的错误

in
9 # Do some work with the model.
10 # Save the variables to disk.
—> 11 save_path = saver.save(sess, “C://tensorflow//model//test”)
12 print ("Model saved in file: ", save_path)

ValueError: Parent directory of C://tensorflow//model//test doesn’t exist, can’t save.

未定义C://tensorflow//model//test目录

WARNING:tensorflow:From D:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use tf.global_variables_initializer instead.
修改初始化变量方式为global_variables_initializer()方法
成功解决问题

import tensorflow as tf

var1 = tf.Variable([0, 0], name = “v1”)
var2 = tf.Variable([0, 0], name = “v2”)
saver = tf.train.Saver()
module_file = tf.train.latest_checkpoint(‘test/’)
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
saver.restore(sess, module_file)
print(“Model restored.”)

NotFoundError (see above for traceback): Key v2_9 not found in checkpoint
[[Node: save_10/RestoreV2_32 = RestoreV2[dtypes=[DT_INT32], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save_10/Const_0_0, save_10/RestoreV2_32/tensor_names, save_10/RestoreV2_32/shape_and_slices)]]

还未找到对应解决方法,后面啃到事务回滚再来填这个坑

NotFoundError Traceback (most recent call last)
in
7 config.gpu_options.allow_growth = True
8 with tf.Session(config=config) as sess:
----> 9 saver.restore(sess,module_file)
10 print(“Model restored.”)
NotFoundError (see above for traceback): Key v1_10 not found in checkpoint
[[Node: save_16/RestoreV2_17 = RestoreV2[dtypes=[DT_INT32], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save_16/Const_0_0, save_16/RestoreV2_17/tensor_names, save_16/RestoreV2_17/shape_and_slices)]]

未找到对应的解决方法。
先写到这里,洗漱,准备上班……

你可能感兴趣的:(tensorflow,python)