【问题标题】:Concatenate input with constant vector in keras在keras中将输入与常数向量连接起来
【发布时间】:2017-06-24 20:32:22
【问题描述】:

我正在尝试将我的输入与 keras-2 函数 API 中的常量张量连接起来。在我真正的问题中,常量取决于设置中的一些参数,但我认为下面的示例显示了我得到的错误。

from keras.layers import*
from keras.models import *
from keras import backend as K
import numpy as np

a = Input(shape=(10, 5))
a1 = Input(tensor=K.variable(np.ones((10, 5))))
x = [a, a1]  # x = [a, a] works fine
b = concatenate(x, 1)
x += [b]  # This changes b._keras_history[0].input
b = concatenate(x, 1)
model = Model(a, b)

我得到的错误是:

ValueError                                Traceback (most recent call last)
~/miniconda3/envs/ds_tools/lib/python3.6/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
    418             try:
--> 419                 K.is_keras_tensor(x)
    420             except ValueError:

~/miniconda3/envs/ds_tools/lib/python3.6/site-packages/keras/backend/theano_backend.py in is_keras_tensor(x)
    198                           T.sharedvar.TensorSharedVariable)):
--> 199         raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
    200                          'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type `<class 'theano.gpuarray.type.GpuArraySharedVariable'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-2-53314338ab8e> in <module>()
      5 a1 = Input(tensor=K.variable(np.ones((10, 5))))
      6 x = [a, a1]
----> 7 b = concatenate(x, 1)
      8 x += [b]  # This changes b._keras_history[0].input
      9 b = concatenate(x, 1)

~/miniconda3/envs/ds_tools/lib/python3.6/site-packages/keras/layers/merge.py in concatenate(inputs, axis, **kwargs)
    506         A tensor, the concatenation of the inputs alongside axis `axis`.
    507     """
--> 508     return Concatenate(axis=axis, **kwargs)(inputs)
    509 
    510 

~/miniconda3/envs/ds_tools/lib/python3.6/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    550                 # Raise exceptions in case the input is not compatible
    551                 # with the input_spec specified in the layer constructor.
--> 552                 self.assert_input_compatibility(inputs)
    553 
    554                 # Collect input shapes to build layer.

~/miniconda3/envs/ds_tools/lib/python3.6/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
    423                                  'Received type: ' +
    424                                  str(type(x)) + '. Full input: ' +
--> 425                                  str(inputs) + '. All inputs to the layer '
    426                                  'should be tensors.')
    427 

ValueError: Layer concatenate_2 was called with an input that isn't a symbolic tensor. Received type: <class 'theano.gpuarray.type.GpuArraySharedVariable'>. Full input: [concatenate_1/input_3, concatenate_1/variable]. All inputs to the layer should be tensors.

我正在运行带有theano 后端的keras 版本2.0.5,以及theano 版本0.10.0dev1。关于出了什么问题或更正确的方法来完成连接的任何想法?

【问题讨论】:

标签: python keras keras-2


【解决方案1】:

keras 中的维度是这样工作的:

  • 当您在层中定义它们、构建模型时,您永远不会定义“batch_size”。
  • 在内部,使用后端函数,在损失函数和任何张量操作中,批量维度是第一个

Keras 向您显示 None 以在摘要、错误和其他方面表示批量大小。

这意味着:

  • a 的形状是 (None, 10, 5)
  • a1 的形状正好是 (10,5)。你不能连接它们。

您可以采取一些解决方法,例如创建形状为 (1,10,5) 的 a1,然后在批处理维度中重复它的值:

constant=K.variable(np.ones((1,10, 5)))
constant = K.repeat_elements(constant,rep=batch_size,axis=0)

我完全无法使用Input(tensor=...),因为常量的维度是固定的,而输入的维度是None,所以我使用了一个 lambda 层:

b = Lambda(lambda x: K.concatenate([x,constant],axis=1),output_shape=(20,5))(a)

但我完全不明白你想用x += [b] 和其他实现什么。

【讨论】:

  • 感谢您的详细回答。我想要的形状是 (10,5),而不是 (None, 10, 5),但我可以根据你写的内容进行翻译。
  • 你需要(None,10,5),否则会与模型根本不兼容。当您说input_shape=(10,5) 时,您肯定是在创建(None,10,5) 形状。这就是 keras 的工作原理。
  • 对,我的代码中的输入定义是错误的。它应该是inp = Input(shape=(5,)),但我没有抓住它,因为它甚至在检查形状之前就给出了错误。
  • 我创建了一个类似的问题,主要是因为我无法将此答案映射到我的问题,可能是因为我没有经验。如果您能看看我的问题,我将不胜感激。 stackoverflow.com/questions/58158620/…
猜你喜欢
  • 2018-10-19
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多