0
0
TensorFlowml~5 mins

Validation split in TensorFlow

Choose your learning style9 modes available
Introduction

Validation split helps check how well a model learns by testing it on unseen data during training.

When you want to see if your model is improving without using the test data.
When you have limited data and want to use part of it to check model accuracy.
When tuning model settings to avoid overfitting.
When you want quick feedback on model performance during training.
Syntax
TensorFlow
model.fit(x_train, y_train, validation_split=0.2, epochs=10)

validation_split is a decimal between 0 and 1 showing the fraction of training data used for validation.

The split happens before training, so the last part of the data is used for validation.

Examples
Uses 10% of training data for validation during 5 epochs.
TensorFlow
model.fit(x_train, y_train, validation_split=0.1, epochs=5)
Uses 30% of training data for validation during 20 epochs.
TensorFlow
model.fit(x_train, y_train, validation_split=0.3, epochs=20)
Sample Model

This code trains a simple neural network on random data. It uses 20% of the training data as validation to check model performance during training.

TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np

# Create dummy data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))

# Build a simple model
model = models.Sequential([
    layers.Dense(16, activation='relu', input_shape=(20,)),
    layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train with validation split
history = model.fit(x_train, y_train, validation_split=0.2, epochs=3, batch_size=32, verbose=2)

# Print final validation accuracy
val_acc = history.history['val_accuracy'][-1]
print(f'Final validation accuracy: {val_acc:.4f}')
OutputSuccess
Important Notes

The validation split takes the last part of your data, so shuffle your data before training if order matters.

Validation data is not used to update model weights, only to check performance.

Summary

Validation split helps check model performance during training on unseen data.

It uses a fraction of training data as validation automatically.

Helps detect overfitting and tune model settings.