首页 文章

TensorFlow - 如何在不将其添加到图形的情况下执行操作?

提问于
浏览
0

我无法找到一个令人满意的答案:如何在不膨胀图表的情况下执行操作?

具体来说,我想将一些输出张量显示为图像 . 这需要调用session.run()来转换numpy数组中的张量 . 但是session.run()操作作为Op添加到图形中,最终Graph变得膨胀,产生:

"ValueError: GraphDef cannot be larger than 2GB"

相关代码如下:

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不允许我们从图中删除节点 . 我想过调用一个新的tf.Session(),但这样做会导致:

'ValueError: Tensor Tensor("Placeholder:0", shape=(1, 1), dtype=int32) is not an element of this graph.'

1 回答

  • 1

    这正是占位符的用途 . 您可以为图像创建占位符,然后每次要打印最佳预测时,都会将图像输入到占位符并计算要计算的内容 . 这样它只会添加一次图表 .

相关问题