Keras: Predict model within custom loss function

谁说胖子不能爱 提交于 2021-02-05 05:57:26

问题


I am trying to use some_model.predict(x) within a custom loss function.

I found this custom loss function:

_EPSILON = K.epsilon()

def _loss_tensor(y_true, y_pred):
    y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
    out = -(y_true * K.log(y_pred) + (1.0 - y_true) * K.log(1.0 - y_pred))
    return K.mean(out, axis=-1)

But the problem is that model.predict() is expecting a numpy array. So I looked for how to convert a tensor (y_pred) to a numpy array. I found tmp = K.tf.round(y_true) but this returns a tensor. I have also found: x = K.eval(y_true) which takes a Keras variable and returns a numpy array. This produces the error: You must feed a value for placeholder tensor 'dense_78_target' with dtype float..... Some people suggested setting the learning phase to true. I did that, but it did not help.

What I just want to do:

def _loss_tensor(y_true, y_pred):
    y_tmp_true = first_decoder.predict(y_true)
    y_tmp_pred = first_decoder.predict(y_pred)

    return keras.losses.binary_crossentropy(y_tmp_true,y_tmp_pred)

Any help would be appreciated.

This works:

sess = K.get_session()
with sess.as_default():
  tmp = K.tf.constant([1,2,3]).eval()
  print(tmp)

I also tried this now:

tmp = first_decoder(y_true)

This fails the assertion:

assert input_shape[-1]

Maybe someone knows how to resolve this?

Update: I can now feed it through the model with:

y_t = first_decoder(K.reshape(y_true, (1,512)))
y_p = first_decoder(K.reshape(y_pred, (1,512)))

But when I try to return the binary cross entropy the shape is not right:

Input to reshape is a tensor with 131072 values, but the requested shape has 
512

I figured out that 131072 was the product of my batch size and input size (256*512). I then adopted my code to reshape to (256,512) size. The first batch runs fine, but then I get another error that says that the passed size was (96,512).

[SOLVED]Update: It works now:

def _loss_tensor(y_true, y_pred):
    num_ex = K.shape(y_true)[0]
    y_t = first_decoder(K.reshape(y_true, (num_ex,512)))
    y_p = first_decoder(K.reshape(y_pred, (num_ex,512)))

    return keras.losses.binary_crossentropy(y_t,y_p)

来源:https://stackoverflow.com/questions/52284595/keras-predict-model-within-custom-loss-function

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