TensorFlow
NotImplementedError
symbolic Tensor
numpy array conversion
LSTM error

NotImplementedError Cannot convert a symbolic Tensor lstm_2/strided_slice0 to a numpy array. T

Master System Design with Codemia

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

Introduction

This error happens when code tries to treat a symbolic TensorFlow value as if it were an eager value that already has concrete data. In Keras models, especially around LSTM layers, tensors inside the model graph are symbolic placeholders during model construction, so NumPy cannot consume them until the computation is actually executed.

What a Symbolic Tensor Really Is

When you build a Keras model with the functional API or with symbolic layer calls, TensorFlow is describing a computation graph. The tensors created at that stage are not ordinary arrays. They represent future values that will exist only when real input data flows through the model.

A simple example:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(10, 4))
4x = tf.keras.layers.LSTM(8)(inputs)
5outputs = tf.keras.layers.Dense(1)(x)
6
7model = tf.keras.Model(inputs, outputs)

Inside this model-building code, x is not a NumPy array. It is a symbolic tensor produced by the LSTM layer. If you try to hand it to NumPy during graph construction, TensorFlow raises the symbolic-tensor error.

The Most Common Cause: Mixing NumPy with Model Graph Code

The classic failure looks like this:

python
1import numpy as np
2import tensorflow as tf
3
4inputs = tf.keras.Input(shape=(10, 4))
5x = tf.keras.layers.LSTM(8)(inputs)
6
7bad = np.array(x)

NumPy expects actual values. x does not have actual values yet, so conversion is impossible.

Another common variation is using a NumPy function where a TensorFlow op should be used:

python
1import numpy as np
2import tensorflow as tf
3
4inputs = tf.keras.Input(shape=(10, 4))
5x = tf.keras.layers.LSTM(8)(inputs)
6
7bad = np.mean(x, axis=1)

The fix is to keep graph computations in TensorFlow:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(10, 4))
4x = tf.keras.layers.LSTM(8, return_sequences=True)(inputs)
5good = tf.reduce_mean(x, axis=1)

TensorFlow ops know how to operate on symbolic tensors during graph construction.

Convert to NumPy Only After Execution

If you actually want a NumPy array, first run the model or evaluate the tensor so it has concrete values.

python
1import tensorflow as tf
2import numpy as np
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Input(shape=(10, 4)),
6    tf.keras.layers.LSTM(8)
7])
8
9sample = np.random.rand(2, 10, 4).astype("float32")
10result = model(sample)
11
12print(type(result))
13print(result.numpy())

Here the model was executed with real input data, so the output is an eager tensor and .numpy() works.

That is the core rule: symbolic tensors exist during graph definition, concrete tensors exist during execution.

Put Custom Logic in TensorFlow-Compatible Layers

This error often appears when developers insert Python or NumPy code directly into the middle of model construction. If the logic belongs inside the model, write it with TensorFlow ops or wrap it in a custom layer.

python
1import tensorflow as tf
2
3class MeanOverTime(tf.keras.layers.Layer):
4    def call(self, inputs):
5        return tf.reduce_mean(inputs, axis=1)
6
7
8inputs = tf.keras.Input(shape=(10, 4))
9x = tf.keras.layers.LSTM(8, return_sequences=True)(inputs)
10x = MeanOverTime()(x)
11outputs = tf.keras.layers.Dense(1)(x)
12
13model = tf.keras.Model(inputs, outputs)

This keeps the entire computation graph-compatible.

If the logic truly must happen in plain NumPy, move it outside the symbolic graph and run it on actual data before model input or after model output.

Common Pitfalls

The biggest mistake is calling NumPy functions on symbolic tensors inside model-building code. If the value comes from Input, LSTM, Dense, or another Keras layer during graph construction, assume it is symbolic unless proven otherwise.

Another issue is confusing tensor.numpy() with something that works everywhere. .numpy() is for eager tensors with concrete values, not graph placeholders.

Developers also run into trouble when slicing or reshaping symbolic tensors with Python code that indirectly triggers NumPy conversion. Use TensorFlow slicing and TensorFlow math when the value is still inside the model graph.

Finally, do not try to "fix" the issue by forcing eager execution blindly. The real solution is to respect the boundary between symbolic model construction and runtime evaluation.

Summary

  • A symbolic tensor represents future computation, not concrete data.
  • NumPy cannot consume symbolic tensors during Keras graph construction.
  • Use TensorFlow ops instead of NumPy inside the model graph.
  • Convert to NumPy only after the model has executed on real input data.
  • If custom logic belongs inside the model, implement it with TensorFlow-compatible layers or functions.

Course illustration
Course illustration

All Rights Reserved.