使用 Embedding 层创建 Keras 深度学习模型,但在训练时返回错误

Posted

技术标签:

【中文标题】使用 Embedding 层创建 Keras 深度学习模型,但在训练时返回错误【英文标题】:Created a Keras deep learning model using Embedding layer but returned an error while training 【发布时间】:2021-08-03 21:15:12 【问题描述】:

我创建了一个 Keras 深度学习模型,使用嵌入层进行情绪分析。但是,当我开始训练模型时,它返回了这个错误,我无法弄清楚。

错误:

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:755 train_step
        loss = self.compiled_loss(
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:203 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:152 __call__
        losses = call_fn(y_true, y_pred)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:256 call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:1537 categorical_crossentropy
        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/backend.py:4833 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    /opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (None, 15) and (None, 1) are incompatible

这是我的模型:

def model_0(opt, train_condition, xTrain, yTrain):
    model = Sequential()
    model.add(Embedding(132190, 8, input_length=60, name='embedding'))
    model.add(LSTM(128, return_sequences=True))
    model.add(LSTM(64, return_sequences=False))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='softmax'))
    model.compile(loss="categorical_crossentropy", optimizer=opt, metrics='accuracy')
    
    if train_condition == True:
        history = model.fit(xTrain, yTrain, epochs=50, batch_size=100, validation_split=0.2)

    opt = tf.keras.optimizers.Adam(learning_rate=0.001)
    model_0(opt, True, xTrain=x_train, yTrain=y_train)

x_train 和 y_train 形状:

x_train: (606965, 60)
y_train: (606965, 15) 

请指教?????????

【问题讨论】:

你的标签 (y_train) 是单热编码的吗?错误的原因是您的标签形状为 (None, 15),而您的输出层形状为 (None, 1) 是的,我有一个热编码的 y_train 你的最后一个dense必须是:Dense(15, activation='softmax') 【参考方案1】:

您的标签似乎与您的模型无关。尝试将最后一个密集层更改为

model.add(Dense(15, activation='softmax'))

你的模型应该如下图所示

def model_0(opt, train_condition, xTrain, yTrain):
    model = Sequential()
    model.add(Embedding(132190, 8, input_length=60, name='embedding'))
    model.add(LSTM(128, return_sequences=True))
    model.add(LSTM(64, return_sequences=False))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(15, activation='softmax'))
    model.compile(loss="categorical_crossentropy", optimizer=opt, metrics='accuracy')
    
    if train_condition == True:
        history = model.fit(xTrain, yTrain, epochs=50, batch_size=100, validation_split=0.2)

    opt = tf.keras.optimizers.Adam(learning_rate=0.001)
    model_0(opt, True, xTrain=x_train, yTrain=y_train)

【讨论】:

以上是关于使用 Embedding 层创建 Keras 深度学习模型,但在训练时返回错误的主要内容,如果未能解决你的问题,请参考以下文章

Keras官方中文文档:嵌入层Embedding

keras的Embedding层

带有嵌入层的 Keras LSTM 自动编码器

keras常见层用法备忘

Keras嵌入层:将零填充值保持为零

如何在 Gensim 的 Word2Vec 中正确使用 get_keras_embedding()?