Evg*_*pov 2 python keras tensorflow
当我steps_per_epoch在model.fit(..)方法中指定参数时,我注意到训练模型的速度会大大降低 。当我指定steps_per_epoch为None(或不使用它)时,纪元的ETA是连续2秒:
9120/60000 [===> ......................................]-ETA:2秒-损失: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)
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用纯Keras,但问题仍然存在。我使用1.12.0Tensorflow,python 3和Ubuntu 18.04.1 LTS版本。
为什么steps_per_epoch争论会导致如此严重的速度下降,我该如何避免呢?
谢谢!
注意,您正在使用fit数据数组。您没有使用fit_generator或使用任何生成器。
steps_per_epoch除非您有非常规的想法,否则使用毫无意义。
的默认批次大小fit为32,这意味着您正在60000 // 32 = 1875按每个时期进行步数训练。
如果您使用的是1875,则将训练与默认数量相同的批次None。如果使用60000步骤,则将一个纪元乘以32。(由于速度的巨大差异,在这种情况下,默认批处理大小也会更改)
输出中显示的无级数拟合总数是图像总数。请注意,已完成项目的数量如何以32的倍数增长。
使用步骤时显示的总数是步骤数。请注意,已完成的步骤数如何由1增长1。