Skip to content

Adding local Random state to fit_loop #12357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions keras/engine/training_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def fit_loop(model, fit_function, fit_inputs,
initial_epoch=0,
steps_per_epoch=None,
validation_steps=None,
validation_freq=1):
validation_freq=1,
seed=113):
Copy link
Collaborator

@fchollet fchollet Mar 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use default seeds. This would dramatically reduce randomness (effectively remove randomness since there is no user-facing way to set the seed).

"""Abstract fit function for `fit_function(fit_inputs)`.

Assumes that fit_function returns a list, labeled by out_labels.
Expand Down Expand Up @@ -143,6 +144,8 @@ def fit_loop(model, fit_function, fit_inputs,
model._feed_targets +
model._feed_sample_weights)
indices_for_conversion_to_dense = []
rng = np.random.RandomState(seed)

for i in range(len(feed)):
if issparse(fit_inputs[i]) and not K.is_sparse(feed[i]):
indices_for_conversion_to_dense.append(i)
Expand Down Expand Up @@ -180,7 +183,7 @@ def fit_loop(model, fit_function, fit_inputs,
if shuffle == 'batch':
index_array = batch_shuffle(index_array, batch_size)
elif shuffle:
np.random.shuffle(index_array)
rng.shuffle(index_array)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version already does not modify the global behavior of np.random. The purpose of this PR is not clear to me.


batches = make_batches(num_train_samples, batch_size)
for batch_index, (batch_start, batch_end) in enumerate(batches):
Expand Down