tensorflow - 如何在 tensorflow 2.0 RNN 中使用预训练的嵌入矩阵作为嵌入层的初始权重?

标签 tensorflow nlp recurrent-neural-network embedding glove

我想使用预训练的 GloVe 嵌入作为 RNN 编码器/解码器中嵌入层的初始权重。代码在 Tensorflow 2.0 中。简单地将嵌入矩阵作为 weights = [embedding_matrix] 参数添加到 tf.keras.layers.Embedding 层不会这样做,因为编码器是一个对象,我现在不确定是否将 embedding_matrix 有效地传递给这个对象训练时间。

我的代码紧跟 neural machine translation example in the Tensorflow 2.0 documentation .在此示例中,我将如何向编码器添加预先训练的嵌入矩阵?编码器是一个对象。当我开始训练时,GloVe 嵌入矩阵对 Tensorflow 图不可用。我收到错误消息:

运行时错误:无法在 Tensorflow 图形函数中获取值。

代码在训练过程中使用了 GradientTape 方法和教师强制。

我尝试修改编码器对象以在各个点包含 embedding_matrix,包括在编码器的 中。初始化 ,调用并初始化_hidden_​​state。所有这些都失败了。 stackoverflow 和其他地方的其他问题是针对 Keras 或更旧版本的 Tensorflow,而不是 Tensorflow 2.0。

class Encoder(tf.keras.Model):
    def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
        super(Encoder, self).__init__()
        self.batch_sz = batch_sz
        self.enc_units = enc_units
        self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix])
        self.gru = tf.keras.layers.GRU(self.enc_units,
                                       return_sequences=True,
                                       return_state=True,
                                       recurrent_initializer='glorot_uniform')

    def call(self, x, hidden):
        x = self.embedding(x)
        output, state = self.gru(x, initial_state = hidden)
        return output, state

    def initialize_hidden_state(self):
        return tf.zeros((self.batch_sz, self.enc_units))

encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE)

# sample input
sample_hidden = encoder.initialize_hidden_state()
sample_output, sample_hidden = encoder(example_input_batch, sample_hidden)
print ('Encoder output shape: (batch size, sequence length, units) {}'.format(sample_output.shape))
print ('Encoder Hidden state shape: (batch size, units) {}'.format(sample_hidden.shape))

# ... Bahdanau Attention, Decoder layers, and train_step defined, see link to full tensorflow code above ...

# Relevant training code

EPOCHS = 10

training_record = pd.DataFrame(columns = ['epoch', 'training_loss', 'validation_loss', 'epoch_time'])


for epoch in range(EPOCHS):
    template = 'Epoch {}/{}'
    print(template.format(epoch +1,
                 EPOCHS))
    start = time.time()

    enc_hidden = encoder.initialize_hidden_state()
    total_loss = 0
    total_val_loss = 0

    for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
        batch_loss = train_step(inp, targ, enc_hidden)
        total_loss += batch_loss

        if batch % 100 == 0:
            template = 'batch {} ============== train_loss: {}'
            print(template.format(batch +1,
                            round(batch_loss.numpy(),4)))

最佳答案

我试图做同样的事情并得到完全相同的错误。问题是嵌入层中的权重目前已被弃用。换 weights=embeddings_initializer=为我工作。

self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim, 
embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix),
trainable=False)

关于tensorflow - 如何在 tensorflow 2.0 RNN 中使用预训练的嵌入矩阵作为嵌入层的初始权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770009/

相关文章:

python-3.x - 使用 tf.reshape() 时出现无效参数错误

python - 回归: Training Test Split - held out test?

python - nlp 多标签分类 tf 与 tfidf

machine-learning - 使用 NLP 从字符串中查找意图的良好资源

python - tensorflow 2.0 中是否有 cudnnLSTM 或 cudNNGRU 替代方案

elasticsearch - 使用机器学习创建异常检测

speech-recognition - 如何准备语音识别数据集

python - Nesterov 的加速梯度下降是如何在 Tensorflow 中实现的?

python - 如何在tensorflow中保存迭代模型和最佳模型?

python - 使用 sklearn 和 pandas 在一个模型中结合词袋和其他特征