How to Solve Tensorflow SincNet Error

Error Messages:

File ~/apps/miniconda3/envs/oscar_py39_tf28/lib/python3.9/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb
 
File ~/apps/miniconda3/envs/oscar_py39_tf28/lib/python3.9/site-packages/tensorflow/python/framework/func_graph.py:1147, in func_graph_from_py_func..autograph_handler(*args, **kwargs)
   1145 except Exception as e:  # pylint:disable=broad-except
   1146   if hasattr(e, "ag_error_metadata"):
-> 1147     raise e.ag_error_metadata.to_exception(e)
   1148   else:
   1149     raise
 
ValueError: in user code:
 
 
    ValueError: A tf.Variable created inside your tf.function has been garbage-collected. Your code needs to keep Python references to variables created inside `tf.function`s.
     
    A common way to raise this error is to create and return a variable only referenced inside your function:
     
    @tf.function
    def f():
      v = tf.Variable(1.0)
      return v
     
    v = f()  # Crashes with this error message!
     
    The reason this crashes is that @tf.function annotated function returns a **`tf.Tensor`** with the **value** of the variable when the function is called rather than the variable instance itself. As such there is no code holding a reference to the `v` created inside the function and Python garbage collects it.
     
    The simplest way to fix this issue is to create variables outside the function and capture them:
     
    v = tf.Variable(1.0)
     
    @tf.function
    def f():
      return v
     
    f()  # 
    v.assign_add(1.)
    f()  #

Solution:

Modify the path that you import package.

Modify

from keras.layers import Layer, InputSpec

to

from tensorflow.python.keras.engine.base_layer import Layer, InputSpec

Source From: https://stackoverflow.com/questions/66100195/a-tf-variable-created-inside-my-tf-function-has-been-garbage-collected/72285425#72285425

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *