Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedError: Cannot convert a symbolic Tensor (truediv_2:0) to a numpy array #38836

Closed
nbro opened this issue Apr 23, 2020 · 12 comments
Closed
Assignees
Labels
comp:ops OPs related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.1 for tracking issues in 2.1 release type:bug Bug

Comments

@nbro
Copy link

nbro commented Apr 23, 2020

System information

  • Have I written custom code: Yes
  • OS Platform and Distribution: Mac OS Catalina
  • TensorFlow installed from: binary
  • TensorFlow version (use command below): 2.1.0
  • Python version: 3.7.0

Describe the current behavior

I get the following error

NotImplementedError: Cannot convert a symbolic Tensor (truediv_2:0) to a numpy array.

When executing the following code

import tensorflow as tf
import tensorflow_probability as tfp

tf.config.experimental_run_functions_eagerly(True)


def get_mnist_data(normalize=True, categorize=True):
    img_rows, img_cols = 28, 28
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

    if tf.keras.backend.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    if normalize:
        x_train /= 255
        x_test /= 255

    if categorize:
        y_train = tf.keras.utils.to_categorical(y_train)
        y_test = tf.keras.utils.to_categorical(y_test)

    return x_train, y_train, x_test, y_test, input_shape


def get_model(input_shape, num_classes=10):
    model = tf.keras.Sequential()
    model.add(tfp.layers.Convolution2DFlipout(6, input_shape=input_shape, kernel_size=3, padding="SAME",
                                              activation=tf.nn.relu))
    model.add(tf.keras.layers.Flatten())
    model.add(tfp.layers.DenseFlipout(num_classes))
    return model


def train():
    x_train, y_train, x_test, y_test, input_shape = get_mnist_data()

    batch_size = 64

    model = get_model(input_shape)

    model.summary()

    model.compile(loss="categorical_crossentropy")

    model.fit(x_train, y_train, batch_size=batch_size, epochs=1)


if __name__ == '__main__':
    train()

This error is caused by the statement tf.config.experimental_run_functions_eagerly(True). However, if I remove that statement, I get another well known, older and extremely annoying problem/bug that is described in this other issue: #33729 that doesn't allow me to do anything for my work.

Describe the expected behavior

NO BUG or ERROR.

See also #38775 and https://stackoverflow.com/q/61388919/3924118.

@Saduf2019
Copy link
Contributor

@nbro
i ran the code shared, please find the gist here

@Saduf2019 Saduf2019 added the stat:awaiting response Status - Awaiting response from author label Apr 23, 2020
@nbro
Copy link
Author

nbro commented Apr 23, 2020

@Saduf2019 You are not using TensorFlow 2.1.0 in that gist.

The new versions of TF are even buggier, so I would like to avoid discussions related to newer versions now (unless they solve my problems, which would be like a miracle).

@Saduf2019
Copy link
Contributor

@nbro
the error on 2.1 is as per this gist

@nbro
Copy link
Author

nbro commented Apr 23, 2020

@Saduf2019 You're using tensorflow-probability 0.10.0rc0 in that gist. Use TFP 0.9.0 (the last stable version).

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Apr 25, 2020
@Saduf2019
Copy link
Contributor

Saduf2019 commented Apr 27, 2020

@nbro
I am able to replicate this issue with the specifications requested please find the gist here

I also ran the code shared by you on nightly and do not face any errros, please find the gist here for the same

@Saduf2019 Saduf2019 added comp:ops OPs related issues stat:awaiting response Status - Awaiting response from author labels Apr 27, 2020
@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

@google-ml-butler google-ml-butler bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label May 5, 2020
@google-ml-butler
Copy link

Closing as stale. Please reopen if you'd like to work on this further.

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@Ramkumar47
Copy link

Hi, I am also getting the exact same issue, but sorry for not sharing the code as it is on a project.
Is there any solution for this?
i am getting this error when eager execution is enabled, but i can get it to work when it is disabled,
i need eager execution for debugging my code.

@alik604
Copy link

alik604 commented Feb 19, 2021

so sad that he got ghosted...
I've got the same error

@Saduf2019
Copy link
Contributor

@Ramkumar47

Please create a new issue as this issue is in closed status.

@yparam98
Copy link

yparam98 commented Feb 24, 2021

@Ramkumar47 Any luck? Getting the exact same issue with this:

data_augmentation = keras.Sequential(
    [
        layers.experimental.preprocessing.RandomFlip("horizontal",
                                                     input_shape=(img_height,
                                                                  img_width,
                                                                  3)),
        layers.experimental.preprocessing.RandomRotation(0.1),
        layers.experimental.preprocessing.RandomZoom(0.1),
    ]
)

Error Trace:

Traceback (most recent call last):
  File "/home/yathavan/Documents/python_whiteboard/DRAFT_cvproject/flowers/flowers.py", line 136, in <module>
    data_augmentation = keras.Sequential(
  File "/usr/lib/python3.9/site-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/sequential.py", line 144, in __init__
    self.add(layer)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/sequential.py", line 223, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 951, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1090, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 822, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 863, in _infer_output_signature
    outputs = call_fn(inputs, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/preprocessing/image_preprocessing.py", line 866, in call
    output = control_flow_util.smart_cond(training, random_rotated_inputs,
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/utils/control_flow_util.py", line 114, in smart_cond
    return smart_module.smart_cond(
  File "/usr/lib/python3.9/site-packages/tensorflow/python/framework/smart_cond.py", line 54, in smart_cond
    return true_fn()
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/preprocessing/image_preprocessing.py", line 861, in random_rotated_inputs
    get_rotation_matrix(angles, img_hd, img_wd),
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/preprocessing/image_preprocessing.py", line 757, in get_rotation_matrix
    array_ops.zeros((num_angles, 2), dtypes.float32),
  File "/usr/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2819, in wrapped
    tensor = fun(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2868, in zeros
    output = _constant_if_small(zero, shape, dtype, name)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2804, in _constant_if_small
    if np.prod(shape) < 1000:
  File "<__array_function__ internals>", line 5, in prod
  File "/usr/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 3030, in prod
    return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
  File "/usr/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 852, in __array__
    raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (random_rotation/rotation_matrix/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.1 for tracking issues in 2.1 release type:bug Bug
Projects
None yet
Development

No branches or pull requests

6 participants