gpt4 book ai didi

python - 具有不同输入大小的 Keras 共享层

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:02 25 4
gpt4 key购买 nike

根据 shared layers documentation在 Keras 中,可以创建共享层并使用不同的输入形状对其进行实例化。它给出了 Conv2D 共享层的示例,如下所示:

a = Input(shape=(32, 32, 3))
b = Input(shape=(64, 64, 3))

conv = Conv2D(16, (3, 3), padding='same')
conved_a = conv(a)

# Only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 3)

conved_b = conv(b)
# now the `.input_shape` property wouldn't work, but this does:
assert conv.get_input_shape_at(0) == (None, 32, 32, 3)
assert conv.get_input_shape_at(1) == (None, 64, 64, 3)

我正在对 Dense 层尝试同样的事情,但它似乎不起作用。这是我尝试过的方法,但由于输入形状不匹配,它似乎出错了。我错过了什么吗?

tf.keras.backend.clear_session()
dense = Dense(100)
i1 = Input(shape=(10,))
i2 = Input(shape=(200,))
d1 = dense(i1)
d2 = dense(i1)
d3 = dense(i2)

以下是堆栈跟踪:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-d3fc6212c6ef> in <module>()
5 d1 = dense(i1)
6 d2 = dense(i1)
----> 7 d3 = dense(i2)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
751 # Check input assumptions set after layer building, e.g. input shape.
752 if build_graph or in_deferred_mode:
--> 753 self._assert_input_compatibility(inputs)
754
755 if not in_deferred_mode:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _assert_input_compatibility(self, inputs)
1511 ' incompatible with the layer: expected axis ' + str(axis) +
1512 ' of input shape to have value ' + str(value) +
-> 1513 ' but received input with shape ' + str(shape))
1514 # Check shape.
1515 if spec.shape is not None:

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 10 but received input with shape [None, 200]

最佳答案

当您在此行的 i1 上应用 dense 层时:

d1 = dense(i1)

这个密集层的权重将被构建,因此在未来它会期望输入具有与其权重兼容的形状。这就是为什么在 i2 上应用 dense 层后您会看到以下错误:

expected axis -1 of input shape to have value 10

i1 的形状是 (10,) 因此 dense 层期望形状为 (10,) 的样本。但是 i2 具有 (200,) 的形状,因此与 dense 层的输入不兼容。

卷积层可以应用于具有不同宽度和高度(但 channel 数量相同)的输入的原因很简单,因为它们的权重形状(即卷积核或过滤器)不依赖于空间维度输入的数量(但是,这取决于输入中的 channel 数,这就是为什么在示例中您提供的 ab 都有 3 个 channel )。

关于python - 具有不同输入大小的 Keras 共享层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54022742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com