简体   繁体   中英

TypeError: Expected String, got 0 of type int instead

i've been using the code from tf.estimate.quickstart to run my own image classification task and i keep getting the error stated in the title, this is the full code:

import numpy as np
import tensorflow as tf
import pandas as pd

train_x = pd.read_csv('train_set_x.csv')
train_y = pd.read_csv('train_set_y.csv')

test_x = pd.read_csv('test_set_x.csv')
test_y = pd.read_csv('test_set_y.csv')
feature_columns = [tf.feature_column.numeric_column("x", shape=[784])]

uni = set()
for i in range(0,26):
str(i)
uni.add(i)

uni = list(uni)


train_input_fn = tf.estimator.inputs.numpy_input_fn(
  x={"x": np.array(train_x)},
  y=np.array(train_y),
  num_epochs=None,
  shuffle=True)
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
  hidden_units=[10, 20, 10],
  n_classes=26,
  model_dir="/tmp/test_run",
  label_vocabulary=uni)


classifier.train(input_fn=train_input_fn, steps=1)

test_input_fn = tf.estimator.inputs.numpy_input_fn(
  x={"x": np.array(test_x)},
  y=np.array(test_y),
  num_epochs=1,
  shuffle=False)
accuracy_score = classifier.evaluate(input_fn = test_input_fn)["Accuracy"]   

print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

the dataset i've used is the EMNIST dataset which contains handwritten letter images in the dimension of 28 x 28 which i've converted into a .csv file, is this the right way to go about image recognition training? the error is occurs in the line classifier.train(input_fn=train_input_fn, steps=1) here is the full stack trace:

Traceback (most recent call last):
  File "C:/Users/Slither/Documents/Scripts/lol.py", line 33, in <module>
    classifier.train(input_fn=train_input_fn, steps=1)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", line 302, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", line 711, in _train_model
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", line 694, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\canned\dnn.py", line 334, in _model_fn
    config=config)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\canned\dnn.py", line 203, in _dnn_model_fn
    logits=logits)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 456, in create_estimator_spec
    name='class_string_lookup')
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\ops\lookup_ops.py", line 1197, in index_to_string_table_from_tensor
    vocabulary_list = ops.convert_to_tensor(vocabulary_list, dtypes.string)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 836, in convert_to_tensor
    as_ref=False)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 926, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 383, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "C:\Users\Slither\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 303, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected string, got 0 of type 'int' instead.

Process finished with exit code 1

词汇表应该是字符串,但您正在添加整数(您正在调用 str(i) 并在您可能表示 i = str(i) 时忽略结果)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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