且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每次重复后,keras模型的训练变慢

更新时间:2023-12-01 22:49:22

为什么每次跑步后训练时间都会增加?

简短的回答:您需要在创建的每个新模型之前使用tf.keras.backend.clear_session().

仅当急切的执行关闭时,才会出现此问题.

好的,让我们在有和没有clear_session的情况下进行一次实验. make_model的代码在此响应的结尾.

Okay, so let's run an experiment with and without clear_session. The code for make_model is at the end of this response.

首先,让我们看一下使用清晰会话时的培训时间.我们将运行此实验10次,打印结果

First, let's look at the training time when using clear session. We'll run this experiment 10 times an print the results

non_seq_time = [ make_model(clear_session=True) for _ in range(10)]

with clear_session = True

non sequential
Elapse =  1.06039
Elapse =  1.20795
Elapse =  1.04357
Elapse =  1.03374
Elapse =  1.02445
Elapse =  1.00673
Elapse =  1.01712
Elapse =    1.021
Elapse =  1.17026
Elapse =  1.04961

如您所见,训练时间保持恒定

As you can see, the training time stays about constant

现在让我们在不使用清晰会话的情况下重新运行实验并查看培训时间

Now let's re-run the experiment without using clear session and review the training time

non_seq_time = [ make_model(clear_session=False) for _ in range(10)]

with clear_session = False

non sequential
Elapse =  1.10954
Elapse =  1.13042
Elapse =  1.12863
Elapse =   1.1772
Elapse =   1.2013
Elapse =  1.31054
Elapse =  1.27734
Elapse =  1.32465
Elapse =  1.32387
Elapse =  1.33252

如您所见,没有clear_session,训练时间就会增加

as you can see, the training time increases without clear_session

# Training time increases - and how to fix it

# Setup and imports

# %tensorflow_version 2.x

import tensorflow as tf
import tensorflow.keras.layers as layers
import tensorflow.keras.models as models
from time import time

# if you comment this out, the problem doesn't happen
# it only happens when eager execution is disabled !!
tf.compat.v1.disable_eager_execution()


(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()


# Let's build that network
def make_model(activation="relu", hidden=2, units=100, clear_session=False):
    # -----------------------------------
    # .     HERE WE CAN TOGGLE CLEAR SESSION
    # -----------------------------------
    if clear_session:
        tf.keras.backend.clear_session()

    start = time()
    inputs = layers.Input(shape=[784])
    x = inputs

    for num in range(hidden) :
        x = layers.Dense(units=units, activation=activation)(x)

    outputs = layers.Dense(units=10, activation="softmax")(x)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

    results = model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=200, verbose=0)
    elapse = time()-start
    print(f"Elapse = {elapse:8.6}")
    return elapse

# Let's try it out and time it

# prime it first
make_model()

print("Use clear session")
non_seq_time = [ make_model(clear_session=True) for _ in range(10)]

print("Don't use clear session")
non_seq_time = [ make_model(clear_session=False) for _ in range(10)]