【问题标题】:Training this model leads to memory leakage训练这个模型会导致内存泄漏
【发布时间】:2019-11-09 15:46:04
【问题描述】:

我一直在训练模型,并且使用 htop 我看到内存随着每次迭代而不断增加。环顾四周,大多数人说图形必须继续增长,因为我每次迭代都加载一个新模型,或者因为我添加了新的操作,但我没有做上述任何事情。 这是最小的可重现示例。

from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import numpy as np

#%% Params
OBSERVATION_SPACE_VALUES = 4
ACTION_SPACE_SIZE = 2
LEARNING_RATE = 0.00025/4
DENSE_PARAMS = [256]

class Network():
    def __init__(self, state_size=OBSERVATION_SPACE_VALUES, action_size=ACTION_SPACE_SIZE, learning_rate=LEARNING_RATE,    
                 dense_params=DENSE_PARAMS):

        self.state_size = state_size
        self.action_size = action_size
        self.learning_rate= learning_rate        
        self.model = self.create_model(dense_params)

    def create_model(self, dense_params=[256]):

        model = Sequential()
        for params in dense_params:
            units = params
            model.add(Dense(units, activation='relu',input_shape=[self.state_size]))

        model.add(Dense(self.action_size, activation="linear"))
        model.compile(loss="mse", optimizer=Adam(lr=self.learning_rate))
        return model

Agent = Network()

for i in range(10_000):
    state = np.random.rand(Agent.state_size)
    state = np.expand_dims(state, axis=0)
    output = np.random.rand(Agent.action_size)
    output = np.expand_dims(output, axis=0)
    Agent.model.fit(state,output,verbose=True)

还有:

tf.__version__
2.0.0
tf.keras.__version__
2.2.4-tf

【问题讨论】:

  • range(10_000)??

标签: python tensorflow keras


【解决方案1】:

问题是使用多个.fit 调用。要解决此问题,您可以:

  • 为您的数据创建一个 data_generator 并调用 .fit(epochs=10000)

  • 保留 for 循环,但改为调用 `train_on_batch (doc)

【讨论】:

  • .fit 通话与问题无关
  • 嗨拉斐尔。谢谢回复。事情是在我感兴趣的实际脚本中,我需要适合循环,因为我做了很多其他收集数据的事情(它是一种 RL 算法),所以我不能只使用 epoch 而不是循环。我刚刚发现这似乎是特定版本的 tf.keras 中的问题,并且预测也会发生同样的事情。可能的解决方案是将 predict 和 fit 替换为 predict on_batch 和 train_on_batch,尽管它们并不理想,所以我将问题留待解决
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
相关资源
最近更新 更多