[英]Keras backend (tensorflow) vs Keras
I would like to custom a Keras loss function but I do not really understand something. 我想自定义Keras损失函数,但我不太了解。
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. 如果我使用tensorflow作为Keras的后端,我需要使用
keras.backend
函数还是可以直接使用tensorflow中的函数?
I only see posts where people are using functions from keras.backend
but not from tensorflow (even if tensorflow has much more functions). 我只看到人们使用
keras.backend
功能而不是tensorflow中的帖子(即使tensorflow具有更多功能)。 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. 这两个函数都运行良好,但是一个函数直接使用了tensorflow,另一个函数使用了
keras.backend
函数。
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 我知道这是一个愚蠢的例子,但是当您想做更复杂的事情时,我认为使用tensorflow比使用keras函数要容易,因为有更多可用的函数
如评论中所指出和在此答案中所述: “在以下情况下,必须使用Keras后端函数(即keras.backend。*):1)需要预处理或扩充传递给实际函数的参数Tensorflow或Theano后端或对返回的结果进行后处理,或2)您想编写一个可在所有Keras支持的后端上使用的模型。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.