fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"
MIT License
17.95k stars 8.48k forks source link

Bug on code for k-fold cross val #223

Open jboverio opened 1 year ago

jboverio commented 1 year ago

This code (k-fold cross val) misses a parenthesis on np.concatenate:

'''k = 3 num_validation_samples = len(data) // k np.random.shuffle(data) validation_scores = [] for fold in range(k): validation_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() ❸ model.fit(training_data, ...) validation_score = model.evaluate(validation_data, ...) validation_scores.append(validation_score) validation_score = np.average(validation_scores) ❹ model = get_model() ❺ model.fit(data, ...) ❺ test_score = model.evaluate(test_data, ...) '''

The previous code (held out validation) was correct, the k-fold code misses a "(" and ")", the correct code should look like:

'''training_data = np.concatenate( ( ❷ data[:num_validation_samples fold], ❷ data[num_validation_samples (fold + 1):]) ) ❷'''

As documentation for np.concatenate states:

'''a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) p.concatenate((a, b), axis=None)'''

Kind regards

ifond commented 1 year ago

​ I have received your E-mail——Steven Lee

jboverio commented 1 year ago

Also would be great to make clear on the code too that it is supressing the k-fold for the labels