Tensorflow:Int,float类型在record_defaults中不起作用

Posted

技术标签:

【中文标题】Tensorflow:Int,float类型在record_defaults中不起作用【英文标题】:Tensorflow: Int, float types not working in record_defaults 【发布时间】:2017-09-13 16:39:51 【问题描述】:

我想导入各种类型的列,如 int、float、string 作为张量。只有第一个 record_defaults 工作,其中所有设置为字符串。

但我收到以下错误。有什么方法可以将 tensorflow 与各种类型的占位符一起使用?

csv 文件来自aws。

import tensorflow as tf

# https://s3.amazonaws.com/aml-sample-data/banking.csv
file1="/Users/Q/Downloads/banking.csv"
filename_queue = tf.train.string_input_producer([file1])
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)

# record_defaults = [[""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0]]
# record_defaults = [[0], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0], [0], [0], [0], [""], [0.0], [0.0], [0.0], [0.0], [0.0], [0]]
record_defaults = [[0], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0]]

cols = tf.decode_csv(value, record_defaults=record_defaults, field_delim=",")

features = tf.stack(cols[:-1])

with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(1000):
    # Retrieve a single instance:
    example, label = sess.run([features, cols[-1]])

  coord.request_stop()
  coord.join(threads)

错误信息

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/Q/Dropbox/code/Analyzing-Tea/tf-test.py
Traceback (most recent call last):
  File "/Users/Q/Dropbox/code/Analyzing-Tea/tf-test.py", line 14, in <module>
    features = tf.stack(cols[:-1])
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/array_ops.py", line 715, in stack
    return gen_array_ops._pack(values, axis=axis, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1975, in _pack
    result = _op_def_lib.apply_op("Pack", values=values, axis=axis, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 463, in apply_op
    raise TypeError("%s that don't all match." % prefix)
TypeError: Tensors in list passed to 'values' of 'Pack' Op have types [int32, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string] that don't all match.

Process finished with exit code 1

【问题讨论】:

【参考方案1】:

罪魁祸首是这行:features = tf.stack(cols[:-1])。它试图将所有特征列张量堆叠成一个张量,只有当它们是相同类型时才会起作用(并且类型是从记录默认值推导出来的)。

所以不要把它们都堆起来,你会没事的。

【讨论】:

谢谢@alexandre-passos!我认为错误消息“不完全匹配”意味着我给了某个列的错误类型,但这意味着所有列都应该具有相同的类型。 是的,如果你想在一个张量 (tf.stack) 中连接所有列,那么它们需要具有相同的类型。 根据本文档tensorflow.org/programmers_guide/reading_data处理csv文件,我们需要将所有功能放在一起,如果它们是不同类型的,我们如何处理它们? 您需要先将所有特征转为浮点数。请参阅 tensorflow feature columns 获取 API 以轻松完成此操作。

以上是关于Tensorflow:Int,float类型在record_defaults中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

『TensorFlow』函数查询列表_张量属性调整

TypeError:“Mul”Op 的输入“y”的类型为 float32,与参数“x”的类型 int32 不匹配

tensorflow知识点总结

tf.cast()数据类型转换

tensorflow的数据类型

『TensorFlow』网络操作API_下