Hi!
I’m quite new to TensorFlow; as a first experiment, I’m trying to implement a python script that manipulates tensors. My goal is to compare the performances using TensorFlow vs using NumPy.
To keep the code most general (thus switching between TensorFlow and NumPy by modifying a flag only), I’m using a flag to implement the same function either using TensorFlow or using NumPy; e.g.:
if use_tf:
@tf.function
def maximum(X,Y):
return(tf.maximum(X,Y))
else:
def maximum(X,Y):
return(np.maximum(X,Y))
using the same paradigm, I’m trying to do the same for tf.Variable
, as follows:
if use_tf:
@tf.function
def Variable(X,name=None):
return(tf.Variable(X,name=name))
else:
def Variable(X,name=None):
return(X)
thus, when use_tf=False
this simply returns the input argument, while when using TensorFlow (use_tf=True) this initialises a TensorFlow variable (where X
is a NumPy array)
however, when I call the new function (use_tf=True
):
U=Variable(np.zeros(shape=(3,3),dtype=np.float32),name="U"
I obtained:
/home/cc14/Codes/anaconda3/envs/tf-cpu/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py:1007 fn_with_cond *
functools.partial(
<ipython-input-3-9b20263024b5>:3 Variable *
return(tf.Variable(X,name=name))
/home/cc14/Codes/anaconda3/envs/tf-cpu/lib/python3.9/site-packages/tensorflow/python/ops/variables.py:268 __call__ **
return cls._variable_v2_call(*args, **kwargs)
/home/cc14/Codes/anaconda3/envs/tf-cpu/lib/python3.9/site-packages/tensorflow/python/ops/variables.py:250 _variable_v2_call
return previous_getter(
/home/cc14/Codes/anaconda3/envs/tf-cpu/lib/python3.9/site-packages/tensorflow/python/ops/variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
/home/cc14/Codes/anaconda3/envs/tf-cpu/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py:764 invalid_creator_scope
raise ValueError(
ValueError: tf.function-decorated function tried to create variables on non-first call.
is there a way to create a function that, given a NumPy array, creates and returns a TensorFlow variable?
Thanks for the help.
Cesare