Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotImplementedError: Cannot convert a symbolic Tensor to a numpy array

The code below used to work last year, but updates in keras/tensorflow/numpy broke it. It now outputs the exception below. Does anyone know how to make it work again?

I'm using:

  • Tensorflow 2.4.1
  • Keras 2.4.3
  • Numpy 1.20.1
  • Python 3.9.1
import numpy as np
from keras.layers import LSTM, Embedding, Input, Bidirectional

dim = 30
max_seq_length = 40
vecs = np.random.rand(45,dim)

input_layer = Input(shape=(max_seq_length,))
embedding_layer = Embedding(len(vecs), dim, weights=[vecs], input_length=max_seq_length, trainable=False, name="layerA")(input_layer)
lstm_nobi = LSTM(max_seq_length, return_sequences=True, activation="linear", name="layerB")
lstm = Bidirectional(lstm_nobi, name="layerC")(embedding_layer)

Complete output of the script above: https://pastebin.com/DsQNWVwz

Shortened output:

2021-02-10 17:51:13.037468: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-10 17:51:13.037899: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-02-10 17:51:13.038418: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Traceback (most recent call last):
  File "/run/media/volker/DATA/configruns/load/./test.py", line 13, in <module>
    lstm = Bidirectional(lstm_nobi, name="layerC")(embedding_layer)
  ... omitted, see pastebin ...
  File "/usr/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 852, in __array__
    raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (layerC/forward_layerB/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
like image 774
Volker Weißmann Avatar asked Feb 10 '21 17:02

Volker Weißmann


People also ask

Can tensors be converted to NumPy arrays?

To convert back from tensor to numpy array you can simply run . eval() on the transformed tensor.

How do I convert NumPy to tensor?

a NumPy array is created by using the np. array() method. The NumPy array is converted to tensor by using tf. convert_to_tensor() method.

Which of the following will be used to convert TensorFlow tensor to NumPy Ndarray?

To convert a tensor t to a NumPy array in TensorFlow version 2.0 and above, use the t. numpy() built-in method.

What is tensor NumPy?

Tensor can be defined as a data container. It can be thought of as a multi-dimensional array. Numpy np. array can be used to create tensor of different dimensions such as 1D, 2D, 3D etc.


2 Answers

Installing numpy 1.19.5 works for me even with Python 3.9

pip install -U numpy==1.19.5

My environment is Windows and since I do not have Visual C++ Compiler installed, I ride on third party whl file installation

pip install -U https://mirrors.aliyun.com/pypi/packages/bc/40/d6f7ba9ce5406b578e538325828ea43849a3dfd8db63d1147a257d19c8d1/numpy-1.19.5-cp39-cp39-win_amd64.whl#sha256=0eef32ca3132a48e43f6a0f5a82cb508f22ce5a3d6f67a8329c81c8e226d3f6e
like image 101
Synru Avatar answered Oct 24 '22 04:10

Synru


Solution: Use Python 3.8, because Python 3.9 is not supported by Tensorflow.

like image 7
Volker Weißmann Avatar answered Oct 24 '22 02:10

Volker Weißmann