[英]dtype error when training neural network with TensorFlow
我正在尝试使用字符作为输入来训练分类模型。 我的网络基于TensorFlow教程之一。 我花了好几个小时弄清楚为什么它无法运行。 错误是:
File "estimator.py", line 96, in main
steps=train_steps)
ValueError: Labels dtype should be integer. Instead got <dtype: 'string'>.
我的代码是:
import tensorflow as tf
import data
def main(argv):
(train_x, train_y), (test_x, test_y) = data.load_data()
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0"]
categorical_column_1 = tf.feature_column.categorical_column_with_vocabulary_list(key='Char1',
vocabulary_list=alphabet,
default_value=0, dtype=tf.string)
...
categorical_column_16 = tf.feature_column.categorical_column_with_vocabulary_list(key='Char16',
vocabulary_list=alphabet,
default_value=0, dtype=tf.string)
my_feature_columns = [
tf.feature_column.indicator_column(categorical_column_1),
...
tf.feature_column.indicator_column(categorical_column_16),
]
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns, hidden_units=[16, 16], n_classes=4
)
batch_size = 100
train_steps = 100
classifier.train(
input_fn=lambda: data.train_input_fn(train_x, train_y, batch_size),
steps=train_steps)
eval_result = classifier.evaluate(
input_fn=lambda: data.test_input_fn(test_x, test_y, batch_size)
)
print("\nAccuracy with test data: {accuracy:0.2f}\n".format(**eval_result))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)
我以前有这个问题。 它可能有两个原因:
我使用了Jupyter-Notebook
。 它具有存储某些特定内容的缓存。 您应该清空缓存,例如,重新启动系统。 重新启动内核没有用,因为数据仍然存在于内存中。
我到处搜索,但解决方案不好。 我用两个不同的数据集(MNIST和我创建的另一个)训练了模型,并创建了问题。 您应该只尝试一个数据集。如果您使用两个数据集,这就是问题所在。
您创建的数据类型与模型不相同。 您应该将输入或代码更改为相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.