tensorflow: Cannot convert a symbolic Tensor (Neg_1:0) to a numpy array
Hello.
I am trying to derive with Tensorflow, and get an error telling me to report the behavior to the TensorFlow team. Hence this post.
My code:
from math import e
def cal_sigma(y):
return 1 / (1 + np.power(e, -y) )
@tf.function
def get_derivative(x, y):
with tf.GradientTape(persistent=True) as tape:
tape.watch(x)
tape.watch(y)
y_sigma = cal_sigma(-y)
f = (x + y_sigma) / np.power((x -y), 2)
df_dy = tape.gradient(f, y)
return df_dy
x = tf.constant (1.)
y = tf.constant (-3.)
print(get_derivative(x, y))
If I derive with respect to x (df_dy = tape.gradient(f, x) in second to last line; it works. However, with y, I get the following error:
WARNING:tensorflow:AutoGraph could not transform <method-wrapper '__call__' of numpy.ufunc object at 0x110a96950> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Cannot convert a symbolic Tensor (Neg_1:0) to a numpy array.
WARNING: AutoGraph could not transform <method-wrapper '__call__' of numpy.ufunc object at 0x110a96950> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Cannot convert a symbolic Tensor (Neg_1:0) to a numpy array.
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-11-a4cdc628e4ad> in <module>
2 y = tf.constant (-3.)
3 #print(get_derivative(x, y))
----> 4 print(get_derivative2(x, y))
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
566 xla_context.Exit()
567 else:
--> 568 result = self._call(*args, **kwds)
569
570 if tracing_count == self._get_tracing_count():
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds)
604 # In this case we have not created variables on the first call. So we can
605 # run the first trace but we should fail if variables are created.
--> 606 results = self._stateful_fn(*args, **kwds)
607 if self._created_variables:
608 raise ValueError("Creating variables on a non-first call to a function"
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in __call__(self, *args, **kwargs)
2360 """Calls a graph function specialized to the inputs."""
2361 with self._lock:
-> 2362 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
2363 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2364
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2701
2702 self._function_cache.missed.add(call_context_key)
-> 2703 graph_function = self._create_graph_function(args, kwargs)
2704 self._function_cache.primary[cache_key] = graph_function
2705 return graph_function, args, kwargs
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2591 arg_names=arg_names,
2592 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2593 capture_by_value=self._capture_by_value),
2594 self._function_attributes,
2595 # Tell the ConcreteFunction to clean up its graph once it goes out of
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
976 converted_func)
977
--> 978 func_outputs = python_func(*func_args, **func_kwargs)
979
980 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
437 # __wrapped__ allows AutoGraph to swap in a converted function. We give
438 # the function a weak reference to itself to avoid a reference cycle.
--> 439 return weak_wrapped_fn().__wrapped__(*args, **kwds)
440 weak_wrapped_fn = weakref.ref(wrapped_fn)
441
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
NotImplementedError: in converted code:
<ipython-input-9-af8fccc461fb>:20 get_derivative2 *
y_sigma = cal_sigma(-y)
<ipython-input-9-af8fccc461fb>:4 cal_sigma *
return 1 / (1 + np.power(e, -y) )
/opt/anaconda3/envs/DeepLearning/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:728 __array__
" array.".format(self.name))
NotImplementedError: Cannot convert a symbolic Tensor (Neg_1:0) to a numpy array.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 25
Just use pytorch. Tensorflow and keras dont go well together
In general, you can’t mix NumPy and TF ops. But the error messages should be more informative, and we need to fix that.