Custom loss function not differentiable
I have been trying to implement my own custom loss function for an LSTM encoder decoder architecture using tensorflow , but when I am training using train_on_batch , I am training in eager mode for a reason that I'll explain later , I get this error :
No gradients provided for any variable: (['encoder_lstm/lstm_cell_94/kernel:0', 'encoder_lstm/lstm_cell_94/recurrent_kernel:0', 'encoder_lstm/lstm_cell_94/bias:0', 'decoder_lstm/lstm_cell_95/kernel:0', 'decoder_lstm/lstm_cell_95/recurrent_kernel:0', 'decoder_lstm/lstm_cell_95/bias:0', 'decoder_dense/kernel:0', 'decoder_dense/bias:0'],). Provided grads_and_vars is ((None, <tf.Variable 'encoder_lstm/lstm_cell_94/kernel:0' shape=(300, 200) dtype=float32
my custom loss is :
def sim_desim(y_true, y_pred, sent_length_reg, sim_reg, window_size, token2id, embedding_weights):
# y_true = tf.make_tensor_proto(y_true)
# y_true = tf.make_ndarray(y_true)
# y_pred = tf.make_tensor_proto(y_pred)
# y_pred = tf.make_ndarray(y_pred)
y_true = y_true.numpy()
y_pred = y_pred.numpy()
batch_cost = np.zerors(y_true.shape[0]) # batch size
y_pred_embedded = np.ndarray(y_true.shape) # batch size, sent length, word embedding dim
# transforming y_pred into a tensor of same shape as y_true
pad_embedding = embedding_weights[token2id['<PAD>']][:]
end_embedding = embedding_weights[token2id['<END>']][:]
pad_embedding.resize((1,300))
end_embedding.resize((1,300))
sent_embedding = np.zeros(y_true.shape[1:]) # sent length, word embedding dim
word_embedding = np.zeros((1,y_true.shape[2]))
for i, sent in enumerate(y_pred): # for each sent in the batch (first dim)
sent_embedding.fill(0)
token_ids = np.argmax(sent, axis=1)
for j, token_id in enumerate(token_ids):
word_embedding = embedding_weights[token_id,:]
word_embedding.resize((1,embedding_weights.shape[1]))
np.put_along_axis(arr=sent_embedding, indices=np.full((1,embedding_weights.shape[1]),j), values=word_embedding, axis=0)
np.put_along_axis(arr=y_pred_embedded, indices=np.full((1,y_pred.shape[1],embedding_weights.shape[1]),i), values=sent_embedding, axis=0)
for i, sent in enumerate(y_pred_embedded): # for each sent in the batch
len_actual = min(index_of_vec(pad_embedding, y_pred_embedded[i,:,:]), index_of_vec(end_embedding, y_pred_embedded[i,:,:]), y_pred_embedded.shape[1])
len_desired = min(index_of_vec(pad_embedding, y_true[i,:,:]), index_of_vec(end_embedding, y_true[i,:,:]), y_true.shape[1])
sent_cost = np. 338