【问题标题】:Tensorflow Access CsvDataset valuesTensorflow 访问 CsvDataset 值
【发布时间】:2018-12-13 03:17:02
【问题描述】:

急切执行

我已经通过 API 挖掘了 2 天,但我似乎无法找到使用来自 CsvDataset 对象的数据的方法。

我有以下来自数据集的样本:

70,1,4,130,322,0,2,109,0,24,2,3,3,2 67,0,3,115,564,0,2,160,0,16,2,0,7,1 57,1,2,124,261,0,0,141,0,3,1,0,7,2 64,1,4,128,263,0,0,105,1,2,2,1,7,1 74,0,2,120,269,0,2,121,1,2,1,1,3,1 65,1,4,120,177,0,0,140,0,4,1,0,7,1 56,1,3,130,256,1,2,142,1,6,2,1,6,2 59,1,4,110,239,0,2,142,1,12,2,1,7,2 60,1,4,140,293,0,2,170,0,12,2,2,7,2 63,0,4,150,407,0,2,154,0,4,2,3,7,2

我阅读了他们在高级 API 视频中所说的 csv:

tf.enable_eager_execution()
defaults = [tf.float64] * 14
dataset=tf.data.experimental.CsvDataset(path, defaults)
>>> dataset
>>> <CsvDataset shapes: ((), (), (), (), (), (), (), (), (), (), (), (), (), ()), types: (tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64)>

但从这里开始,我无法访问任何数据,例如获取列的值。

使用list(dataset) 将数据集转换为列表不是一种选择,因为正常大小的 csv(约 190k 样本)需要很长时间。

那么,有没有办法从这个对象中获取列值或行值?或者说用 TF 读取数据而不用 scikit/pandas 真的没有意义吗?

编辑 1: 正如@kvish 所说,尝试做col1 = dataset.map(lambda *row: row[0]),这会返回一个可迭代的&lt;MapDataset shapes: (), types: tf.float64&gt;。问题是必须遍历每一列然后遍历每个 MapDataset 会使复杂性 O(n^2)

idea 输出将是一个张量列表,每个张量包含一个列中的所有值,类似于:

[<tf.Tensor(shape=(10,), dtype=float64, 
numpy=array([70.0,67.0,57.0,64.0,74.0,65.0,56.0,59.0,60.0,63.0]))>,
(...) x14]

【问题讨论】:

  • 要获取行值,您只需要定义一个迭代器。以下是可用的各种迭代器的examples。就专栏而言,这是一个有趣的问题。我不熟悉可用于执行此操作的方法,但您可以轻松做的一件事是定义一个新的数据集对象。例如 col1 = dataset.map(lambda row: row[0])。该对象将包含整个第 1 列。然后您可以像上面所做的那样定义一个迭代器来遍历它。
  • @kvish 将检查这一点,运行测试以与我当前的 pandas 函数进行比较。很快就会发布结果,非常感谢。
  • 如果您正在运行测试,那么我建议您查看 Input Pipeline Performance Guide 以更好地了解如何利用优化。
  • 输入管道是否适合 Eager?我不需要连续的数据流,只需要读取和拆分数据集。
  • 如果是这种情况,那么在急切的设置中您可能没有太多用处。但是,如果您想使用估算器来训练数据,应用特征列等转换仍然很有用

标签: python tensorflow tensorflow-datasets eager-execution


【解决方案1】:

好的,在@kvish 的帮助下,我找到了解决方案。在此解决方案中,需要预先知道行数和列数。

col_list = []
append = col_list.append
to_tensor = tf.convert_to_tensor
for i in range(0,cols):
    col = dataset.map(lambda *row: row[i])
    col = to_tensor(*col.batch(rows))
    append(col)

这将输出所需的:

>>> col_list
[<tf.Tensor: id=256, shape=(10,), dtype=float64, numpy=array([70., 67., 57., 64., 
74., 65., 56., 59., 60., 63.])>, <tf.Tensor: id=282, shape=(10,), dtype=float64, 
numpy=array([1., 0., 1., 1., 0., 1., 1., 1., 1., 0.])>, <tf.Tensor: id=308, shape= 
(10,), dtype=float64, numpy=array([4., 3., 2., 4., 2., 4., 3., 4., 4., 4.])>, 
<tf.Tensor: id=334, shape=(10,), dtype=float64, numpy=array([130., 115., 124., 128., 
120., 120., 130., 110., 140., 150.])>, <tf.Tensor: id=360, shape=(10,), 
dtype=float64, numpy=array([322., 564., 261., 263., 269., 177., 256., 239., 293., 
407.])>, <tf.Tensor: id=386, shape=(10,), dtype=float64, numpy=array([0., 0., 0., 0., 
0., 0., 1., 0., 0., 0.])>, <tf.Tensor: id=412, shape=(10,), dtype=float64, 
numpy=array([2., 2., 0., 0., 2., 0., 2., 2., 2., 2.])>, <tf.Tensor: id=438, shape= 
(10,), dtype=float64, numpy=array([109., 160., 141., 105., 121., 140., 142., 142., 
170., 154.])>, <tf.Tensor: id=464, shape=(10,), dtype=float64, numpy=array([0., 0., 
0., 1., 1., 0., 1., 1., 0., 0.])>, <tf.Tensor: id=490, shape=(10,), dtype=float64, 
numpy=array([24., 16.,  3.,  2.,  2.,  4.,  6., 12., 12.,  4.])>, <tf.Tensor: id=516, 
shape=(10,), dtype=float64, numpy=array([2., 2., 1., 2., 1., 1., 2., 2., 2., 2.])>, 
<tf.Tensor: id=542, shape=(10,), dtype=float64, numpy=array([3., 0., 0., 1., 1., 0., 
1., 1., 2., 3.])>, <tf.Tensor: id=568, shape=(10,), dtype=float64, numpy=array([3., 
7., 7., 7., 3., 7., 6., 7., 7., 7.])>, <tf.Tensor: id=594, shape=(10,), 
dtype=float64, numpy=array([2., 1., 2., 1., 1., 1., 2., 2., 2., 2.])>]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多