Reputation: 85
I have not been able to find a satisfactory answer to this: How do I perform operations without bloating the graph?
Specifically I want to display some of my output tensors as images. This requires calling session.run() to convert the tensors in numpy arrays. However the session.run() operation gets added to the graph as an Op, and eventually the Graph gets bloated, producing:
"ValueError: GraphDef cannot be larger than 2GB"
The relevant code is as follows:
def print_best_prediction(session, predictions, labels, best_prediction):
result = session.run(predictions[best_prediction]/tf.reduce_max(labels[best_prediction]))
plt.imshow(result, cmap='gray')
plt.show()
def train(data_set):
...define model, placeholders, optimizer_step...
with tf.device(hp.device):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hp.num_epochs):
train_images, train_labels = data_set.get_next_images()
feed_dict = {x: train_images, y: train_labels, is_training: 1}
loss_value, _, train_predictions = sess.run([loss, optimizer_step, output], feed_dict=feed_dict)
best_pair = check_accuracy(train_output, train_labels)
print_best_prediction(sess, train_output, train_labels, best_prediction)
Tensorflow does not allow us to remove nodes from the graph. I thought about calling a new tf.Session() but doing this results in:
'ValueError: Tensor Tensor("Placeholder:0", shape=(1, 1), dtype=int32) is not an element of this graph.'
Upvotes: 0
Views: 410
Reputation: 2374
This is exactly what placeholders are for. You can make a placeholder for the image and then every time you want to print the best prediction then you feed in the image to that placeholder and calculate whatever it is that you want to calculate. That way it only gets added the graph once.
Upvotes: 1