TensorFlow
feed_dict
debugging
machine learning
neural networks

Issue feeding a list into feed_dict in TensorFlow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow 1.x, feed_dict maps placeholder tensors to values for session.run(). The most common issue is a shape mismatch — feeding a Python list with the wrong dimensions for the placeholder's expected shape. The fix is to reshape the data to match the placeholder's dimensions exactly, typically converting lists to NumPy arrays with the correct shape. In TensorFlow 2.x, feed_dict is replaced by direct function calls with eager execution.

The Problem

python
1import tensorflow.compat.v1 as tf
2tf.disable_eager_execution()
3
4# Placeholder expects shape (None, 3) — batch of 3-element vectors
5x = tf.placeholder(tf.float32, shape=[None, 3])
6y = x * 2
7
8with tf.Session() as sess:
9    # WRONG — flat list, shape (3,) not (None, 3)
10    result = sess.run(y, feed_dict={x: [1.0, 2.0, 3.0]})
11    # ValueError: Cannot feed value of shape (3,) for Tensor
12    # 'Placeholder:0', which has shape '(?, 3)'

The placeholder expects a 2D tensor (batch dimension + feature dimension), but a flat list is 1D.

Fix: Correct the Shape

python
1import numpy as np
2import tensorflow.compat.v1 as tf
3tf.disable_eager_execution()
4
5x = tf.placeholder(tf.float32, shape=[None, 3])
6y = x * 2
7
8with tf.Session() as sess:
9    # Fix 1: Nested list (2D)
10    result = sess.run(y, feed_dict={x: [[1.0, 2.0, 3.0]]})
11    print(result)  # [[2.0, 4.0, 6.0]]
12
13    # Fix 2: NumPy array with correct shape
14    data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
15    result = sess.run(y, feed_dict={x: data})
16    print(result)  # [[2.0, 4.0, 6.0], [8.0, 10.0, 12.0]]
17
18    # Fix 3: Reshape a flat array
19    flat = np.array([1.0, 2.0, 3.0])
20    result = sess.run(y, feed_dict={x: flat.reshape(1, -1)})
21    print(result)  # [[2.0, 4.0, 6.0]]

Feeding Multiple Placeholders

python
1x = tf.placeholder(tf.float32, shape=[None, 784])
2y_true = tf.placeholder(tf.float32, shape=[None, 10])
3
4# Model
5logits = tf.layers.dense(x, 10)
6loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=logits))
7
8with tf.Session() as sess:
9    sess.run(tf.global_variables_initializer())
10
11    # Feed both placeholders
12    batch_x = np.random.randn(32, 784)  # 32 images, 784 pixels
13    batch_y = np.eye(10)[np.random.randint(0, 10, 32)]  # 32 one-hot labels
14
15    loss_val = sess.run(loss, feed_dict={
16        x: batch_x,
17        y_true: batch_y
18    })
19    print(f"Loss: {loss_val}")

Common Shape Mismatches

python
1# Placeholder: shape=[None, 1] — expects 2D column vector
2x = tf.placeholder(tf.float32, shape=[None, 1])
3
4# WRONG — shape (5,)
5data = [1.0, 2.0, 3.0, 4.0, 5.0]
6sess.run(y, feed_dict={x: data})  # Error
7
8# CORRECT — shape (5, 1)
9data = np.array([[1.0], [2.0], [3.0], [4.0], [5.0]])
10sess.run(y, feed_dict={x: data})  # Works
11
12# Or reshape
13data = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).reshape(-1, 1)
python
1# Placeholder: shape=[None, 28, 28, 1] — expects 4D image tensor
2x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
3
4# WRONG — shape (28, 28)
5img = np.random.randn(28, 28)
6sess.run(y, feed_dict={x: img})  # Error
7
8# CORRECT — shape (1, 28, 28, 1)
9img = np.random.randn(28, 28).reshape(1, 28, 28, 1)
10sess.run(y, feed_dict={x: img})  # Works
11
12# Or use np.expand_dims
13img = np.random.randn(28, 28)
14img = np.expand_dims(img, axis=(0, -1))  # Add batch and channel dims

Feeding Scalar Values

python
1# Placeholder: shape=[] (scalar)
2learning_rate = tf.placeholder(tf.float32, shape=[])
3
4# CORRECT — feed a Python number
5sess.run(train_op, feed_dict={learning_rate: 0.001})
6
7# WRONG — feed a list or array (shape mismatch)
8sess.run(train_op, feed_dict={learning_rate: [0.001]})  # Error

TensorFlow 2.x: No More feed_dict

TensorFlow 2.x uses eager execution by default — no placeholders or feed_dict:

python
1import tensorflow as tf
2
3# TF2 — direct computation
4x = tf.constant([[1.0, 2.0, 3.0]])
5y = x * 2
6print(y.numpy())  # [[2. 4. 6.]]
7
8# TF2 with tf.function
9@tf.function
10def compute(x):
11    return x * 2
12
13result = compute(tf.constant([[1.0, 2.0, 3.0]]))
14print(result.numpy())
15
16# TF2 with Keras model
17model = tf.keras.Sequential([tf.keras.layers.Dense(10)])
18data = np.random.randn(32, 784).astype(np.float32)
19output = model(data)  # Direct call, no session or feed_dict

Migrating from feed_dict to TF2

python
1# TF1 pattern
2x = tf.placeholder(tf.float32, [None, 784])
3y = tf.layers.dense(x, 10)
4with tf.Session() as sess:
5    sess.run(tf.global_variables_initializer())
6    result = sess.run(y, feed_dict={x: np.random.randn(1, 784)})
7
8# TF2 equivalent
9model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(784,))])
10result = model.predict(np.random.randn(1, 784))

Common Pitfalls

  • Shape (n,) vs (n, 1) vs (1, n): A 1D array (n,) is not the same as a column vector (n, 1) or row vector (1, n). Placeholders with shape=[None, 1] require 2D input. Use .reshape(-1, 1) to add the second dimension.
  • Feeding Python lists instead of NumPy arrays: Python lists work but are slower because TensorFlow must convert them internally. For large datasets, always convert to NumPy arrays before feeding.
  • Forgetting the batch dimension: Most placeholders include a batch dimension (None). A single sample must still have the batch dimension: shape (1, 784) not (784,). Use np.expand_dims(data, axis=0).
  • Type mismatch: If the placeholder expects tf.float32 but you feed int data, TensorFlow may raise an error or silently cast. Ensure types match with data.astype(np.float32).
  • Using feed_dict in TF2: TensorFlow 2.x does not use sessions or placeholders. If you are starting a new project, use Keras models and eager execution instead of the TF1 feed_dict pattern.

Summary

  • feed_dict errors are almost always shape mismatches — check placeholder shape vs data shape
  • Add the batch dimension to single samples: data.reshape(1, -1) or np.expand_dims(data, 0)
  • Use NumPy arrays instead of Python lists for better performance
  • Match data types: np.float32 for tf.float32 placeholders
  • In TensorFlow 2.x, use eager execution and Keras models instead of feed_dict
  • Use tensor.shape and data.shape to debug dimension mismatches

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.