tensorflow 2.5中,采用keras,predict很慢,300倍减少predict运行时间的优化方法

版本如下

import tensorflow as tf
import numpy as np
import time

from tensorflow import keras

print(tf.__version__)

2.5.0

下面设置循环次数,构建模型。

loop_times = 100
input_data = np.zeros((2,8,8))
model  = keras.Sequential([
    keras.layers.Flatten(input_shape=(8,8)),
    keras.layers.Dense(10)
])
model.compile(optimizer='adam',loss='softmax')

测试代码1,需要3.59s

clock = time.time()

for i in range(loop_times):
    res = model.predict(x=input_data)

print(time.time() - clock,'s')
print(res)

输出:

3.599362850189209 s
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

优化代码1,需要0.8s

clock = time.time()

for i in range(loop_times):
    res = model.predict(x=tf.data.Dataset.from_tensors(input_data))
    
print(time.time() - clock,'s')
print(res)

输出:

0.8024599552154541 s
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

优化代码2,需要0.07s

这里注意输出为tensor

clock = time.time()
for i in range(loop_times):
    res_m = model(input_data,training = False)
    res = np.array(res_m)
    
print(time.time() - clock,'s')
print(res_m)
print(res)

输出:

0.07204914093017578 s
tf.Tensor(
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]], shape=(2, 10), dtype=float32)

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值