9

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)
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
Stupid420
  • 1,347
  • 3
  • 19
  • 44

1 Answers1

7

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.

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • Is there any reason to use a different metric than loss? – Ben Oct 28 '19 at 08:23
  • Take a classification problem. Your loss will very likely be the categorical cross-entropy but in the end you'll want to know if your model gives you the right answer, so your metric will be the accuracy of your model. Categorical cross entropy and accuracy are correlated but you still need both to optimize and evaluate your model. – Mathieu Sep 17 '20 at 14:46