Reputation: 1293
I am trying to convert data from csv to tfrecords then read it out in mini batches and do a simple MLP but I am getting some errors that I can't figure out.
RuntimeError: Attempted to use a closed Session.
Followed by:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
I'm guessing the shuffle batch queue is closing and no longer feeding the expected data. Also, I think I am missing a step going from the shuffle queue to the feed dict. Any ideas how to make this work or a better way of doing it?
Here is my code:
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} )
Stack Trace:
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.
Upvotes: 1
Views: 2145
Reputation: 4183
Placeholders are one way of getting data into your model. Queue's are another. You can override the value in a tensor generated by a queue runner like any other tensor (e.g. placeholders), but you can't feed the results from a tensor into a placeholder in the same graph/session run.
In other words, rather than creating placeholders, just use the output of your tf.train.shuffle_batch
call:
X = avgs_batch
Y = pdiff_batch
(or replace all references to X
and Y
with avgs_batch
and pdiff_batch
respectively)
Upvotes: 2