TensorFlow
Machine Learning
Neural Networks
AI Development
Data Processing

Basic Tensorflow Question Input and Output Array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Most TensorFlow input and output confusion comes from one thing: shape. Your model does not care whether the source data started as a Python list, NumPy array, or tensor. It cares that the input shape matches the first layer and that the output shape matches the target format expected by the loss function.

Think in Batches, Features, and Targets

In TensorFlow, training data is usually arranged as one batch dimension plus one or more feature dimensions. For a simple tabular dataset with four samples and two features per sample, the input array has shape (4, 2).

python
1import numpy as np
2
3x = np.array([
4    [1.0, 2.0],
5    [2.0, 1.0],
6    [3.0, 5.0],
7    [4.0, 3.0],
8], dtype=np.float32)
9
10print(x.shape)
text
(4, 2)

The matching output array depends on the problem:

  • regression often uses shape (batch_size, 1)
  • binary classification often uses shape (batch_size, 1)
  • multi-class classification may use integer labels with shape (batch_size,) or one-hot labels with shape (batch_size, num_classes)

That means the correct output array is not universal. It depends on the final layer and the loss function.

A Minimal Regression Example

The next example predicts one numeric value from two input features. Notice how the model input shape is (2,), which means two features per sample, not two total samples.

python
1import numpy as np
2import tensorflow as tf
3
4x = np.array([
5    [1.0, 2.0],
6    [2.0, 1.0],
7    [3.0, 5.0],
8    [4.0, 3.0],
9], dtype=np.float32)
10
11y = np.array([[3.0], [3.0], [8.0], [7.0]], dtype=np.float32)
12
13model = tf.keras.Sequential([
14    tf.keras.layers.Input(shape=(2,)),
15    tf.keras.layers.Dense(8, activation="relu"),
16    tf.keras.layers.Dense(1)
17])
18
19model.compile(optimizer="adam", loss="mse")
20model.fit(x, y, epochs=50, verbose=0)
21
22prediction = model.predict(np.array([[5.0, 2.0]], dtype=np.float32), verbose=0)
23print(prediction.shape)
24print(prediction)

Here:

  • 'x has shape (4, 2)'
  • 'y has shape (4, 1)'
  • the prediction for one new sample has shape (1, 1)

That is the simplest way to think about input and output arrays: rows are samples, columns are features or target dimensions.

Classification Changes the Output Rules

For classification, the output layer and label encoding must agree. Suppose there are three classes. One common design is a softmax output with one-hot encoded targets.

python
1import numpy as np
2import tensorflow as tf
3
4x = np.array([
5    [0.1, 0.2],
6    [0.2, 0.1],
7    [0.9, 0.8],
8    [0.8, 0.9],
9    [0.4, 0.7],
10    [0.7, 0.4],
11], dtype=np.float32)
12
13labels = np.array([0, 0, 1, 1, 2, 2])
14y = tf.keras.utils.to_categorical(labels, num_classes=3)
15
16model = tf.keras.Sequential([
17    tf.keras.layers.Input(shape=(2,)),
18    tf.keras.layers.Dense(12, activation="relu"),
19    tf.keras.layers.Dense(3, activation="softmax")
20])
21
22model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
23model.fit(x, y, epochs=80, verbose=0)
24
25probs = model.predict(np.array([[0.85, 0.75]], dtype=np.float32), verbose=0)
26print(probs.shape)
27print(probs)

Now the output array shape is (batch_size, 3) because each sample produces three class probabilities. If you instead use SparseCategoricalCrossentropy, the targets can stay as integer labels with shape (batch_size,).

Arrays, NumPy, and Tensors Are Interchangeable at the Boundary

Keras accepts NumPy arrays directly, and it will convert them internally. TensorFlow tensors work too. For beginners, NumPy is often easier to print and inspect, while tensors become more important once you build tf.data pipelines or custom training logic.

The important rule is not the container type. The important rule is that the shapes and dtypes line up. A float input array and integer class labels are normal. A random mismatch between model output and target shape is not.

Common Pitfalls

  • Confusing the batch dimension with the feature dimension.
  • Using a final layer with shape (1,) but one-hot encoded labels for several classes.
  • Passing integer labels into categorical_crossentropy instead of sparse categorical cross-entropy.
  • Forgetting to convert data to numeric dtypes such as float32.
  • Looking only at the array values instead of printing shape before training.

Summary

  • In TensorFlow, input and output arrays are mainly a shape-matching problem.
  • Inputs are usually arranged as (batch_size, features) for tabular models.
  • Regression and binary classification often use output shape (batch_size, 1).
  • Multi-class classification usually uses either integer labels or one-hot arrays, depending on the loss.
  • Print shapes early and make sure the model, labels, and loss function agree.

Course illustration
Course illustration

All Rights Reserved.