简体   繁体   English

使用TensorFlow训练神经网络时dtype错误

[英]dtype error when training neural network with TensorFlow

I'm trying to train a classification model using characters as input. 我正在尝试使用字符作为输入来训练分类模型。 I based my network off of one of the TensorFlow tutorials. 我的网络基于TensorFlow教程之一。 I've spent hours racking my head as to why it won't run. 我花了好几个小时弄清楚为什么它无法运行。 The error is: 错误是:

File "estimator.py", line 96, in main
    steps=train_steps)
ValueError: Labels dtype should be integer. Instead got <dtype: 'string'>.

My code is: 我的代码是:

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)

I had this problem before. 我以前有这个问题。 It may has 2 reason: 它可能有两个原因:

  1. I used Jupyter-Notebook . 我使用了Jupyter-Notebook It has a cache that stores some specific things. 它具有存储某些特定内容的缓存。 you should empty your cache, for example, restart your System. 您应该清空缓存,例如,重新启动系统。 restarting the kernel is not useful because the data still exist in your memory. 重新启动内核没有用,因为数据仍然存在于内存中。

    I searched everywhere but the solutions were not good. 我到处搜索,但解决方案不好。 I trained my model with two different datasets(MNIST and the other that I created) and the problem created. 我用两个不同的数据集(MNIST和我创建的另一个)训练了模型,并创建了问题。 you should try only one dataset If you use two datasets this is the problem. 您应该只尝试一个数据集。如果您使用两个数据集,这就是问题所在。

  2. your datatype that you create is not the same with your model. 您创建的数据类型与模型不相同。 you should change the input or the code to be the same. 您应该将输入或代码更改为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM