Skip to content

Commit

Permalink
fix order when creating validation from train (keras-team#279)
Browse files Browse the repository at this point in the history
* fix order when creating validation from train

* De-duplicate the data handling.

Co-authored-by: Mark Daoust <markdaoust@google.com>
  • Loading branch information
amitport and MarkDaoust authored Oct 12, 2020
1 parent f5701ff commit 11bb520
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions guides/writing_a_training_loop_from_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = np.reshape(x_train, (-1, 784))
x_test = np.reshape(x_test, (-1, 784))

# Reserve 10,000 samples for validation.
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]

# Prepare the training dataset.
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)

# Prepare the validation dataset.
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_dataset = val_dataset.batch(batch_size)

"""
Here's our training loop:
Expand Down Expand Up @@ -152,20 +164,6 @@
train_acc_metric = keras.metrics.SparseCategoricalAccuracy()
val_acc_metric = keras.metrics.SparseCategoricalAccuracy()

# Prepare the training dataset.
batch_size = 64
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)

# Prepare the validation dataset.
# Reserve 10,000 samples for validation.
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_dataset = val_dataset.batch(64)

"""
Here's our training & evaluation loop:
"""
Expand Down

0 comments on commit 11bb520

Please sign in to comment.