-->

Keras custom loss, having error in batch size in y

2019-08-27 19:38发布

问题:

I want to create a custom loss function, where I want to use my pre calculated y_true, but unfortunately, when I use that I am having error with the batch size not matching with y_pred. I am interested to know the work around. Here is the example code of my custom loss.

  def avg_vect_loss(y_true, y_pred):
    #y_true need not to be used, instead pos_avg_vects and neg_avg_vects are considered as my pre calculated y_trues
     pos_avg_vects, neg_avg_vects = pos_neg_avg_vects()
     l2_dist_to_pos_avg = l2_distance_loss(y_pred, pos_avg_vects)
     l2_dist_to_neg_avg = l2_distance_loss(y_pred, neg_avg_vects)
     return l2_dist_to_pos_avg/l2_dist_to_neg_avg

Code is commented. My loss function is trying to maximize l2 distance between y_pred to neg_avg_vec and minimize l2_distance between y_pred and pos_avg_vec. I am having error because of batch size mismatch between my y_pred and pos_avg_vects, neg_avg_vects.

pos_avg_vector and neg_avg_vector are the average vectors of the nearest and farthest neighbours of y_true.

Please note, y_true, y_pred are the word vectors. My training set and test set consists of word vectors of corresponding words.