python-3.x - 如何在不指定输入(或输入形状)的情况下在 keras 2 功能 API 中链接/组合层

标签 python-3.x keras keras-layer keras-2

我希望能够将几个层放在一起,但在指定输入之前,如下所示:

# conv is just a layer, no application
conv = Conv2D(64, (3,3), activation='relu', padding='same', name='conv')
# this doesn't work:
bn = BatchNormalization()(conv)

请注意,如果可以避免,我不想指定输入或其形状,我想稍后将其用作多个输入的共享层。

有没有办法做到这一点?以上给出了以下错误:
>>> conv = Conv2D(64, (3,3), activation='relu', padding='same', name='conv')
>>> bn = BatchNormalization()(conv)
Traceback (most recent call last):
  File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 419, in assert_input_compatibility
    K.is_keras_tensor(x)
  File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 393, in is_keras_tensor
    raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
ValueError: Unexpectedly found an instance of type `<class 'keras.layers.convolutional.Conv2D'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 552, in __call__
    self.assert_input_compatibility(inputs)
  File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 425, in assert_input_compatibility
    str(inputs) + '. All inputs to the layer '
ValueError: Layer batch_normalization_4 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.convolutional.Conv2D'>. Full input: [<keras.layers.convolutional.Conv2D object at 0x7f3f6e54b748>]. All inputs to the layer should be tensors.

获取 conv 层的输出也不起作用:
>>> bn = BatchNormalization()(conv.output)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 941, in output
    ' has no inbound nodes.')
AttributeError: Layer conv has no inbound nodes.

最佳答案

尝试这个:

def create_shared_layers():
    layers = [
        Conv2D(64, (3,3), activation='relu', padding='same', name='conv'),
        BatchNormalization()
    ]
    def shared_layers(x):
        for layer in layers:
            x = layer(x)
        return x
    return shared_layers

稍后,您可以执行以下操作:
shared_layers = create_shared_layers()
...
h1 = shared_layers(x1)
h2 = shared_layers(x2)

关于python-3.x - 如何在不指定输入(或输入形状)的情况下在 keras 2 功能 API 中链接/组合层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45439492/

相关文章:

python - while 异步中的 True 循环会阻止代码的其他部分工作 Discord.py

python - 任何 keras 层中的 dropout 层和 dropout 参数有什么区别

python - 如何连接两个具有特殊条件的表

javascript - 将保存的 .h5 文件转换为 JSON 文件

python - Tensorflow js : Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), 但得到形状为 [100,120,3] 的数组

python - 为什么在准确率保持不变的情况下损失会减少?

keras - 带嵌入层的状态 LSTM(形状不匹配)

python - 使用具有附加属性的自定义层保存和加载 keras 模型

未找到 Python 模块,但已安装且在搜索路径中

python - 在不丢失数据的情况下更新字典