Keras 中的 model.fit() 和 model.evaluate() 有什么区别? [英] What is the difference between model.fit() an model.evaluate() in Keras?

查看:89
本文介绍了Keras 中的 model.fit() 和 model.evaluate() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 TensorFlow 后端的 Keras 来训练 CNN 模型.

I am using Keras with TensorFlow backend to train CNN models.

model.fit()model.evaluate() 之间有什么区别?理想情况下我应该使用哪一种?(我现在使用 model.fit() ).

What is the between model.fit() and model.evaluate()? Which one should I ideally use? (I am using model.fit() as of now).

我知道 model.fit()model.predict() 的效用.但我无法理解 model.evaluate() 的效用.Keras 文档只是说:

I know the utility of model.fit() and model.predict(). But I am unable to understand the utility of model.evaluate(). Keras documentation just says:

用于评估模型.

我觉得这是一个非常模糊的定义.

I feel this is a very vague definition.

推荐答案

fit() 用于使用给定的输入(和相应的训练标签)训练模型.

fit() is for training the model with the given inputs (and corresponding training labels).

evaluate() 用于使用验证(或测试)数据和相应的标签来评估已经训练好的模型.返回模型的损失值和指标值.

evaluate() is for evaluating the already trained model using the validation (or test) data and the corresponding labels. Returns the loss value and metrics values for the model.

predict() 用于实际预测.它为输入样本生成输出预测.

predict() is for the actual prediction. It generates output predictions for the input samples.

让我们考虑一个简单的回归示例:

Let us consider a simple regression example:

# input and output
x = np.random.uniform(0.0, 1.0, (200))
y = 0.3 + 0.6*x + np.random.normal(0.0, 0.05, len(y))

现在让我们在 keras 中应用回归模型:

Now lets apply a regression model in keras:

# A simple regression model
model = Sequential()
model.add(Dense(1, input_shape=(1,)))
model.compile(loss='mse', optimizer='rmsprop')

# The fit() method - trains the model
model.fit(x, y, nb_epoch=1000, batch_size=100)

Epoch 1000/1000
200/200 [==============================] - 0s - loss: 0.0023

# The evaluate() method - gets the loss statistics
model.evaluate(x, y, batch_size=200)     
# returns: loss: 0.0022612824104726315

# The predict() method - predict the outputs for the given inputs
model.predict(np.expand_dims(x[:3],1)) 
# returns: [ 0.65680361],[ 0.70067143],[ 0.70482892]

这篇关于Keras 中的 model.fit() 和 model.evaluate() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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