[英]Evaluating Pretrained Tensorflow keras model using various loss functions
I'm searching for a way to evaluate a pre-trained TensorFlow Keras model using various loss functions such as MAE
, MSE
,.... and as I checked the Model.evaluate()
function doesn't accept a loss function type as an argument, is it possible to do this without the need of recompiling the model every time we want to evaluate with a new loss function? I'm searching for a way to evaluate a pre-trained TensorFlow Keras model using various loss functions such as
MAE
, MSE
,.... and as I checked the Model.evaluate()
function doesn't accept a loss function type as一个论点,是否可以在每次我们想要评估新的损失 function 时无需重新编译 model 来做到这一点? what is the easiest way to do this?最简单的方法是什么?
You can use multiple loss functions without recompiling;您可以使用多个损失函数而无需重新编译; all you have to do is Assuming First Loss Method As Loss 1 & Second As Loss 2.
您所要做的就是假设第一个损失方法为损失 1 和第二个损失方法为损失 2。
optimizer1 = tf.train.AdamOptimizer().minimize(loss1)
optimizer2 = tf.train.AdamOptimizer().minimize(loss2)
_, _, l1, l2 = sess.run(fetches=[optimizer1, optimizer2, loss1, loss2], feed_dict={x: batch_x, y: batch_y})
Sorry about the inconvenient writing of code, I'm new here写代码不方便见谅,我是新来的
You can recompile your model with new metrics.您可以使用新指标重新编译 model。 I believe you need new metrics for evaluating, not a new loss.
我相信你需要新的评估指标,而不是新的损失。
For example, define a model like this:例如,像这样定义 model:
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(x_train.shape[1:])),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax"),
])
model.compile(loss="sparse_categorical_crossentropy", metrics=["accuracy"], optimizer="adam")
model.fit(x_train, y_train, epochs=3, validation_split=0.2)
model.evaluate(x_test, y_test)
# 313/313 [=================] - 1s 3ms/step - loss: 0.2179 - accuracy: 0.9444
Then you can recompile and evaluate again like:然后您可以重新编译并再次评估,例如:
# Change metrics
model.compile(metrics=["mae", "mse"], loss="sparse_categorical_crossentropy")
model.evaluate(x_test, y_test)
# 313/313 [=================] - 1s 3ms/step - loss: 0.2179 - mae: 4.3630 - mse: 27.3351
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.