TensorFlow
InvalidArgumentError
debugging
machine learning
Python

InvalidArgumentError input_10 is both fed and fetched

Master System Design with Codemia

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

Introduction

The TensorFlow error saying a tensor is both fed and fetched means one tensor is being treated as input and output in the same session call. This usually appears in TensorFlow graph execution code where run arguments are assembled dynamically. The fix is to separate placeholder inputs from computed outputs and enforce this boundary in wrapper code.

Understand Feed Versus Fetch Roles

In graph mode, feed_dict supplies values for placeholders or feedable tensors. Fetch list requests tensors to compute and return.

If the same tensor appears in both, TensorFlow raises an InvalidArgumentError.

A minimal graph example:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, shape=[None, 1], name="input_10")
6y = x * 2
7
8with tf.compat.v1.Session() as sess:
9    result = sess.run(y, feed_dict={x: [[1.0], [2.0]]})
10    print(result)

This works because x is fed and y is fetched.

How the Error Usually Appears

The error often appears in shared helper code, not directly where tensors are defined. A wrapper may accidentally merge input tensors and output tensors into one list.

Example anti-pattern:

python
1# pseudo pattern
2fetches = [input_tensor, output_tensor]
3feed_dict = {input_tensor: batch}
4# same input_tensor now in both sides

The fix is not only at one run call. It requires cleaning the run argument construction path.

Build Explicit Run Interfaces

Avoid generic run wrappers that accept arbitrary tensor lists. Use typed helpers with fixed feed and fetch contracts.

python
def run_inference(sess, x_placeholder, y_tensor, batch):
    return sess.run(y_tensor, feed_dict={x_placeholder: batch})

When interfaces are explicit, feed and fetch overlap is harder to introduce.

Add Runtime Assertions in Legacy Code

If you cannot refactor immediately, add validation before calling session run.

python
1def validate_feed_fetch(feed_dict, fetches):
2    fetch_set = set(fetches if isinstance(fetches, (list, tuple)) else [fetches])
3    for fed in feed_dict.keys():
4        if fed in fetch_set:
5            raise ValueError(f"Tensor fed and fetched: {fed.name}")

This produces clearer failures than deep TensorFlow stack traces.

Inspect Graph and Tensor Naming

Large graphs with reused names can hide mistakes. Print tensor names and operation types while debugging.

python
for op in tf.compat.v1.get_default_graph().get_operations()[:20]:
    print(op.name, op.type)

Clear naming reduces confusion in feed and fetch wiring.

Migration Path to TensorFlow 2

If feasible, migrate graph-mode execution to eager or tf.function. This class of error is less common in modern execution style.

python
1import tensorflow as tf
2
3x = tf.constant([[1.0], [2.0]])
4y = x * 2
5print(y.numpy())

Even partial migration can simplify debugging and reduce session wrapper complexity.

Test for Regression

Add unit tests around wrapper behavior, not only model math.

python
def test_feed_fetch_separation():
    # placeholder and output should be distinct roles
    assert True

In real tests, assert that feed keys match expected placeholders and fetch list excludes those placeholders.

Practical Debug Checklist

When this error appears:

  1. Identify tensor named in error message.
  2. Search where it enters fetch list.
  3. Search where it enters feed dictionary.
  4. Inspect wrapper functions that compose run arguments.
  5. Add assertion guard and rerun.

This approach finds root cause faster than random graph edits.

Common Pitfalls

  • Building feed and fetch collections in mutable shared structures.
  • Passing placeholders through generic output lists.
  • Mixing TensorFlow 1 graph wrappers with TensorFlow 2 style code.
  • Reusing variable names that hide tensor role boundaries.
  • Debugging only failing call site and not wrapper layers.

Summary

  • The error means one tensor is used as both input and output in one run.
  • Keep placeholders in feeds and computed tensors in fetches.
  • Refactor generic wrappers into explicit run interfaces.
  • Add runtime guards in legacy code to catch overlap early.
  • Prefer modern TensorFlow execution models when migration is possible.

Course illustration
Course illustration

All Rights Reserved.