【问题标题】:How to combine embedded columns with other input data in Keras如何在 Keras 中将嵌入的列与其他输入数据结合起来
【发布时间】:2018-06-01 08:48:16
【问题描述】:

我有一列包含 1003 个不同类别的分类数据,我有很多列包含常规整数数据。我想用分类数据嵌入列,并将嵌入的输出与所有其他列一起作为模型的输入。我不确定如何执行此操作,但已尝试使用合并在以下代码中。不幸的是,这会产生 Value 错误:“concat”模式只能合并具有匹配输出形状的图层,除了 concat 轴。图层形状:[(None, 1, 11), (None, 53)]'

任何帮助将不胜感激。

    hidden_layers  = [1000,500,500]


    embedding = Sequential()
    embedding.add(1003, 11, input_length = 1))

    model1 = Sequential()
    model1.add(Dense(53, input_dim=53, activation='relu'))

    model = Sequential()
    model = model.add(Merge([embedding, model1], mode = 'concat'))

    for i, layer_size in enumerate(hidden_layers):
        model.add(Dense(layer_size, activation='relu'))

    model.add(Dense(self.output_layers, activation='linear'))
    model.compile(optimizer = 'adam', loss = 'mse')

【问题讨论】:

    标签: python-3.x tensorflow keras embedding


    【解决方案1】:

    Embedding 层会生成一个 3D 张量,如您在错误消息 (None, 1, 11) 中看到的,其中 1 是您要嵌入的序列长度。为了与 2D 张量合并,您必须将其展平:

    embedding = Sequential()
    embedding.add(Embedding(1003, 11, input_length = 1))
    embedding.add(Flatten())
    

    这将给出 (None, 11) 并且可以与 (None, 53) 合并。

    【讨论】:

    • 有效!但是没有,我遇到了一个新问题,上面写着“AttributeError:'NoneType'对象没有属性'add'”在for循环中的这一行:“model.add(Dense(layer_size,activation='relu') )"
    • 你用model.add的返回值覆盖模型,在for循环之前它是None。
    • 因为它返回None,所以model.add添加了层并且不返回任何东西。您可能会对函数式 API 感到困惑。
    • 那么当我想在一些输入列上使用嵌入但不是全部时我应该怎么做?
    • 你应该切换到functional API 两个有多输入模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多