【发布时间】:2019-03-16 11:38:12
【问题描述】:
使用顺序 API
如果我使用 Keras 的 Sequential API 创建一个 LSTM,代码如下:
from keras.models import Sequential
from keras.layers import LSTM
model = Sequential()
model.add(LSTM(2, input_dim=3))
然后
model.summary()
返回48个参数,如this Stack Overflow question所示即可。
快速详情:
input_dim = 3, output_dim = 2
n_params = 4 * output_dim * (output_dim + input_dim + 1) = 4 * 2 * (2 + 3 + 1) = 48
使用函数式 API
但如果我使用以下代码对功能 API 执行相同操作:
from keras.models import Model
from keras.layers import Input
from keras.layers import LSTM
inputs = Input(shape=(3, 1))
lstm = LSTM(2)(inputs)
model = Model(input=inputs, output=lstm)
然后
model.summary()
返回 32 个参数。
为什么会有这样的差异?
【问题讨论】:
标签: python machine-learning keras lstm recurrent-neural-network