【发布时间】:2018-06-04 12:54:29
【问题描述】:
我正在使用带有 Tensorflow 后端的 Keras。一旦我有了模型,我就会使用 model.predict_proba 来预测概率。这个预测函数所花费的时间似乎与数组的大小无关,或者更不依赖于它所预测的数组的大小。
我可以做些什么来减少这个时间?
首先我运行循环 100 次,并按如下方式对预测进行计时
fname_model = folder+"/my_modelbuy_rocbased.h5"
best_model = load_model(fname_model)
best_model.compile(loss='categorical_crossentropy', optimizer='Adam',
metrics=['acc'])
X_test_orig = pd.read_csv(folder+"/ret_out_20170402.csv",sep=",")
x_test = X_test_orig.as_matrix()
y1=np.array([x_test[1]])
print y1.shape
t1=datetime.now().strftime("%H:%M:%S.%f")
for i in range(0,100,1):
prob1= best_model.predict_proba(y1)
t2=datetime.now().strftime("%H:%M:%S.%f")
print "time taken ", t1,t2
************************************************之后运行中***********************
耗时 11:50:53.909226 11:50:54.372596
因此,对于 100 个预测,所用时间为 11:50:54.372596 减去 11:50:53.909226,大约为 463 毫秒,这意味着每个预测需要 4.63 毫秒
但是,如果我只为一个预测运行循环,如下所示,
fname_model = folder+"/my_modelbuy_rocbased.h5"
best_model = load_model(fname_model)
best_model.compile(loss='categorical_crossentropy', optimizer='Adam',
metrics=['acc'])
X_test_orig = pd.read_csv(folder+"/ret_out_20170402.csv",sep=",")
x_test = X_test_orig.as_matrix()
y1=np.array([x_test[1]])
print y1.shape
t1=datetime.now().strftime("%H:%M:%S.%f")
for i in range(0,1,1):
prob1= best_model.predict_proba(y1)
t2=datetime.now().strftime("%H:%M:%S.%f")
print "time taken ", t1,t2
************************************************之后运行中***********************
耗时 11:54:40.683225 11:54:41.144969
因此,对于 1 次预测,所用时间为 11:54:40.683225 减去 11:54:41.144969,也就是大约 440 毫秒。
无论调用次数如何,模型所需的时间似乎都是固定的。我怎样才能减少这种情况?
【问题讨论】:
-
我发现第一个预测总是在我的 ANN 上花费更长的时间,而其他预测几乎是即时的。尝试进行一次预测,然后再进行两次。
标签: python keras prediction