需要在model.evaluate()之前编译Keras模型 [英] Need To Compile Keras Model Before `model.evaluate()`

查看:490
本文介绍了需要在model.evaluate()之前编译Keras模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 .json .hdf5 文件加载了Keras模型.当我呼叫model.evaluate()时,它返回一个错误:

I load a Keras model from .json and .hdf5 files. When I call model.evaluate(), it returns an error:

您必须在训练/测试之前编译模型.使用`model.compile(optimizer,loss)

You must compile a model before training/testing. Use `model.compile(optimizer, loss)

为什么需要编译才能运行evaluate()?

Why do I need to compile to run evaluate()?

要添加,可以毫无问题地通过predict()传递模型.

To add, the model can be passed predict() with no problem.

推荐答案

因为 evaluate 将计算损失函数和指标.

在编译模型之前,您将没有任何一个.它们是编译方法的参数:

You don't have any of them until you compile the model. They're parameters to the compile method:

model.compile(optimizer=..., loss=..., metrics=...) 

另一方面, predict 不评估任何指标或损失,它只是将输入数据传递通过模型并获得其输出.

On the other hand, predict doesn't evaluate any metric or loss, it just passes the input data through the model and gets its output.

您也需要进行训练的损失",因此,如果不进行编译就无法进行训练.您可以根据需要多次编译模型,甚至可以更改参数.

You need the "loss" for training too, so you can't train without compiling. And you can compile a model as many times as you want, and even change the parameters.

输出和损失函数:

模型的输出取决于使用权重定义的模型.这是自动的,即使没有任何培训,您也可以从任何模型中进行predict. Keras中的每个模型都已经带有权重(由您初始化或随机初始化)

The model's outputs depend on it being defined with weights. That is automatic and you can predict from any model, even without any training. Every model in Keras is already born with weights (either initialized by you or randomly initialized)

您输入一些东西,模型将计算输出.最后,这才是最重要的.好的模型应具有适当的权重,并可以正确输出结果.

You input something, the model calculates the output. In the end of everything, this is all that matters. A good model has proper weights and outputs things correctly.

但是在达到此目的之前,需要对模型进行训练.

But before getting to that end, your model needs to be trained.

现在,损失函数将获取当前输出并将其与预期/真实结果进行比较.该功能应该被最小化.损失越少,您的结果就越接近预期.这是从中获取导数的函数,因此反向传播算法可以更新权重.

Now, the loss function takes the current output and compares it with the expected/true result. It's a function supposed to be minimized. The less the loss, the closer your results are to the expected. This is the function from which the derivatives will be taken so the backpropagation algorithm can update the weights.

损失函数对于模型的最终目的不是有用的,但是对于训练来说是必需的.这可能就是为什么您可以拥有没有损失函数的模型的原因(因此,无法评估它们).

The loss function is not useful for the final purpose of the model, but it's necessary for training. That's probably why you can have models without loss functions (and consequently, there is no way to evaluate them).

这篇关于需要在model.evaluate()之前编译Keras模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆