首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Keras:两个嵌入层的连接失败

Keras:两个嵌入层的连接失败
EN

Stack Overflow用户
提问于 2020-11-01 23:22:34
回答 1查看 321关注 0票数 2

我正在尝试构建一个简单的模型,该模型采用两个一维输入样本,使用嵌入层映射它们,并根据连接的嵌入计算分数:

代码语言:javascript
运行
复制
from tensorflow.keras.layers import Dense, Input, Concatenate, Embedding
from tensorflow.keras.models import Model 

input_1 = Input(shape=(1,)) # each input sample is an integer from 0 to 9
input_2 = Input(shape=(1,))

embedding_1 = Embedding(input_dim=10, output_dim=2) # map each input to 2d embedding vector 
embedding_2 = Embedding(input_dim=10, output_dim=2)

combined = Concatenate()([embedding_1, embedding_2])

score = Dense(1, activation='linear')(combined)

model = Model(inputs=[input_1, input_2], outputs=score)

以上操作失败,并显示以下错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-146-f8b1f9bf33a7> in <module>
     12 embedding_2 = Embedding(input_dim=10, output_dim=2)
     13 
---> 14 combined = Concatenate(axis=1)([embedding_1, embedding_2])
     15 
     16 score = Dense(1, activation='linear')(combined)

~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    980       with ops.name_scope_v2(name_scope):
    981         if not self.built:
--> 982           self._maybe_build(inputs)
    983 
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):

~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2641         # operations.
   2642         with tf_utils.maybe_init_scope(self):
-> 2643           self.build(input_shapes)  # pylint:disable=not-callable
   2644       # We must set also ensure that the layer is marked as built, and the build
   2645       # shape is stored since user defined build functions may not be calling

~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    321     if input_shape is not None:
    322       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 323     output_shape = fn(instance, input_shape)
    324     # Return shapes from `fn` as TensorShapes.
    325     if output_shape is not None:

~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    490   def build(self, input_shape):
    491     # Used purely for shape validation.
--> 492     if not isinstance(input_shape[0], tuple) or len(input_shape) < 2:
    493       raise ValueError('A `Concatenate` layer should be called '
    494                        'on a list of at least 2 inputs')

TypeError: 'NoneType' object is not subscriptable
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-02 00:02:33

您没有将输入传递到嵌入层。

这应该是可行的:

代码语言:javascript
运行
复制
embedding_1 = Embedding(input_dim=10, output_dim=2)(input_1)
embedding_2 = Embedding(input_dim=10, output_dim=2)(input_2) 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64633512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档