【问题标题】:Tensorflow: using an input-pipeline (.csv) as a dictionary for trainingTensorflow:使用输入管道(.csv)作为训练字典
【发布时间】:2023-04-03 16:14:02
【问题描述】:

我正在尝试在 .csv 数据集(5008 列,533 行)上训练模型。 我正在使用文本阅读器将数据解析为两个张量,一个保存要在 [example] 上训练的数据,另一个保存正确的标签 [label]:

def read_my_file_format(filename_queue):
    reader = tf.TextLineReader()
    key, record_string = reader.read(filename_queue)
    record_defaults = [[0.5] for row in range(5008)]

    #Left out most of the columns for obvious reasons
    col1, col2, col3, ..., col5008 = tf.decode_csv(record_string, record_defaults=record_defaults)
    example = tf.stack([col1, col2, col3, ..., col5007])
    label = col5008
    return example, label

def input_pipeline(filenames, batch_size, num_epochs=None):
    filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs, shuffle=True)
    example, label = read_my_file_format(filename_queue)
    min_after_dequeue = 10000
    capacity = min_after_dequeue + 3 * batch_size
    example_batch, label_batch = tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue)
    return example_batch, label_batch

这部分正在工作,当执行类似的事情时:

with tf.Session() as sess:
    ex_b, l_b = input_pipeline(["Tensorflow_vectors.csv"], 10, 1)
    print("Test: ",ex_b)

我的结果是Test: Tensor("shuffle_batch:0", shape=(10, 5007), dtype=float32)

到目前为止,这对我来说似乎很好。接下来,我创建了一个包含两个隐藏层(分别为 512 和 256 个节点)的简单模型。当我尝试训练模型时出现问题:

batch_x, batch_y = input_pipeline(["Tensorflow_vectors.csv"], batch_size)
_, cost = sess.run([optimizer, cost], feed_dict={x: batch_x.eval(), y: batch_y.eval()})

我的这种方法基于this example that uses the MNIST database。 但是,当我执行此操作时,即使我只是使用batch_size = 1,Tensorflow 也会挂起。如果我省略了应该从张量获取实际数据的 .eval() 函数,我会得到以下响应:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

现在我可以理解了,但是我不明白为什么当我包含 .eval() 函数时程序会挂起,而且我不知道在哪里可以找到有关此问题的任何信息。

编辑:我包含了我整个脚本的最新版本here。即使我实施了(据我所知)vijay m

提供的解决方案,程序仍然挂起

【问题讨论】:

  • 能否请您添加整个代码?
  • 完整代码可以在这里找到:link

标签: tensorflow tensorflow-gpu tensor


【解决方案1】:

正如错误所说,您正在尝试将张量提供给feed_dict。您已经定义了一个input_pipeline 队列,但您不能将其作为feed_dict 传递。将数据传递给模型和训练的正确方法如下面的代码所示:

 # A queue which will return batches of inputs 
 batch_x, batch_y = input_pipeline(["Tensorflow_vectors.csv"], batch_size)

 # Feed it to your neural network model: 
 # Every time this is called, it will pull data from the queue.
 logits = neural_network(batch_x, batch_y, ...)

 # Define cost and optimizer
 cost = ...
 optimizer = ...

 # Evaluate the graph on a session:
 with tf.Session() as sess:
    init_op = ...
    sess.run(init_op)

    # Start the queues
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    # Loop through data and train
    for ( loop through steps ):
        _, cost = sess.run([optimizer, cost])

    coord.request_stop()
    coord.join(threads) 

【讨论】:

  • 非常感谢您的帮助!由于我的向量中的尺寸不相等而进行了额外的必要更改(我通过使用重塑函数batch_y = tf.reshape(batch_y, [12, 1]) 解决了这个问题),我仍然不知所措,因为程序再次挂起。如果您愿意看一下,这是我的整个代码的链接:link。我认为这也可能对刚进入 Tensorflow 的其他人有所帮助,因为有时很难确定程序为何挂起。
  • 注意:准确地说,它挂在 118 行之后。顺便说一下,在 980Ti 上运行,所以我希望硬件不会成为这个问题的原因。
  • 您也可以分享输入csv 吗?
  • 当然可以。笔记;这是一个直接下载链接:link 顺便说一句,显然它确实 工作,但模型不起作用。至少在处理第 10 批之后,它报告的成本为 0.00000000,这对我来说似乎根本不合适。
  • 您的模型有问题,它假定您的标签是 0-1。我们不应该在这个线程中讨论它。为这个问题创建一个不同的线程。如果上面的答案已经解决了您的原始问题,请考虑通过复选标记来接受它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 2017-01-29
  • 2018-02-27
相关资源
最近更新 更多