我正在尝试将数据从csv转换为tfrecords,然后以迷你批次读取它并做一个简单的MLP但我得到一些我无法弄清楚的错误。
RuntimeError:尝试使用已关闭的会话。
其次是:
TypeError:Feed的值不能是tf.Tensor对象。可接受的Feed值包括Python标量,字符串,列表或numpy ndarrays。
我猜测洗牌批处理队列正在关闭,不再提供预期的数据。此外,我想我错过了从shuffle队列到feed dict的一个步骤。任何想法如何使这项工作或更好的方式吗?
这是我的代码:
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.
答案 0 :(得分:2)
占位符是将数据导入模型的一种方法。队列是另一个。您可以像任何其他张量(例如占位符)一样覆盖由队列运行器生成的张量中的值,但是您无法将张量中的结果提供到同一图形/会话运行中的占位符中。
换句话说,不要创建占位符,只需使用tf.train.shuffle_batch
调用的输出:
X = avgs_batch
Y = pdiff_batch
(或分别用X
和Y
替换对avgs_batch
和pdiff_batch
的所有引用