Python Tensorflow图的运行速度在每次迭代中不断降低

Python Tensorflow图的运行速度在每次迭代中不断降低,python,tensorflow,Python,Tensorflow,我有一个非常简单的tensorflow设置,但它的一个方面是计算运行所需时间的精度不断提高。我不明白为什么会这样。我在保留错误的同时,尽可能简化了代码。这是密码 import time import tensorflow as tf import numpy as np # dummy data data = np.zeros((12, 784)) labels = np.zeros((12, 10)) xs = tf.placeholder(tf.float32, [12, 784]) y

我有一个非常简单的tensorflow设置,但它的一个方面是计算运行所需时间的精度不断提高。我不明白为什么会这样。我在保留错误的同时,尽可能简化了代码。这是密码

import time
import tensorflow as tf
import numpy as np

# dummy data
data = np.zeros((12, 784))
labels = np.zeros((12, 10))

xs = tf.placeholder(tf.float32, [12, 784])
ys = tf.placeholder(tf.float32, [12, 10])

weights = tf.Variable(tf.truncated_normal([784, 10], stddev=0.1))

prediction = tf.nn.softmax(tf.matmul(xs, weights))

sess = tf.Session()

init = tf.global_variables_initializer()
sess.run(init)

while True:

    y_pre = sess.run(prediction, feed_dict={xs: data})
    correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    start_time = time.time()

    r = sess.run(accuracy, feed_dict={xs: data, ys: labels})

    time_taken = time.time() - start_time

    #why does time_taken keep growing?
    print("time_taken", time_taken)

我怀疑我在while-True循环中做错了什么。根据我的经验,时间从0.01左右的低点开始,但如果你保持足够长的时间,它似乎会无限期地增长到0.30甚至更高。有没有办法保持所用的时间不变?任何帮助都将不胜感激。

您能在执行过程中查看一下您的RAM吗?

在运行时,RAM的使用率似乎增加了大约3 MB/s。Hm。这很奇怪。如果我是对的,它应该保持不变,因为您没有创建新数据。几分钟后它停止上升了吗?没有,内存使用量一直在增长。内存增长的速度确实变慢了,但我认为这是因为程序本身变慢了。我让它开了一段时间,开始时是0.00399,结束时是3.6614,程序的内存使用量增加了三倍。你能检查一下你的全局文件,看看垃圾收集器是否真的在工作吗?