【问题标题】:What such notation means Dense()() brackets and brackets in python?这样的符号是什么意思密集()()括号和python中的括号?
【发布时间】:2019-06-10 03:52:17
【问题描述】:

我在密集类的 keras 文档中发现了这样的符号: 预测=密集(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

【问题讨论】:

    标签: python keras


    【解决方案1】:

    x()() 表示调用x() 返回一个可立即调用的可调用对象(如函数或类构造函数)。把它想象成:

    fnc = x()
    result = fnc()
    

    一个简单的例子:

    def foo():
        def bar():
            return 'baz'
        return bar
    
    >>> foo()()
    'baz'
    

    【讨论】:

      【解决方案2】:

      Dense(...) 创建Dense 类的实例。这个实例有一个__call__ 运算符,用圆括号拼写。

      【讨论】:

        猜你喜欢
        • 2019-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        相关资源
        最近更新 更多