我在密集类的喀拉拉邦文档中发现了这种表示法: 预测=密集(10,激活='softmax')[x) 这样的符号()()是什么意思?
from keras.layers import Input, Dense
from keras.models import Model
# This returns a tensor
inputs = Input(shape=(784,))
# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(data, labels) # starts training
答案 0 :(得分:1)
x()()
表示调用x()
返回可立即调用的可调用对象(如函数或类构造函数)。认为是:
fnc = x()
result = fnc()
一个简单的例子:
def foo():
def bar():
return 'baz'
return bar
>>> foo()()
'baz'
答案 1 :(得分:1)
Dense(...)
创建Dense
类的实例。该实例具有一个__call__
运算符,该运算符带有圆括号。