我有这段代码:
from keras.models import Model
from keras.layers import Input, Dense
a = Input(shape=(32,))
b = Dense(32)(a)
Dense(32)(a)
我知道我们正在创建keras.layers.Dense
对象,但是(a)
对我们创建的对象Dense(32)
部分是什么?
另外,python内部如何理解它?
b = Dense(32)(a)
部分创建一个Dense
层,该层接收张量a
作为输入。这样做是为了允许使用具有不同输入的相同密集层(即允许共享权重(。
例如,请考虑以下代码片段:
from keras.models import Model
from keras.layers import Input, Dense
a = Input(shape=(32,))
b = Input(shape=(32,))
dense = Dense(32)
c = dense(a)
d = dense(b)
在这里,dense = Dense(32)
实例化一个可调用的Dense
层。你可以把它想象成你正在创建一个函数,你可以调用不同的输入(即 c = dense(a)
和d = dense(b)
(。这提供了一种非常方便的权重共享方式。