【问题标题】:tensorflow.python.framework.errors.InvalidArgumentError: Field 0 in record 0 is not a valid int32: 1 0tensorflow.python.framework.errors.InvalidArgumentError:记录 0 中的字段 0 不是有效的 int32:1 0
【发布时间】:2016-12-17 05:08:01
【问题描述】:

我的 .CSV 文件包含如下所示的数据... 1 0 0 0 0 0 0 0 0 0 0 0

我正在尝试通过以下代码读取此 .CSV 文件

filename = "alpha_test.csv"
#setup text reader
file_length = file_len(filename)
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TextLineReader(skip_header_lines=0)
_, csv_row = reader.read(filename_queue)

#setuo text reader
listoflists = []
a_list = []
for i in range(0,49):
        listoflists.append((list([0])))


record_defaults =listoflists


data = tf.decode_csv(csv_row,record_defaults=record_defaults,field_delim='\t')
#turn features back into a tensor
features = tf.pack(data)

print("loading "+ str(features)+ "line(s)\n")

with tf.Session() as sess:
    tf.initialize_all_variables().run()

    #start populating filename queue
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners()

    for i in range(file_length):
        example = sess.run([features])
        print(example)


    coord.request_stop()
    coord.join()
    print("\ndone loading")

但它给出了以下错误

ERROR:tensorflow:Exception in QueueRunner: Attempted to use a closed Session.
Traceback (most recent call last):......


tensorflow.python.framework.errors.InvalidArgumentError: Field 0 in record 0 is not a valid int32: 1   0
     [[Node: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32, DT_INT32], field_delim="\t", _device="/job:localhost/replica:0/task:0/cpu:0"](ReaderRead:1, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_1, DecodeCSV/record_defaults_2, DecodeCSV/record_defaults_3, DecodeCSV/record_defaults_4, DecodeCSV/record_defaults_5, DecodeCSV/record_defaults_6, DecodeCSV/record_defaults_7, DecodeCSV/record_defaults_8, DecodeCSV/record_defaults_9, DecodeCSV/record_defaults_10, DecodeCSV/record_defaults_11, DecodeCSV/record_defaults_12, DecodeCSV/record_defaults_13, DecodeCSV/record_defaults_14, DecodeCSV/record_defaults_15, DecodeCSV/record_defaults_16, DecodeCSV/record_defaults_17, DecodeCSV/record_defaults_18, DecodeCSV/record_defaults_19, DecodeCSV/record_defaults_20, DecodeCSV/record_defaults_21, DecodeCSV/record_defaults_22, DecodeCSV/record_defaults_23, DecodeCSV/record_defaults_24, DecodeCSV/record_defaults_25, DecodeCSV/record_defaults_26, DecodeCSV/record_defaults_27, DecodeCSV/record_defaults_28, DecodeCSV/record_defaults_29, DecodeCSV/record_defaults_30, DecodeCSV/record_defaults_31, DecodeCSV/record_defaults_32, DecodeCSV/record_defaults_33, DecodeCSV/record_defaults_34, DecodeCSV/record_defaults_35, DecodeCSV/record_defaults_36, DecodeCSV/record_defaults_37, DecodeCSV/record_defaults_38, DecodeCSV/record_defaults_39, DecodeCSV/record_defaults_40, DecodeCSV/record_defaults_41, DecodeCSV/record_defaults_42, DecodeCSV/record_defaults_43, DecodeCSV/record_defaults_44, DecodeCSV/record_defaults_45, DecodeCSV/record_defaults_46, DecodeCSV/record_defaults_47, DecodeCSV/record_defaults_48)]]
Caused by op u'DecodeCSV', defined at:

我已经用以下数据进行了测试

1   0   0   0   0
0   0   1   1   1
1   0   0   0   0
0   0   1   1   1

但它给出了同样的错误

ERROR:tensorflow:Exception in QueueRunner: Attempted to use a closed Session.

【问题讨论】:

  • 您需要在 TF 会话中运行功能(例如 rval= sess.run(features)),还需要设置协调器并启动队列运行器。有关示例,请参阅Reading Data Howto 的 CSV 文件部分。
  • 您所做的更改看起来不错。由于某种原因, decode_csv 线程似乎正在崩溃。也许“record_defaults”不正确。例如,csv 真的每行有 49 个条目吗? CSV 文件真的是制表符分隔的吗?当我用一个小的测试 csv 运行它并确保我的参数是一致的时,它可以工作。当我有错误的参数时,我会得到你得到的错误。
  • 此文件是制表符分隔的,csv 每行有 49 个实体。我很困惑为 record_defaults 初始化张量对象列表。
  • record_defaults 用于告诉解码器在特定行中缺少值时该怎么做。对于任何缺失值,您设置它的方式将返回 0 作为 int32。 [ [0.0], [0.0], [0.0] ...] 将为任何缺失值返回 float32 0。 [ [], [], [], ... ] 如果缺少任何值,将返回错误。我能想到的唯一另一件事是将路径添加到 csv 文件名,以确保代码能够找到并打开 csv 文件。
  • 我用一个小数据集进行了测试,我把它包括在这里。但是@Bob 给出了同样的问题。

标签: python numpy tensorflow deep-learning


【解决方案1】:

您看到问题的最可能原因是文件第一行中的 1 和 0 实际上由 3 个空格分隔,而不是制表符。请注意在您对decode_csv 的调用中field_delim 是如何正确设置为\t,但它似乎读取的值是1 0。这正是如果 1 和 0 用三个空格分隔,而下一个 0 用制表符分隔会发生的情况。

确保文件不包含空格,并且所有的分隔符实际上都是制表符

【讨论】:

  • 我已经用一个小数据进行了测试,其中有抽头分隔的空间..但它给出了同样的错误
  • 你能把这个小数据集上传到ideone或drop box吗?
猜你喜欢
  • 2018-08-10
  • 2017-11-28
  • 1970-01-01
  • 2012-12-16
  • 2018-09-26
  • 2013-07-15
  • 1970-01-01
  • 2012-10-27
  • 2019-12-23
相关资源
最近更新 更多