TensorFlow报错的解决方案(持续更新)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23142123/article/details/78605722

错误一

ValueError: Initializer for variable rnn/basic_rnn_cell/kernel/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.

X = np.random.randn(2, 2, 1)

# 第二个example长度为1
X[1,1:] = 0
X_lengths = [2, 1]

cell = tf.nn.rnn_cell.BasicRNNCell(num_units=64)

Y = tf.placeholder(tf.int32, [2,2,1])

outputs, last_states = tf.nn.dynamic_rnn(cell=cell, dtype=tf.float64, sequence_length=X_lengths, inputs=Y)
output = tf.reshape(outputs, [-1, 2])


result = tf.contrib.learn.run_n({"outputs": outputs, "last_states": last_states}, n=1, feed_dict={Y:X})

print(result[0])

Y = tf.placeholder(tf.int32, [2,2,1])行的类型需要跟tf.nn.dynamic_rnn中的类型统一,将int32改成float64,运行成功。
此外,tf.nn.dynamic_rnn函数似乎只允许float类型,将两个类型统一成int32,仍然报这个错(I don’t know why now)。

总结:

1)两者类型需要保持一致
2)只支持float类型,float32、float64都可以

错误二

ValueError: setting an array element with a sequence.

from sklearn.decomposition import PCA
import numpy as np

x = np.array([[1.],  [0.9,  0.95],  [1.01,  1.03],  [2.,  2.],  [2.03,  2.06],  [1.98,  1.89],
       [3., 3.],  [3.03,  3.05],  [2.89,  3.1],  [4.,  4.],  [4.06,  4.02],  [3.97,  4.01]])
pca=PCA(n_components=1, copy=False)
print(pca.fit_transform(x))
print(x)

一般这种错误是array中数组长度不统一,如第一个数组[1.]维度出错,与其它数组维度不一致。

错误三

error destroying CUDA event in context 000001EAA2598510: CUDA_ERROR_LAUNCH_FAILED

 labels = tf.one_hot(tf.reshape(output_data, [-1]), depth=vocab_size + 1)
 loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)

代码在这个位置报错,设置它们在cpu下执行即可解决。

  with tf.device('/cpu:0'):
       labels = tf.one_hot(tf.reshape(output_data, [-1]), depth=vocab_size + 1)
       loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)

错误四

ValueError: Shape (?, 1) must have rank at least 3

报错行的tensor要求输入3维的参数,但是shape(?,1)是两维的。改成正确的数据格式即可。

错误五

当编写Python脚本时,中文注释或者输出时,会提示错误:SyntaxError:Non-ASCII character ‘\xe5’ in file

在Python源文件的开始行加上:

# coding:UTF-8
或者
# -*- coding:UTF-8 -*-
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页