python - sess.run 动态增加内存使用量

标签 python tensorflow memory

我尝试通过以下代码来训练我的模型。

sess.run([train_op, model.global_step, model.loss, model.prediction], feed_dict)

但是,当我运行“model.prediction”时,我发现内存使用量动态增加。

我从不在迭代过程中保留“sess.run()”的结果。

“模型.预测”是

@property
def prediction(self):
    return [tf.argmax(self.logits_b, 1),
            tf.argmax(self.logits_m, 1),
            tf.argmax(self.logits_s, 1),
            tf.argmax(self.logits_d, 1)]

我不知道为什么会这样。 请帮助我。

最佳答案

每次使用属性预测时,您都会在图中创建新的操作。您应该只预先创建一次操作并将它们返回到属性中:

def create_model(self):
    # Hypothetical function that creates the model, only called once
    # ...
    self._prediction = (tf.argmax(self.logits_b, 1),
                        tf.argmax(self.logits_m, 1),
                        tf.argmax(self.logits_s, 1),
                        tf.argmax(self.logits_d, 1))
    # ...

@property
def prediction(self):
    return self._prediction

关于python - sess.run 动态增加内存使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634675/

相关文章:

python - 这个CNN的降维似乎违背了我对理论的理解

python - keras层中的参数适用于哪里?

objective-c - 避免、发现和消除 Cocoa 中的内存泄漏

python - Keras 总是预测相同的输出

python - 如何在多线程或多处理中运行脚本

python - 在 Tensorflow 中,是否有操作/是否有操作来接受张量(文件名)并输出图像?

c - 自动局部变量是否存储在C中的堆栈中?

c - 调用免费电话前是否需要重置保护

python - 如何获取列表中所有元素的日志

python - 如何在 while 循环中使用索引每次查找下一个出现的位置