Keras:语法说明

Keras: Syntax clarification

keras 新手:

我正在尝试了解 keras 中使用的语法。 我难以理解的语法是在构建网络时。我在很多地方都看到过,如下面的代码所述。

语句如下:current_layer = SOME_CODE(current_layer)
这样的说法是什么意思?这是否意味着首先要遵循SOME_CODE中描述的计算,然后是当前层中描述的计算?

这种语法有什么用,应该在什么时候使用?有什么优势和替代方案吗?

input_layer = keras.layers.Input(
        (IMAGE_BORDER_LENGTH, IMAGE_BORDER_LENGTH, NB_CHANNELS))

current_layer = image_mirror_left_right(input_layer)

current_layer = keras.layers.convolutional.Conv2D(
      filters=16, "some values " ])
        )(current_layer)

def random_image_mirror_left_right(input_layer):
    return keras.layers.core.Lambda(function=lambda batch_imgs: tf.map_fn(
        lambda img: tf.image.random_flip_left_right(img), batch_imgs
    )
    )(input_layer)

如果你确实是 Keras 的新手,正如你所说,我强烈建议你在这个阶段不要为这些高级的东西烦恼。

您所指的 repo 是一个相当高级且非常重要的案例,它使用专门的库 (HyperOpt) 自动元优化 Keras 模型。它涉及 'automatic' 根据一些已存储在 Python 字典中的配置参数构建模型...

此外,您引用的函数超出了 Keras 范围,涉及 TensorFlow 方法和 lambda 函数...

current_layer=SOME_CODE(current_layer)是Keras的典型例子Functional API; according to my experience, it is less widely used than the more straightforward Sequential API,但在一些更高级的情况下可能会派上用场,例如:

The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. [...] With the functional API, it is easy to re-use trained models: you can treat any model as if it were a layer, by calling it on a tensor. Note that by calling a model you aren't just re-using the architecture of the model, you are also re-using its weights.