TensorFlow中常见的错误解释及解决方法
1、AttributeError: ‘tuple’ object has no attribute ‘ndims’
使用TensorFlow中的keras接口来建立模型,然后调用的时候出现上述AttributeError: ‘tuple’ object has no attribute ‘ndims’错误。这时候需要检查input的数据是否是Tensor格式。我之前遇到这个问题一直以为是keras的input不支持list的原因(我自定义的Layer,输入是多个array组合的list),最终发现是我的输入数据是两个numpy array组成的list,只需要把numpy array list转换成tensor的list即可。
如:
input1 = np.zeors((3,3))
input2 = np.zeors((4,2))
customer_layer = CustomerLayer()
# 如下使用方法会报错
res = customer_layer([input1,input2])
上述代码会出错,需要将两个input转换成tensor
# 加一个转换
input1 = tf.conver_to_tensor(np.zeors((3,3)))
input2 = tf.conver_to_tensor(np.zeors((4,2)))
customer_layer = CustomerLayer()
# 如下使用方法会报错
res = customer_layer([input1,input2])
2、FailedPreconditionError (see above for traceback): Error while reading resource variable encoder_layer/kernel from Container: localhost.
完整的错误显示如下:
FailedPreconditionError (see above for traceback): Error while reading resource variable encoder_layer/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/encoder_layer/kernel)
[[node encoder_layer/kernel/Read/ReadVariableOp (defined at attention.py:81) ]]
这个错误是由于变量没有初始化导致的,首先确保包含如下初始化代码:
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
其次,需要所有的涉及模型定义或者是变量的定义都需要放在这段代码之前定义。
欢迎大家关注DataLearner官方微信,接受最新的AI技术推送
