I have a keras RNN model that like this one using pre-trained Word2Vec weights
model = Sequential()
model.add(L.Embedding(input_dim=vocab_size, output_dim=embedding_size,
input_length=max_phrase_length,
weights=[pretrained_weights],trainable=False))
model.add((L.LSTM(units=rnn_units)))
model.add((L.Dense(vocab_size,activation='sigmoid')))
adam=Adam(lr)
model.compile(optimizer=adam, loss='cosine_proximity',
metrics=['cosine_proximity'])
During training I want to create a custom loss function to compare the predicted and true word vectors associated with the predicted and true integer indices.
def custom_loss(y_true,y_pred):
A=extract_the_word_vectors_for_the_indices(y_true)
B=extract_the_word_vectors_for_the_indices(y_pred)
return some keras backend function of A and B
For example, suppose my batch size is 4. Then from model.fit, I can pass y_pred
through an argmax
such that K.argmax(y_pred)=[i1,i2,i3,4]
, integers corresponding to the word vectors vectors[i1], vectors[i2], vectors[i3], vectors[i4]
. I want to do some maths with the predicted vectors and compare them to the ground truth vectors, as a way to monitor progress (not as a loss function). So I need a "Keras-full" way to do this.
If y_true
were a numpy array of indices and word_model
is my word2vec model, then I could get an array of the vectors by just doing word_model.wv.vectors[y_true]
. However it seems very wasteful to convert y_true
from tensor to numpy, then back to tensor later. So I can't seem to get anything to work in native keras, and when I try to extract the tensors to numpy arrays and work with those, I get errors as well. Grrrr...
I imagine there has to be a way to extract the word vectors from the embedding layer for y_pred and y_true, but I have no idea how. Anyone?