【问题标题】:Tensorflow reading data from tfrecords into mini batches properlyTensorflow 将数据从 tfrecord 正确读取到小批量中
【发布时间】:2017-01-25 13:40:18
【问题描述】:

我正在尝试将数据从 csv 转换为 tfrecords,然后以小批量读取它并执行简单的 MLP,但我遇到了一些我无法弄清楚的错误。

RuntimeError: 尝试使用已关闭的会话。

接着是:

TypeError:提要的值不能是 tf.Tensor 对象。可接受的提要值包括 Python 标量、字符串、列表或 numpy ndarray。

我猜 shuffle 批处理队列正在关闭并且不再提供预期的数据。另外,我认为我错过了从洗牌队列到提要字典的步骤。任何想法如何使这项工作或更好的方式做到这一点?

这是我的代码:

import numpy as np
import tensorflow as tf
import pandas

filename = "test.tfrecords"
writer = tf.python_io.TFRecordWriter(filename)

csv = pandas.read_csv("TRAINING.csv").values
with tf.python_io.TFRecordWriter(filename) as writer:
    for row in csv:
        features, label = row[:-1], row[-1]
        example = tf.train.Example()
        example.features.feature["avgs"].float_list.value.extend(features)
        example.features.feature["pdiff"].float_list.value.append(label)
        writer.write(example.SerializeToString())

def read_and_decode_single_example(filename):
    filename_queue = tf.train.string_input_producer([filename],
                                                   num_epochs=None)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)

    features = tf.parse_single_example(
        serialized_example,
        features={
            'pdiff': tf.FixedLenFeature([], np.float32),
            'avgs': tf.FixedLenFeature([14], np.float32)})

    pdiff = features['pdiff']
    avgs = features['avgs']

    return avgs, pdiff

avgs, pdiff = read_and_decode_single_example(filename)

avgs_batch, pdiff_batch = tf.train.shuffle_batch(
    [avgs, pdiff], batch_size=200,
    capacity=2000,
    min_after_dequeue=600)


n_features = 14
batch_size = 50
hidden_units = 7
lr = .03

X = tf.placeholder(tf.float32,[None,n_features])
Y = tf.placeholder(tf.float32,[None])

W = tf.Variable(tf.truncated_normal([n_features,hidden_units]))
Wout = tf.Variable(tf.truncated_normal([hidden_units,1]))

b = tf.Variable(tf.zeros([hidden_units]))
bout = tf.Variable(tf.zeros([1]))


hidden1 = tf.nn.sigmoid(tf.matmul(X,W) + b)
pred = tf.matmul(hidden1,Wout) + bout

loss = tf.reduce_sum(tf.pow(pred - Y,2))
optimizer = tf.train.AdamOptimizer(lr).minimize(loss)


 with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    tf.train.start_queue_runners(sess=sess)

    for step in range(1000):
        _, loss_val = sess.run([optimizer,loss],
                feed_dict={X: avgs_batch, Y: pdiff_batch} )

堆栈跟踪:

ERROR:tensorflow:Exception in QueueRunner: Attempted to use a closed Session.
Traceback (most recent call last):
ERROR:tensorflow:Exception in QueueRunner: Attempted to use a closed Session.
  File "tf_tb.py", line 110, in <module>
    feed_dict={X: avgs_batch, Y: pdiff_batch} )
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 924, in _run
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/training/queue_runner_impl.py", line 234, in _run
    sess.run(enqueue_op)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 902, in _run
    raise RuntimeError('Attempted to use a closed Session.')
RuntimeError: Attempted to use a closed Session.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/training/queue_runner_impl.py", line 234, in _run
    sess.run(enqueue_op)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/Users/mnaymik/.envs/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 902, in _run
    raise RuntimeError('Attempted to use a closed Session.')
RuntimeError: Attempted to use a closed Session.

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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    占位符是将数据导入模型的一种方式。队列是另一个。您可以像任何其他张量(例如占位符)一样覆盖队列运行器生成的张量中的值,但您不能将来自张量的结果提供给同一图形/会话运行中的占位符。

    换句话说,不要创建占位符,只需使用tf.train.shuffle_batch 调用的输出:

    X = avgs_batch
    Y = pdiff_batch
    

    (或将所有对XY 的引用分别替换为avgs_batchpdiff_batch

    【讨论】:

    • 对不起,我应该包含我的模型,我更新了我的代码。 X 和 Y 是占位符。
    • 我知道 feed dict 不接受张量,但是如何将 shuffle_batch 张量注入图中?
    • 啊,我明白了。那么我可以忘记 feed_dict,只需将 sess.run 行更改为:_, loss_val = sess.run([optimizer,loss])?这似乎现在正在运行,但是如果我在循环结束时打印损失,为什么它会显示“Tensor("Sum:0", shape=(), dtype=float32)"?这不应该是我的误差平方和,一个常数吗?
    • 抱歉,还有一个问题...以前当 X 是占位符时,我可以通过 feed dict 向 X 注入测试数据来获得预测。我现在如何通过我的模型运行测试数据?
    • 完全一样。您仍然可以像占位符一样输入非占位符,并且输入的值将覆盖通常存在的值。
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    相关资源
    最近更新 更多