解释keras代码片段
Explain keras code snippet
我有这段代码:
from keras.models import Model
from keras.layers import Input, Dense
a = Input(shape=(32,))
b = Dense(32)(a)
in 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)
)。这提供了一种非常方便的共享权重的方式。
我有这段代码:
from keras.models import Model
from keras.layers import Input, Dense
a = Input(shape=(32,))
b = Dense(32)(a)
in 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)
)。这提供了一种非常方便的共享权重的方式。