我试图估计我的keras模型的预测时间,并意识到一些奇怪的东西。除了通常相当快之外,每隔一段时间,模型需要相当长的时间才能得出预测。不仅如此,这些时间也随着模型运行时间的延长而增加。我添加了一个最小的工作示例来重现错误。
import time
import numpy as np
from sklearn.datasets import make_classification
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Make a dummy classification problem
X, y = make_classification()
# Make a dummy model
model = Sequential()
model.add(Dense(10, activation='relu',name='input',input_shape=(X.shape[1],)))
model.add(Dense(2, activation='softmax',name='predictions'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, verbose=0, batch_size=20, epochs=100)
for i in range(1000):
# Pick a random sample
sample = np.expand_dims(X[np.random.randint(99), :], axis=0)
# Record the prediction time 10x and then take the average
start = time.time()
for j in range(10):
y_pred = model.predict_classes(sample)
end = time.time()
print('%d, %0.7f' % (i, (end-start)/10))
时间不取决于样本(它是随机挑选的)。如果重复测试,则预测需要较长时间的 for 循环中的索引将(几乎)再次相同。
我正在使用:
tensorflow 2.0.0 python 3.7.4
对于我的应用程序,我需要保证在一定时间内执行。然而,考虑到这种行为,这是不可能的。哪里出错了?是 Keras 中的 bug 还是 tensorflow 后端中的 bug?
12345678_0001
潇湘沐
相关分类