【发布时间】:2019-07-05 04:51:56
【问题描述】:
当我在 model.fit(..) 方法中指定 steps_per_epoch 参数时,我注意到训练模型速度大幅下降。当我将 steps_per_epoch 指定为 None(或不使用它)时,epoch 的 ETA 是连续 2 秒:
9120/60000 [===>.......................] - ETA:2s - 损失:0.7055 - acc:0.7535
当我添加 steps_per_epoch 参数时,ETA 会增加 5 小时,训练速度变得非常慢:
5/60000 [.......................] - 预计到达时间:5:50:00 - 损失:1.9749 - 累积:0.3437
这是可重现的脚本:
import tensorflow as tf
from tensorflow import keras
import time
print(tf.__version__)
def get_model():
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
(train_images, train_labels), (test_images, test_labels) = keras.datasets.fashion_mnist.load_data()
train_images = train_images / 255.0
model = get_model()
# Very quick - 2 seconds
start = time.time()
model.fit(train_images, train_labels, epochs=1)
end = time.time()
print("{} seconds", end - start)
model = get_model()
# Very slow - 5 hours
start = time.time()
model.fit(train_images, train_labels, epochs=1, steps_per_epoch=len(train_images))
end = time.time()
print("{} seconds", end - start)
我也尝试过使用纯 Keras,但问题仍然存在。我使用 1.12.0 版本的 Tensorflow、python 3 和 Ubuntu 18.04.1 LTS。
为什么steps_per_epoch 参数会导致如此显着的速度下降,我该如何避免这种情况?
谢谢!
【问题讨论】:
-
这会很慢,但是 mnist 数据集中的 5 张图像需要 5 小时?看起来太多了。 (如果steps_per_epoch为60000,批量大小为1张)。
-
尝试使用
steps_per_epoch(有大步和小步)运行示例代码,在这两种情况下都无法在我的GPU上分配内存。但是使用batch_size(100) 运行正常。 -
貌似
steps_per_epoch切换到None,是切换batch_size的默认值(32)。猜猜你可以在另一个 [SO 主题:fit_generator 中的 Keras steps_per_epoch 如何工作](stackoverflow.com/questions/46820609) 中找到答案 -
@Butuzov 我也尝试在第一个示例中将 batch_size 设置为 1,它的工作速度相对较快 - 只需一分钟的训练。
标签: python tensorflow keras