How to use a pre-trained embedding matrix in tensorflow 2.0 RNN as initial weights in an embedding layer?
Asked Answered
S

2

5

I'd like to use a pretrained GloVe embedding as the initial weights for an embedding layer in an RNN encoder/decoder. The code is in Tensorflow 2.0. Simply adding the embedding matrix as a weights = [embedding_matrix] parameter to the tf.keras.layers.Embedding layer won't do it because the encoder is an object and I'm not sure now to effectively pass the embedding_matrix to this object at training time.

My code closely follows the neural machine translation example in the Tensorflow 2.0 documentation. How would I add a pre-trained embedding matrix to the encoder in this example? The encoder is an object. When I get to training, the GloVe embedding matrix is unavailable to the Tensorflow graph. I get the error message:

RuntimeError: Cannot get value inside Tensorflow graph function.

The code uses the GradientTape method and teacher forcing in the training process.

I've tried modifying the encoder object to include the embedding_matrix at various points, including in the encoder's init, call and initialize_hidden_state. All of these fail. The other questions on stackoverflow and elsewhere are for Keras or older versions of Tensorflow, not 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)))
Stanislaw answered 20/4, 2019 at 3:33 Comment(0)
L
7

I was trying to do the same thing and getting the exact same error. The problem was that weights in the Embedding layer is currently deprecated. Changing weights= to embeddings_initializer= worked for me.

self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim, 
embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix),
trainable=False)
Longe answered 31/7, 2019 at 6:53 Comment(1)
Beautiful, but how could I load the embedding_matrix?Ovolo
B
4

firslty : load pretrained embedding matrix using

      def pretrained_embeddings(file_path, EMBEDDING_DIM, VOCAB_SIZE, word2idx):
          # 1.load in pre-trained word vectors     #feature vector for each word
          print("graph in function",tf.get_default_graph())   
          print('Loading word vectors...')
          word2vec = {}
          with open(os.path.join(file_path+'.%sd.txt' % EMBEDDING_DIM),  errors='ignore', encoding='utf8') as f:
          # is just a space-separated text file in the format:
          # word vec[0] vec[1] vec[2] ...
          for line in f:
             values = line.split()
             word = values[0]
             vec = np.asarray(values[1:], dtype='float32')
             word2vec[word] = vec

          print('Found %s word vectors.' % len(word2vec))

          # 2.prepare embedding matrix
          print('Filling pre-trained embeddings...')
          num_words = VOCAB_SIZE
          # initialization by zeros
          embedding_matrix = np.zeros((num_words, EMBEDDING_DIM))
          for word, i in word2idx.items():
            if i < VOCAB_SIZE:
                embedding_vector = word2vec.get(word)
                if embedding_vector is not None:
                  # words not found in embedding index will be all zeros.
                  embedding_matrix[i] = embedding_vector

          return embedding_matrix

2-then update Encoder class as following:

    class Encoder(tf.keras.Model):
       def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz,embedding_matrix):
          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))

3-calling function that loads pre-trained embedding to get embedding matrix

    embedding_matrix = pretrained_embeddings(file_path, EMBEDDING_DIM,vocab_size, word2idx) 
    encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE,embedding_matrix)

    # 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))

Note : this works on tensorflow 1.13.1 well

Batista answered 29/6, 2019 at 19:30 Comment(1)
example_input_batch is the hot-encoded word matrix, correct?Karankaras

© 2022 - 2024 — McMap. All rights reserved.