I would like to custom a Keras loss function but I do not really understand something.
If I use tensorflow as a backend for Keras, do I need to use functions from keras.backend
or can I use functions directly from tensorflow.
I only see posts where people are using functions from keras.backend
but not from tensorflow (even if tensorflow has much more functions). Are there reasons to do so?
For a toy example :
from keras import backend as K
import tensorflow as tf
def loss_keras(y_true, y_pred):
square_error = K.square(y_pred - y_true)
loss = K.mean(square_error)
return loss
def loss_tf(y_true, y_pred):
square_error = tf.squared_difference(y_pred, y_true)
loss = tf.reduce_mean(square_error)
return loss
Both of these functions work well but one is using directly tensorflow and the other is using keras.backend
functions.
I know that this is a silly example but when you want to do more complicated stuff, I thought that using tensorflow would be easier than keras functions as there are more functions available
如评论中所指出和在此答案中所述: “在以下情况下,必须使用Keras后端函数(即keras.backend。*):1)需要预处理或扩充传递给实际函数的参数Tensorflow或Theano后端或对返回的结果进行后处理,或2)您想编写一个可在所有Keras支持的后端上使用的模型。”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.