我从这个 TensorFlow 的 csv 阅读器中遗漏了什么? [英] What am I missing from this csv reader for TensorFlow?
问题描述
它主要是从网站上的教程复制粘贴.我收到一个错误:
It is mostly a copy paste from the tutorial, on the website. I am getting an error:
无效参数:ConcatOp:在范围 [0, 0), 但得到 0 [[Node: concat = Concat[N=4, T=DT_INT32,_device="/job:localhost/replica:0/task:0/cpu:0"](concat/concat_dim, DecodeCSV, DecodeCSV:1, DecodeCSV:2, DecodeCSV:3)]]
Invalid argument: ConcatOp : Expected concatenating dimensions in the range [0, 0), but got 0 [[Node: concat = Concat[N=4, T=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](concat/concat_dim, DecodeCSV, DecodeCSV:1, DecodeCSV:2, DecodeCSV:3)]]
我的 csv 文件的内容是:
the contents of my csv file is:
3,4,1,8,4
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["test2.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
# print tf.shape(col1)
features = tf.concat(0, [col1, col2, col3, col4])
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(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)
推荐答案
问题是由程序中张量的形状引起的.TL;DR 你应该使用 tf.pack()
而不是 tf.concat()
,它会转换将四个标量 col
张量转换为长度为 4 的一维张量.
The issue arises due to the shape of the tensors in your program. TL;DR Instead of tf.concat()
you should use tf.pack()
, which will transform the four scalar col
tensors into a 1-D tensor of length 4.
在我们开始之前,请注意您可以在任何 Tensor
对象上使用 get_shape()
方法来获取有关该张量的静态形状信息.例如,代码中注释掉的行可能是:
Before we start, note that you can use the get_shape()
method on any Tensor
object to get static shape information about that tensor. For example, the commented-out line in your code could be:
print col1.get_shape()
# ==> 'TensorShape([])' - i.e. `col1` is a scalar.
reader.read()
返回的 value
张量是一个标量字符串.tf.decode_csv(value, record_defaults=[...])
为 record_defaults
的每个元素生成一个与 value
形状相同的张量>,即本例中的标量.标量是具有单个元素的 0 维张量.tf.concat(i, xs)
不是在标量上定义:它将 N 维张量列表 (xs
) 连接成一个新的 N 维张量,沿着维度 i
,其中 0 <=我<N
,如果 N = 0
,则没有有效的 i
.
The value
tensor returned by reader.read()
is a scalar string. tf.decode_csv(value, record_defaults=[...])
produces, for each element of record_defaults
, a tensor of the same shape as value
, i.e. a scalar in this case. A scalar is a 0-dimensional tensor with a single element. tf.concat(i, xs)
is not defined on scalars: it concatenates a list of N-dimensional tensors (xs
) into a new N-dimensional tensor, along dimension i
, where 0 <= i < N
, and there is no valid i
if N = 0
.
tf.pack(xs)
运算符旨在简单地解决这个问题.它需要一个 k
N 维张量列表(具有相同的形状),并将它们打包成一个 N+1 维张量,其大小为 k
在第 0 维.如果您将 tf.concat()
替换为 tf.pack()
,您的程序将可以运行:
The tf.pack(xs)
operator is designed to solve this problem simply. It takes a list of k
N-dimensional tensors (with the same shape) and packs them into an N+1-dimensional tensor with size k
in the 0th dimension. If you replace the tf.concat()
with tf.pack()
, your program will work:
# features = tf.concat(0, [col1, col2, col3, col4])
features = tf.pack([col1, col2, col3, col4])
with tf.Session() as sess:
# Start populating the filename queue.
# ...
这篇关于我从这个 TensorFlow 的 csv 阅读器中遗漏了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!