Before a model can learn anything, your data has to be in the right form. Keras works with tensors: grids of numbers with a particular shape. In this module you'll learn what tensors and shapes are, how to turn tables, images and text into them, and how to scale, encode and split data so your models train well.
- Level: Beginner
- Time: about 35 minutes
- Needs: Module 1, basic Python
By the end of this module you will be able to
- Explain what a tensor is, and describe its shape, rank and data type
- Recognise the standard shapes for tables, images, time series and text
- Explain the batch dimension and set keras.Input correctly
- Convert a pandas DataFrame into arrays Keras can use
- Encode categories with one-hot encoding and integer labels
- Scale data with a Normalization layer, without data leakage
- Split data into training, validation and test sets
- Show with real code why scaling matters
2.1What is a tensor?
Neural networks only understand numbers. A tensor is simply a container of numbers arranged in a grid, with any number of dimensions. You've already met most of them:
Every tensor has three properties you'll check again and again:
| Property | Meaning | Example |
|---|---|---|
| Shape | The size of each dimension | (2000, 7): 2,000 rows and 7 columns |
| Rank | How many dimensions there are | (2000, 7) has rank 2 |
| Data type (dtype) | The kind of number stored | float32 (decimals) is Keras's default |
Tensors and NumPy arrays
If you know NumPy, a tensor is almost exactly like a NumPy array. Keras happily accepts NumPy arrays and pandas data converted with .to_numpy(), so in practice you'll mostly prepare data with NumPy and pandas and pass it straight to Keras.
2.2Common data shapes
Different kinds of data have standard shapes. Learn these and most Keras shape errors disappear.
| Data | Shape | Example |
|---|---|---|
| Table (spreadsheet, CSV) | (samples, features) | 2,000 customers × 7 features → (2000, 7) |
| Greyscale images | (samples, height, width, 1) | 1,000 images of 28 × 28 → (1000, 28, 28, 1) |
| Colour images | (samples, height, width, 3) | 1,000 photos of 224 × 224 → (1000, 224, 224, 3) |
| Time series | (samples, time steps, features) | 500 sequences of 30 days × 3 readings → (500, 30, 3) |
| Text | (samples, tokens) | 1,000 sentences of 40 tokens → (1000, 40) |
2.3The batch dimension and keras.Input
In every shape above, the first number is the number of examples. Keras calls this the batch dimension. When you tell Keras what your data looks like with keras.Input, you leave it out and describe one example only:
keras.Input(shape=(7,)). Keras then accepts any number of examples.That comma in (7,) matters: in Python, (7) is just the number 7, while (7,) is a tuple containing 7. When Keras prints shapes, you'll see the batch dimension shown as None, for example (None, 7), meaning "any number of examples".
Try it: shape explorer
Notice how quickly image data grows: 10,000 colour photos at 224 × 224 is over 1.5 billion numbers, about 5.6 GB. That's why image models are trained in small batches, a few dozen images at a time.
2.4Turning categories into numbers
Networks can't read words like "monthly" or "annual". Categories must become numbers, and how you do it matters.
Input features: one-hot encoding
If you replaced monthly, annual and two-year with 0, 1 and 2, the network would think "two-year" is twice "annual", which makes no sense. Instead, use one-hot encoding: one column per category, with a 1 in the matching column.
| contract | contract_annual | contract_monthly | contract_two_year |
|---|---|---|---|
| monthly | 0 | 1 | 0 |
| annual | 1 | 0 | 0 |
| two_year | 0 | 0 | 1 |
With pandas, one line does it: pd.get_dummies(df, columns=["contract"], dtype=float). Keras also has layers that do this inside the model, such as layers.StringLookup, which you'll meet with text in Module 9.
Labels: two options for classes
| Label format | Looks like | Use with this loss (Module 4) |
|---|---|---|
| Integer labels | [2, 0, 1] | "sparse_categorical_crossentropy" |
| One-hot labels | [[0,0,1], [1,0,0], [0,1,0]] | "categorical_crossentropy" |
Integer labels are simpler and use less memory, so they're usually the better choice. If you need one-hot labels, use keras.utils.to_categorical(y).
2.5Scaling: put features on the same footing
In our customer data, age runs from 18 to 80, but data used runs into the tens of thousands of megabytes. Neural networks learn slowly and badly when features are on wildly different scales: the big numbers drown out the small ones, and training becomes unstable. The fix is scaling.
| Method | What it does | When to use it |
|---|---|---|
| Standardisation | Subtracts the mean and divides by the standard deviation, so most values fall between about −3 and 3 | The default choice for tables and time series |
| Min-max scaling | Squeezes values into 0 to 1 | Images: divide pixel values by 255 |
The Keras way: a Normalization layer
Keras can standardise your data inside the model, with a Normalization layer. You call adapt() once to let it learn each feature's mean and spread:
norm = layers.Normalization()
norm.adapt(X_train) # learn means and spreads from the TRAINING data
model = keras.Sequential([
keras.Input(shape=(7,)),
norm, # scaling happens here, automatically
layers.Dense(16, activation="relu"),
layers.Dense(1, activation="sigmoid"),
])
The big advantage: the scaling is saved as part of the model. When you use the model later on new data, you can feed it raw numbers and it scales them exactly the same way. No separate scaler to forget.
Avoid data leakage: split first, then scale
Always call adapt() on the training data only. If you learn the means from the whole dataset, including the test set, information about the test set leaks into the model and your test score will look better than it really is.
2.6Training, validation and test sets
To know whether a model works on new data, you must test it on data it never learned from. Most projects use three sets:
| Set | Typical share | Used for |
|---|---|---|
| Training set | 60 to 80% | Learning the weights |
| Validation set | 10 to 20% | Checking progress during training, and choosing settings (Module 5) |
| Test set | 10 to 20% | One final, honest score at the very end |
Use scikit-learn's train_test_split to make the test set, with stratify=y so each set has the same share of each class. Keras can then carve out the validation set for you with model.fit(..., validation_split=0.2).
Time series are different
For data over time, never shuffle before splitting. Train on the past and test on the future, or the model gets to "see the future" during training.
2.7Preparing real data in Python
Let's put it all together. We'll inspect some tensors, build a realistic table of 2,000 broadband customers, encode it, split it, scale it with a Normalization layer, and then prove that scaling matters by training the same model with and without it.
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import numpy as np
import pandas as pd
import keras
from keras import layers
from sklearn.model_selection import train_test_split
# ---------- 1. Tensors: shape, rank and dtype ----------
scalar = keras.ops.convert_to_tensor(7.0)
vector = keras.ops.convert_to_tensor([1.0, 2.0, 3.0])
matrix = keras.ops.convert_to_tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
images = keras.ops.zeros((32, 28, 28, 1)) # 32 greyscale 28x28 images
for name, t in [("scalar", scalar), ("vector", vector),
("matrix", matrix), ("images", images)]:
dtype = keras.backend.standardize_dtype(t.dtype)
print(f"{name:7} shape={str(tuple(t.shape)):16} rank={len(t.shape)} dtype={dtype}")
# ---------- 2. A realistic table of customers ----------
rng = np.random.default_rng(0)
n = 2000
df = pd.DataFrame({
"age": rng.integers(18, 80, n),
"monthly_spend": rng.gamma(2, 25, n).round(2), # in pounds
"support_calls": rng.poisson(1.5, n),
"data_used_mb": rng.gamma(3, 4000, n).round(0), # big numbers!
"contract": rng.choice(["monthly", "annual", "two_year"], n, p=[.5, .3, .2]),
})
risk = (0.03 * df.monthly_spend + 0.6 * df.support_calls - 0.02 * df.age
- 0.0001 * df.data_used_mb
+ df.contract.map({"monthly": 1.5, "annual": 0, "two_year": -1.5}))
df["churned"] = (risk + rng.normal(0, 1, n) > 1.0).astype(int)
print("\n", df.head(3).to_string(index=False), sep="")
# ---------- 3. Turn the table into numbers ----------
X = pd.get_dummies(df.drop(columns="churned"), columns=["contract"], dtype=float)
y = df["churned"].to_numpy()
print("\nColumns:", list(X.columns))
X = X.to_numpy(dtype="float32") # Keras likes float32
print("X shape:", X.shape, "| y shape:", y.shape)
# ---------- 4. Split BEFORE scaling ----------
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
# ---------- 5. Scale inside the model with a Normalization layer ----------
norm = layers.Normalization()
norm.adapt(X_train) # learn mean and spread from TRAINING data only
print("\nFirst 3 means learned:", np.asarray(norm.mean).ravel()[:3].round(1))
print("Scaled first row: ", np.asarray(norm(X_train[:1])).round(2))
# ---------- 6. Does scaling matter? Train with and without it ----------
def make_model(scale):
keras.utils.set_random_seed(0)
model = keras.Sequential([keras.Input(shape=(X.shape[1],))]
+ ([norm] if scale else [])
+ [layers.Dense(16, activation="relu"),
layers.Dense(1, activation="sigmoid")])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(X_train, y_train, epochs=30, batch_size=32, verbose=0)
return model.evaluate(X_test, y_test, verbose=0)[1]
print(f"\nTest accuracy without scaling: {make_model(False):.3f}")
print(f"Test accuracy with scaling: {make_model(True):.3f}")
print(f"Always guessing 'stays': {1 - y_test.mean():.3f}")
scalar shape=() rank=0 dtype=float32 vector shape=(3,) rank=1 dtype=float32 matrix shape=(2, 3) rank=2 dtype=float32 images shape=(32, 28, 28, 1) rank=4 dtype=float32 age monthly_spend support_calls data_used_mb contract churned 70 44.43 0 7391.0 annual 0 57 160.95 1 6699.0 monthly 1 49 10.79 1 1361.0 annual 0 Columns: ['age', 'monthly_spend', 'support_calls', 'data_used_mb', 'contract_annual', 'contract_monthly', 'contract_two_year'] X shape: (2000, 7) | y shape: (2000,) First 3 means learned: [49.2 51.6 1.5] Scaled first row: [[ 0.44 -0.36 -0.43 -0.55 -0.67 1. -0.49]] Test accuracy without scaling: 0.685 Test accuracy with scaling: 0.863 Always guessing 'stays': 0.547
What the output tells us
- Shapes and ranks match Figure 1: a scalar has shape
(), and a batch of 32 greyscale images has shape(32, 28, 28, 1), rank 4. Everything isfloat32by default. - One-hot encoding turned the
contractcolumn into three 0/1 columns, giving 7 features andX.shape = (2000, 7). - The Normalization layer learned the means from the training data (average age 49.2, average spend £51.60), and turned each row into small numbers centred on 0.
- Scaling made a big difference: 68.5% accuracy without it and 86.3% with it. Without scaling, the huge "data used" numbers drowned out everything else. Always compare with the "always guess the most common answer" baseline: here, 54.7%.
SummaryKey takeaways
- A tensor is a grid of numbers with a shape, a rank (number of dimensions) and a dtype (usually float32).
- Standard shapes: tables
(samples, features), images(samples, height, width, channels), time series(samples, steps, features). - The first dimension is the batch dimension.
keras.Input(shape=...)describes one example only. - Use one-hot encoding for category features; use integer labels with sparse categorical cross-entropy.
- Scale your features. A
Normalizationlayer does it inside the model and is saved with it. - Split first, then scale, using the training data only, to avoid data leakage.
- Use training, validation and test sets, and compare with a simple baseline.
Check your understanding
Your Keras roadmap
- 01Introduction to KerasWhat Keras is, setup and your first model
- 02Tensors and Data for KerasYou are here
- 03The Sequential API and Dense LayersStacking layers into a model
- 04Compiling ModelsLosses, optimisers and metrics
- 05Training and Evaluatingfit(), validation and callbacks
- 06The Functional APIMultiple inputs, outputs and branches
- 07RegularisationDropout, batch normalisation and early stopping
- 08Image Classification with KerasCNNs and data augmentation
- 09Text and Sequences with KerasEmbeddings, LSTMs and text classification
- 10Pretrained Models, Saving and DeploymentFine-tuning and sharing your models
Next module
The Sequential API and Dense Layers