我在 tensorflow 上有一个非常奇怪的问题。我将我的问题简化为以下版本?
我只是用tensorflow语言写了一个简单的矩阵乘法,然后我把这个矩阵乘法放在一个“for循环”中(当然你可以把其他复杂的函数放在for循环中,结论是一样的)。
我设置了 10000 次迭代?并打印每个循环中消耗的时间,然后我可以观察到时间的消耗逐渐增加。?我希望每个循环的时间应该相同?但事实并非如此。)
import tensorflow as tf
import numpy as np
import datetime
graph=tf.Graph()
with graph.as_default():
with tf.device("/gpu:0"):
a=np.arange(10).reshape(1,-1)
b=np.arange(100).reshape(10,10)
A = tf.placeholder(tf.float32, [1,10])
B = tf.placeholder(tf.float32, [10,10])
sess = tf.InteractiveSession()
for step in range(10000):
starttime = datetime.datetime.now()
RESULT = tf.matmul(A,B)
RESULT=sess.run(RESULT,feed_dict={A: a, B: b})
endtime = datetime.datetime.now()
print(endtime-starttime)
Run Code Online (Sandbox Code Playgroud)
开始时,程序打印以下结果:
0:00:00.003058
0:00:00.003216
0:00:00.003195
0:00:00.003213
0:00:00.003653
0:00:00.003599
0:00:00.003297
0:00:00.003172
0:00:00.003235
0:00:00.004374
0:00:00.003442
0:00:00.003387
0:00:00.003290
Run Code Online (Sandbox Code Playgroud)
几秒钟后,我得到了这个:
0:00:00.011470
0:00:00.013232
0:00:00.013088
0:00:00.015906
0:00:00.012659
0:00:00.012914
0:00:00.012562
0:00:00.011941
0:00:00.013575
0:00:00.012251
0:00:00.013759
0:00:00.012534 …
Run Code Online (Sandbox Code Playgroud) 我对张量流有一个非常奇怪的问题.我将我的问题简化为以下版本:
我问这个是因为我需要进行一系列训练,我只是把它们放在for循环中,然后我为每次迭代使用不同的参数.
为了简化问题,我只用张力流语言编写一个简单的矩阵乘法,然后我把这个"矩阵乘法训练"放在"for循环"中(当然你可以把其他复杂的函数放在for循环中,结论是一样的) .
我设置了100000次迭代次数,这意味着我将运行10000次训练示例.并且在每个循环中打印耗时,然后我可以观察到每次迭代的时间消耗是相同的,这是没有问题的.但是内存成本增加得非常快,最后我得到了错误:"内存耗尽"(我期望内存应该在每次迭代时保持相同)
import tensorflow as tf
import numpy as np
import datetime
for i in range(100000): # I must put the following code in this for loop
starttime = datetime.datetime.now()
graph=tf.Graph()
with graph.as_default():
with tf.device("/cpu:0"):
a=np.arange(100).reshape(1,-1)
b=np.arange(10000).reshape(100,100)
A = tf.placeholder(tf.float32, [1,100])
B = tf.placeholder(tf.float32, [100,100])
sess = tf.InteractiveSession()
RESULT =tf.matmul(A,B)
RESULT_o=sess.run(RESULT,feed_dict={A: a, B: b})
endtime = datetime.datetime.now()
print(endtime-starttime)
Run Code Online (Sandbox Code Playgroud)
I know the reason is that in each iteration, the program created a new operation,this will increase memory. I …