Keras custom loss, having error in batch size in y_true and y_pred

断了今生、忘了曾经 提交于 2019-12-13 04:24:50

问题


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.

来源:https://stackoverflow.com/questions/53650690/keras-custom-loss-having-error-in-batch-size-in-y-true-and-y-pred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!