Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loss, metrics, and scoring in Keras

What is the difference between loss, metrics and scoring in building a keras model? Should they be different or same? In a typical model, we use all of the three forGridSearchCV.

Here is the snapshot of a typical model for regression which uses all the three.

def create_model():

 model = Sequential()
 model.add(Dense(12, input_dim=1587, activation='relu'))
 model.add(Dense(1, activation='sigmoid'))

 model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mean_squared_error'])
 return model

model = KerasRegressor(build_fn=create_model, verbose=0)
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model,param_grid=param_grid, scoring='r2' n_jobs=-1)
grid_result = grid.fit(X, Y)
like image 626
Abdul Karim Khan Avatar asked Jul 10 '18 03:07

Abdul Karim Khan


People also ask

What is the difference between loss and metrics Keras?

The loss function is used to optimize your model. This is the function that will get minimized by the optimizer. A metric is used to judge the performance of your model. This is only for you to look at and has nothing to do with the optimization process.

How is Keras loss calculated?

Loss calculation is based on the difference between predicted and actual values. If the predicted values are far from the actual values, the loss function will produce a very large number. Keras is a library for creating neural networks.

What is loss in Keras model?

Loss: A scalar value that we attempt to minimize during our training of the model. The lower the loss, the closer our predictions are to the true labels. This is usually Mean Squared Error (MSE) as David Maust said above, or often in Keras, Categorical Cross Entropy.


1 Answers

No, they are all different things used for different purposes in your code.

There are two parts in your code.

1) Keras part:

 model.compile(loss='mean_squared_error', 
               optimizer='adam', 
               metrics=['mean_squared_error'])

a) loss: In the Compilation section of the documentation here, you can see that:

A loss function is the objective that the model will try to minimize.

So this is actually used together with the optimizer to actually train the model

b) metrics: According to the documentation:

A metric function is similar to a loss function, except that the results from evaluating a metric are not used when training the model.

This is only used to report the metrics so that the used (you) can judge the performance of model. It does not impact how the model is trained.

2) Grid-search part:

scoring: Again, check the documentation

A single string or a callable to evaluate the predictions on the test set.

This is used to find the combination of parameters which you defined in param_grid which gives the best score.

They can very well (in most cases) be different, depending on what you want.

like image 78
Vivek Kumar Avatar answered Oct 06 '22 12:10

Vivek Kumar