根据Keras中的shared layers documentation,可以创建一个共享层并使用不同的输入形状实例化它。它给出了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]
答案 0 :(得分:1)
在此行的dense
上应用i1
层时:
d1 = dense(i1)
将构造此密集层的权重,因此在将来,它将期望输入的形状与其权重兼容。这就是为什么在dense
上应用i2
层后看到以下错误的原因:
expected axis -1 of input shape to have value 10
i1
的形状为(10,)
,因此dense
层将期望形状为(10,)
的样本。但是i2
的形状为(200,)
,因此与dense
层的输入不兼容。
将卷积层应用于不同宽度和高度(但通道数相同)的输入的原因仅仅是因为其权重的形状(即卷积核或滤波器)不取决于空间尺寸的输入(但是,这取决于输入中的通道数,这就是在示例中您同时提供a
和b
都具有3个通道的原因)。