Python 函数两括号()() ()(X)的语法含义

在写吴恩达《深度学习》课程编程题时,见到这样的用法:

def model(input_shape):
    # Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
    X_input = Input(input_shape)

    # Zero-Padding: pads the border of X_input with zeroes
    X = ZeroPadding2D((3, 3))(X_input)

    # CONV -> BN -> RELU Block applied to X
    X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)

    # MAXPOOL
    X = MaxPooling2D((2, 2), name='max_pool')(X)

    # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)

    # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
    model = Model(inputs = X_input, outputs = X, name='HappyModel')

    return model

X = Activation('relu')(X)  这种两个括号之前没遇到过。

其实是第一个函数Activation('relu')返回了一个函数,如果后面还有括号,说明要执行前面那个返回了的函数,如果里面有参数,说明返回的函数有参数需求,如Activation('relu')返回了一个...(type X)函数,并要执行它。多个括号以此类推以此类推。

看下面一个例子就知道了

def func(d):
    print("this is func");
    print(d);
    def func8(x):
        print(x);
    return func8;

func(20)(9)

输出是

猜你喜欢

转载自blog.csdn.net/u013166171/article/details/81292132