将输入与常数向量在keras中并置 [英] Concatenate input with constant vector in keras
问题描述
我正在尝试将我的输入与keras-2函数API中的恒定张量连接起来.在我真正的问题中,常量取决于设置中的某些参数,但是我认为以下示例显示了我得到的错误.
I am trying to concatenate my input with a constant tensor in the keras-2 function API. In my real problem, the constants depend on some parameters in setup, but I think the example below shows the error I get.
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版本0.10.0dev1
的theano后端运行keras版本2.0.5
.关于出了什么问题或实现连接的更正确方法的任何想法?
I am running keras version 2.0.5
with the theano backend, with theano version 0.10.0dev1
. Any ideas on what is going wrong or a more correct way to accomplish the concatenation?
推荐答案
喀拉拉邦维数的工作方式如下:
Dimensions in keras work like this:
- 在分层定义它们并构建模型时,您永远不会定义"batch_size".
- 在内部,使用后端函数,损失函数和任何张量操作,批处理维度是第一个
Keras向您显示一个None
,以摘要,错误和其他形式表示批次大小.
Keras shows you a None
to represent the batch size in summaries, errors and others.
这意味着:
- a的形状是(None,10,5)
- a1的形状仅为(10,5).您不能将它们串联起来.
您可以执行一些解决方法,例如创建形状为(1,10,5)的a1,然后在批处理维度中重复其值:
There are a few workarounds you can do, such as creating a1 with shape (1,10,5) and then repeating it's values in the batch dimension:
constant=K.variable(np.ones((1,10, 5)))
constant = K.repeat_elements(constant,rep=batch_size,axis=0)
我完全无法使用Input(tensor=...)
,因为常量的维是固定的,而输入的维是None
,所以我在lambda层上进行了处理:
I was totally unable to use Input(tensor=...)
because the constant's dimension is fixed, and the input's dimension is None
, so I worked it around with a lambda layer:
b = Lambda(lambda x: K.concatenate([x,constant],axis=1),output_shape=(20,5))(a)
但是我完全不明白您想用x += [b]
和其他实现什么.
But I can't at all understand what you want to achieve with x += [b]
and the rest.
这篇关于将输入与常数向量在keras中并置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!