Performance Tensorflow 1与Tensorflow 2的Keras推断速度相差2+;时代
我试图找出两种不同型号的速度不同的原因Performance Tensorflow 1与Tensorflow 2的Keras推断速度相差2+;时代,performance,tensorflow,keras,Performance,Tensorflow,Keras,我试图找出两种不同型号的速度不同的原因 使用tensorflow 1.x构建的LSTM RNN模型: 使用tensorflow 2.0 keras构建的相同模型(至少从我的理解来看): 架构是: Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param #
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 4, 100) 55400
_________________________________________________________________
lstm (LSTM) (None, 100) 80400
_________________________________________________________________
dense (Dense) (None, 554) 55954
=================================================================
Total params: 191,754
Trainable params: 136,354
Non-trainable params: 55,400
_________________________________________________________________
我期待类似的推理运行时,但使用tensorflow 1.x构建的运行时要快得多。我曾试图仅使用本机tensorlow函数将tensorflow 1.x模型转换为tensorflow 2,但由于tensorflow从1.x到2有很大的变化,我在转换时遇到了困难,我只能使用tf.keras创建它
就速度而言,因为我使用这两种方法来生成文本序列+获取单词概率,所以我没有一个推断时间差(我无法修改tensorflow 1.x模型中的现有API来获得这个结果)。但总的来说,我发现我的用例在时间上至少有2倍的差异
推理速度差异背后可能的原因是什么?如果需要,我很乐意提供更多信息。我们可以看看您的代码吗?同时,您是否使用了
tf.function
?@JoelB我将更新代码tmr。我已经搜索了tf.function,但是没有,我还没有尝试过,我不确定如何将它应用到keras模型。@JoelB我已经更新了代码。我没有构建tensorflow 1.x模型,所以我在使用tensorflow 2.x转换tensorflow图时遇到了问题,特别是由于变量范围的问题。
def setup_model():
model = Sequential()
model.add(Embedding(input_dim=vocab_size,
output_dim=embedding_dim,
weights=[embedding_matrix],
input_length=4,
trainable=False))
model.add(LSTM(config.hidden_size, activation="tanh"))
model.add(Dense(vocab_size, activation="softmax"))
return model
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 4, 100) 55400
_________________________________________________________________
lstm (LSTM) (None, 100) 80400
_________________________________________________________________
dense (Dense) (None, 554) 55954
=================================================================
Total params: 191,754
Trainable params: 136,354
Non-trainable params: 55,400
_________________________________________________________________