凯拉斯。连接图层。类型错误

Keras. Concatenate layers. TypeError

我想创建一个孪生模型,定义较低以用于协同过滤。第一个创建用户的嵌入,第二个创建项目的嵌入。

import keras

from keras import backend as K

from keras.layers import Input, Embedding, Dense, Flatten, concatenate
from keras.models import Model

n_users, n_items = 100, 3000

users_input     = Input(shape=(n_users,), dtype='int32', name='users')
users_embedding = Embedding(output_dim=6, input_dim=n_users, input_length=1)(users_input)
users_flatten   = Flatten()(users_embedding)

items_input     = Input(shape=(n_items,), dtype='int32', name='items')
items_embedding = Embedding(output_dim=6, input_dim=n_items, input_length=1)(items_input)
items_flatten   = Flatten()(items_embedding)

layer_0 = concatenate([users_flatten, items_flatten])
layer_1 = Dense(8, activation='relu')(layer_0)
layer_2 = Dense(1, activation='relu')(layer_1)

model = Model(inputs=[users_input, items_input], outputs=[layer_2])

如您所见,我在串联方面遇到了问题。这是我的堆栈跟踪:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-bf475de5f9cc> in <module>()
----> 1 layer_0 = concatenate([users_flatten, items_flatten])
      2 layer_1 = Dense(8, activation='relu')(layer_0)
      3 layer_2 = Dense(1, activation='relu')(layer_1)

/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc 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 

/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
    583 
    584             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 585             output = self.call(inputs, **kwargs)
    586             output_mask = self.compute_mask(inputs, previous_mask)
    587 

/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc in call(self, inputs)
    281             raise ValueError('A `Concatenate` layer should be called '
    282                              'on a list of inputs.')
--> 283         return K.concatenate(inputs, axis=self.axis)
    284 
    285     def compute_output_shape(self, input_shape):

/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in concatenate(tensors, axis)
   1679         return tf.sparse_concat(axis, tensors)
   1680     else:
-> 1681         return tf.concat([to_dense(x) for x in tensors], axis)
   1682 
   1683 

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(concat_dim, values, name)
    998       ops.convert_to_tensor(concat_dim,
    999                             name="concat_dim",
-> 1000                             dtype=dtypes.int32).get_shape(
   1001                             ).assert_is_compatible_with(tensor_shape.scalar())
   1002       return identity(values[0], name=scope)

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    667 
    668         if ret is None:
--> 669           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    670 
    671         if ret is NotImplemented:

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    174                                          as_ref=False):
    175   _ = as_ref
--> 176   return constant(v, dtype=dtype, name=name)
    177 
    178 

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
    163   tensor_value = attr_value_pb2.AttrValue()
    164   tensor_value.tensor.CopyFrom(
--> 165       tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    166   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    167   const_tensor = g.create_op(

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
    365       nparray = np.empty(shape, dtype=np_dt)
    366     else:
--> 367       _AssertCompatible(values, dtype)
    368       nparray = np.array(values, dtype=np_dt)
    369       # check to them.

/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
    300     else:
    301       raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302                       (dtype.name, repr(mismatch), type(mismatch).__name__))
    303 
    304 

TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

作为示例,我为 functional API 使用了 Keras 文档。我使用 TensorFlow 作为后端。

解决方案:更新Keras和Tresorflow到最新版本。