Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between model.fit() an model.evaluate() in Keras?

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

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

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:

It is used to evaluate the model.

I feel this is a very vague definition.

like image 732
Abhijit Balaji Avatar asked Jun 30 '17 09:06

Abhijit Balaji


People also ask

What is model evaluate in keras?

Evaluation is a process during development of the model to check whether the model is best fit for the given problem and corresponding data. Keras model provides a function, evaluate which does the evaluation of the model. It has three main arguments, Test data.

What is the difference between model predict and model evaluate?

predict() returns the final output of the model, i.e. answer. While model. evaluate() returns the loss. The loss is used to train the model (via backpropagation) and it is not the answer.

What does model evaluate mean?

Model evaluation is the process of using different evaluation metrics to understand a machine learning model's performance, as well as its strengths and weaknesses. Model evaluation is important to assess the efficacy of a model during initial research phases, and it also plays a role in model monitoring.

What does fit function do in keras?

fit method. Trains the model for a fixed number of epochs (iterations on a dataset). x: Input data.


2 Answers

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

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() 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)) 

enter image description here

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] 
like image 186
vijay m Avatar answered Oct 10 '22 14:10

vijay m


In Deep learning you first want to train your model. You take your data and split it into two sets: the training set, and the test set. It seems pretty common that 80% of your data goes into your training set and 20% goes into your test set.

Your training set gets passed into your call to fit() and your test set gets passed into your call to evaluate(). During the fit operation a number of rows of your training data are fed into your neural net (based on your batch size). After every batch is sent the fit algorithm does back propagation to adjust the weights in your neural net.

After this is done your neural net is trained. The problem is sometimes your neural net gets overfit which is a condition where it performs well for the training set but poorly for other data. To guard against this situation you run the evaluate() function to send new data (your test set) through your neural net to see how it performs with data it has never seen. There is no training occurring, this is purely a test. If all goes well then the score from training is similar to the score from testing.

like image 39
rancidfishbreath Avatar answered Oct 10 '22 13:10

rancidfishbreath