#K-fold validation method

1 messages · Page 1 of 1 (latest)

smoky bluff
#

hi.
I couldn't understand the k-fold method in deep learning.
I know what is happening.
I mean look at this code :

k = 3
num_validation_samples = len(data) // k
np.random.shuffle(data)
val_scores = []
for fold in range(k):
  print(f'Doing fold {fold}')
  val_data = data[num_validation_samples * fold:
                  num_validation_samples * (fold + 1)]
  training_data = np.concatenate(
      [data[:num_validation_samples * fold],
      data[num_validation_samples * (fold + 1):]]
      )
  model = get_model()
  x_train = np.concatenate([np.expand_dims(training_data[:,0],1), np.expand_dims(training_data[:,1],1)], axis=1)
  y_train = np.expand_dims(training_data[:,2], 1)
  model.fit(x_train, y_train,
            batch_size=2**5,
            epochs=20,
            verbose=0)
  x_test = np.concatenate([np.expand_dims(val_data[:,0],1), np.expand_dims(val_data[:,1],1)], axis=1)
  y_test = np.expand_dims(val_data[:,2], 1)
  val_score, val_acc = model.evaluate(x_test, y_test, verbose=0)
  val_scores.append(val_acc)

val_score = np.mean(val_scores)

print(val_score)
model = get_model()
history = model.fit(X, y,
          epochs=20,
          batch_size=2**5,
          verbose=0)
test_score = model.evaluate(X_test,y_test)

in each iteration, we recreate the model. We just see the val_score at the end. and after that, we again recreate the model with random parameters and retrain and reevaluate it. why?

waxen zephyr
smoky bluff
#

well, what is the purpose of doing this ? I mean we want a model that generalizes well.

waxen zephyr
# smoky bluff well, what is the purpose of doing this ? I mean we want a model that generalize...

SK Learn has a very good explanation on what is cross validation is it used: https://scikit-learn.org/stable/modules/cross_validation.html
Essentially, we want to make sure that the current parameters actually perform generally find throughout different folds. Every fold has a different train and validation set, and you need train from scratch because you shouldn't be leaking validation into your training set. It's a good way to measure performance, although impractical for larger deep learning models