这是我的示例代码:
N = 3000
with tf.variable_scope("scope") as scope:
A = tf.Variable(np.random.randn(N,N), dtype=tf.float32, name='A')
sess = tf.Session()
for _ in range(100):
sess.run(tf.global_variables_initializer())
运行代码会在我的机器上分配> 10GB的内存。我想多次重新训练模型,而不必每次都将整个图重置为默认图。我想念什么?
谢谢!
我发现了问题。对于将来遇到相同问题的任何人:问题似乎是每次在循环中都会创建一个新的初始化操作。我的解决方案是重用初始化操作。这为我解决了内存“泄漏”:
N = 3000
tf.reset_default_graph()
with tf.variable_scope("scope") as scope:
A = tf.Variable(np.random.randn(N,N), dtype=tf.float32, name='A')
varlist = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="scope")
init = tf.variables_initializer(varlist) # or tf.global_variables_initializer()
for _ in range(100):
sess = tf.Session()
sess.run(init) # here we reuse the init operation
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句