Data API Error : Cannot convert a symbolic Tensor (truediv:0) to a numpy array - tensorflow

I have 45000 images of size 224*224, stored as a numpy array. This array, called source_arr has shape 45000,224,224 and it fits in the memory.
I want to divide this array into train, test and validate array and pre-process (normalize and convert greyscale to 3 channels RGB) them using tf.data API.
I have written a pre process function like:
def pre_process(x):
#Zero centering the scaled dataset
x_norm = (x-mean_Rot_MIP)/Var_Rot_MIP
#Stack 3 channels
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=0)
print('Rotn MIP 3ch dim:', x_norm_3ch.shape) # (3, 224, 224)
#converting channel 1st to channel last move axis 1 to 3
x_norm_3ch = moveaxis(x_norm_3ch, 0,2)
print('Rotn MIP ch last dim: ',x_norm_3ch.shape) # (224, 224, 3)
return x_norm_3ch
X_train_cases_idx.idx contains the index of images from source_arr that are part of training data.
I have read the corresponding training images from source_arr in the dataset object like:
X_train = tf.data.Dataset.from_tensor_slices([source_arr[i] for i in X_train_cases_idx.idx])
And then I apply the pre_process function on the training images like
X_train = X_train.map(pre_process)
but I get the following error
Traceback (most recent call last):
File "<ipython-input-37-69aa131a6944>", line 1, in <module>
X_train = X_train.map(pre_process)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1695, in map
return MapDataset(self, map_func, preserve_cardinality=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 4045, in __init__
use_legacy_function=use_legacy_function)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3371, in __init__
self._function = wrapper_fn.get_concrete_function()
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2939, in get_concrete_function
*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2906, in _get_concrete_function_garbage_collected
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3364, in wrapper_fn
ret = _wrapper_helper(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3299, in _wrapper_helper
ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 258, in wrapper
raise e.ag_error_metadata.to_exception(e)
NotImplementedError: in user code:
<ipython-input-2-746b4230fbd1>:58 pre_process *
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=1)
<__array_function__ internals>:6 stack **
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 stack
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 <listcomp>
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py:138 asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:848 __array__
" a NumPy call, which is not supported".format(self.name))
NotImplementedError: Cannot convert a symbolic Tensor (truediv: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
What am I doing wrong, and how do I fix it?
I am using Tensorflow 2.0 with python 3.7 on windows 10

As the error message points out, you are trying to use NumPy functions to operate with TensorFlow tensors. Instead, you should use TensorFlow operations. This is equivalent to what you were trying to do:
def pre_process(x):
x_norm = (x - mean_Rot_MIP) / Var_Rot_MIP
# Stacking along the last dimension to avoid having to move channel axis
x_norm_3ch = tf.stack((x_norm, x_norm, x_norm), axis=-1)
return x_norm_3ch

Related

Why TensorFlow throws this exception when loading a model that was normalized like this?

All latest versions from the very moment of this post.
tensorflow-gpu: 2.6.0
Python: 3.9.7
CUDA: 11.4.2
cuDNN: 8.2.4
As in the code below, when loading a model that was normalized by not passing arguments to Normalization() it throws an exception when that model is loaded by load_model(), however before loading the model I can use it without any apparent issues which makes you think it's all good since Normalization() did NOT complain and took care of the input shape. When loading a model that was normalized by Normalization(input_dim=5) it does NOT thrown any exception since a known shape is specified. That is weird I mean it should warn you that when normalizing it without passing arguments to Normalization() you should expect an exception when loading it.
I'm not sure if it's a bug so I'm posting it here before reporting a bug in the github section, maybe I'm missing to setup something.
Here's my code:
import numpy as np
import tensorflow as tf
def main():
train_data = np.array([[1, 2, 3, 4, 5]])
train_label = np.array([123])
# Uncomment this to load the model and comment the next model and normalizer related lines.
#model = tf.keras.models.load_model('AI/test.h5')
normalizer = tf.keras.layers.experimental.preprocessing.Normalization()
normalizer.adapt(train_data)
model = tf.keras.Sequential([normalizer, tf.keras.layers.Dense(units=1)])
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.1), loss='mean_absolute_error')
model.fit(train_data, train_label, epochs=3000)
model.save('AI/test.h5')
unseen_data = np.array([[1, 2, 3, 4, 6]])
prediction = model.predict(unseen_data)
print(prediction)
if __name__ == "__main__":
main()
It throws the following exception:
Traceback (most recent call last):
File "E:\Backup\Desktop\tensorflow_test.py", line 30, in <module>
main()
File "E:\Backup\Desktop\tensorflow_test.py", line 11, in main
model = tf.keras.models.load_model('AI/test.h5')
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\save.py", line 200, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\hdf5_format.py", line 180, in load_model_from_hdf5
model = model_config_lib.model_from_config(model_config,
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\model_config.py", line 52, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\serialization.py", line 208, in deserialize
return generic_utils.deserialize_keras_object(
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\utils\generic_utils.py", line 674, in deserialize_keras_object
deserialized_obj = cls.from_config(
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\sequential.py", line 434, in from_config
model.add(layer)
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\training\tracking\base.py", line 530, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\sequential.py", line 217, in add
output_tensor = layer(self.outputs[0])
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py", line 976, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py", line 1114, in _functional_construction_call
outputs = self._keras_tensor_symbolic_call(
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py", line 848, in _keras_tensor_symbolic_call
return self._infer_output_signature(inputs, args, kwargs, input_masks)
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py", line 886, in _infer_output_signature
self._maybe_build(inputs)
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py", line 2659, in _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
File "C:\Users\censored\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\preprocessing\normalization.py", line 145, in build
raise ValueError(
ValueError: All `axis` values to be kept must have known shape. Got axis: (-1,), input shape: [None, None], with unknown axis at index: 1
Process finished with exit code 1
It looks like a bug.
Follow this link
if 'input_dim' in kwargs and 'input_shape' not in kwargs:
# Backwards compatibility: alias 'input_dim' to 'input_shape'.
kwargs['input_shape'] = (kwargs['input_dim'],)
if 'input_shape' in kwargs or 'batch_input_shape' in kwargs:
# In this case we will later create an input layer
# to insert before the current layer
if 'batch_input_shape' in kwargs:
batch_input_shape = tuple(kwargs['batch_input_shape'])
elif 'input_shape' in kwargs:
if 'batch_size' in kwargs:
batch_size = kwargs['batch_size']
else:
batch_size = None
batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
self._batch_input_shape = batch_input_shape
The error occurs because the normalization could not get any shape information which would lead to self._input_batch_shape =(None, None).
But when loading model(deserialization), It would call build function which should have known shape in all axes.
# Sorted to avoid transposing axes.
self._keep_axis = sorted([d if d >= 0 else d + ndim for d in self.axis])
# All axes to be kept should have known shape.
for d in self._keep_axis:
if input_shape[d] is None:
raise ValueError(
'All `axis` values to be kept must have known shape. Got axis: {}, '
'input shape: {}, with unknown axis at index: {}'.format(
self.axis, input_shape, d))

Tensorflow: How do I pass the gradient through an integer tensor?

I am having an issue maintaining the flow of gradients across a custom operation in my W-GAN network that I built with TensorFloww and Keras. I have structured the W-GAN to learn to produce an image "x" (let's assume that batch_size=1 for simplicity). That image is then passed to the function below that uses the learned images to index a filter bank look-up table (LUT). I am getting the following error:
Traceback (most recent call last):
File "/home/PycharmProjects/ngan/main.py", line 141, in <module>
gan.fit(training_images, batch_size=opts["BATCH_SIZE"], epochs=opts["EPOCHS"],
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 571, in train_function
outputs = self.distribute_strategy.run(
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 951, in run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 2290, in call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 2649, in _call_for_each_replica
return fn(*args, **kwargs)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 282, in wrapper
return func(*args, **kwargs)
File "/home/PycharmProjects/ngan/ngan_test.py", line 352, in train_step
self.generator_optimizer.apply_gradients(
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py", line 472, in apply_gradients
grads_and_vars = _filter_grads(grads_and_vars)
File "/home/anaconda3/envs/ngan/lib/python3.8/site-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py", line 1218, in _filter_grads
raise ValueError("No gradients provided for any variable: %s." %
ValueError: No gradients provided for any variable: ['conv_layer_0/kernel:0', 'batch_normalization/gamma:0', 'batch_normalization/beta:0', 'conv2d/kernel:0', 'conv2d/bias:0', 'dense/kernel:0', 'layer_normalization/gamma:0', 'layer_normalization/beta:0'].
Even through tf.gather_nd is a non-differentiable operation, the TensorFlow developers have seen fit to at least have it pass through the gradient, so I don't think that is the issue. I think the issue lies with the tf.cast to
integer operation. As far as I can tell TensorFlow has a gradient hard-stop across integer tensors. I even tried creating a tf.custom_gradient pass through like I did for the tf.round operation, but that also did not work. Can someone please suggest a way to index a LUT?
Code below:
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras.backend as K
def test_function(x, lut, xmin, xmax, filter_bank):
"""
Parameters:
x: learned image (float32); shape=(batch_size, width, height, 1)
lut: look-up table (float32); shape=(1, 194, 12)
xmin: left limit (float32); shape=(1,)
xmax: right limit (float32); shape=(1,)
flter_bank: a filter bank of functions (float32); shape=(width*heigh, 12)
Returns:
proc_image: processed image (float32); shape=(batch_size, width, height, 1)
"""
#tf.custom_gradient
def custom_round(x):
""" Apply Floor operation and pass through gradient unchanged """
def grad_fn(dy):
return dy
return tf.round(x), grad_fn
# Convert incoming variable to a map of indices:
index = (x - xmin) / xmax * lut.shape[1]
index = K.cast(custom_round(index), dtype=tf.int32)
index = layers.Flatten()(index)
# Index the LUT:
b = layers.Flatten()(K.cast(K.zeros_like(x), tf.int32))
indices = K.stack([b, index], -1)
indexed_weights = tf.gather_nd(lut, indices, batch_dims=0)
# Calculated weighted sum of filter bank:
proc_image = tf.math.reduce_sum(tf.math.multiply(indexed_weights, filter_bank), axis=2, keepdims=True)
return layers.Flatten()(proc_image)
I am running:
Version information:
tensorflow-gpu-v2.0.0
NVIDIA-SMI 430.64 / Driver Version: 430.64 / CUDA Version: 10.1
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

tfidf_Train and features_Train are lists of lists containing floats, i.e. [[0.14, 0.22...],[0.52,0.34]]
I tried converting the variables to np array using np.asarray(), but I still get error at the bottom below the code when fitting my model. Appreciate any help.
inp = Input(shape=(sen_Len,))
embed = Embedding(len(term_Index)+1, emb_Dim, weights=[emb_Mat],
input_length=sen_Len, trainable=False)(inp)
emb_input = LSTM(60, dropout=0.1, recurrent_dropout=0.1)(embed)
tfidf_i = Input(shape=(1,))
conc = Concatenate()([emb_input, tfidf_i])
drop = Dropout(0.2)(conc)
dens = Dense(2)(drop)
acti = Activation('sigmoid')(dens)
model = Model([inp, tfidf_i], acti)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics =
['accuracy'])
history = model.fit([features_Train,tfidf_Train], target_Train, epochs = 50,
batch_size=128, validation_split=0.2)
Error:
x = _process_numpy_inputs(x)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/keras/engine/data_adapter.py", line 1048, in
_process_numpy_inputs
inputs = nest.map_structure(_convert_non_tensor, inputs)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/util/nest.py", line 568, in map_structure
structure[0], [func(*x) for x in entries],
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/util/nest.py", line 568, in <listcomp>
structure[0], [func(*x) for x in entries],
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/keras/engine/data_adapter.py", line 1045, in
_convert_non_tensor
return ops.convert_to_tensor(x)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/framework/ops.py", line 1314, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/framework/tensor_conversion_registry.py", line 52,
in _default_conversion_function
return constant_op.constant(value, dtype, name=name)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/framework/constant_op.py", line 258, in constant
allow_broadcast=True)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/framework/constant_op.py", line 266, in
_constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/home/stud/henrikm/anaconda3/lib/python3.7/site-
packages/tensorflow_core/python/framework/constant_op.py", line 96, in
convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type
numpy.ndarray).
I solved the problem by using a Sequential model, removing line 5 and 6 (I only used one input layer) and concatenating tfidf_Train to features_Train using np.concatenate instead of a Concatenate layer.

How to create a list of tensors and use tf.stack in tensorflow2 in a for loop

I am trying to create a list of tensors and stack them together using for loop in tensorflow2. I created a test example and tried it like the following.
import tensorflow as tf
#tf.function
def test(x):
tensor_list = []
for i in tf.range(x):
tensor_list.append(tf.ones(4)*tf.cast(i, tf.float32))
return tf.stack(tensor_list)
result = test(5)
print(result)
but I get the following error like this:
Traceback (most recent call last):
File "test.py", line 10, in <module>
result = test(5)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in __call__
result = self._call(*args, **kwds)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 503, in _call
self._initialize(args, kwds, add_initializers_to=initializer_map)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 408, in _initialize
*args, **kwds))
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1848, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 905, in wrapper
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.InaccessibleTensorError: in converted code:
test.py:8 test *
return tf.stack(tensor_list)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py:180 wrapper
return target(*args, **kwargs)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py:1165 stack
return gen_array_ops.pack(values, axis=axis, name=name)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py:6304 pack
"Pack", values=values, axis=axis, name=name)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py:793 _apply_op_helper
op_def=op_def)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:544 create_op
inp = self.capture(inp)
/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:603 capture
% (tensor, tensor.graph, self))
InaccessibleTensorError: The tensor 'Tensor("mul:0", shape=(4,), dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values, explicit Python locals or TensorFlow collections to access it. Defined in: FuncGraph(name=while_body_8, id=139870442952744); accessed from: FuncGraph(name=test, id=139870626510608).
Does anyone know what am I doing wrong? How do I create a list of tensor and stack them together with for loop in tensorflow 2?
Looping over a tensor should be generally done by using ´tf.map_fn´. Here is a solution that works:
import tensorflow as tf
import numpy as np
#tf.function
def test(x):
tensor_list = tf.map_fn(lambda inp: tf.ones(4)*tf.cast(inp, tf.float32), x, dtype=tf.dtypes.float32)
return tf.stack(tensor_list)
result = test(np.arange(5))
print(result)
However, you have to feed a real array in your test() function, but alternatively, you can call tf.range() inside the tf.function to convert the scalar into a tensor.
use tf.TensorArray instead of list

Simple RNN Model not working due to issue with tfVariable in TF2

I'm trying to build a simple model and save the untrained layers. (I'll later want to train it). I'm trying to use tensorflow core API's without relying on Keras layers so I can more directly control what I use and maximize compatibility with TFLite.
import numpy as np
import tensorflow as tf
class BasicModel(tf.Module):
def __init__(self):
self.const = None
#tf.function(input_signature=[
tf.TensorSpec(shape=[None,20],dtype=tf.int32),
])
def rnn(self, captions):
# ENCODER
weights = tf.Variable(tf.random.normal([10000, 724]))#, shape=[vocab_size,embedding_dimension], name="embedding_weights")
embedding_output = tf.nn.embedding_lookup(weights,captions)
#activation is tanh for GRUCell
sequence = tf.unstack(embedding_output,num=20, axis=1)
cell = tf.compat.v1.nn.rnn_cell.GRUCell(20)
print(sequence)
gru_layer = tf.compat.v1.nn.static_rnn(cell, sequence, dtype=tf.float32)
return gru_layer
root = BasicModel()
concrete_function = root.rnn.get_concrete_function()
tf.saved_model.save(root,"model",concrete_function)
I expect to have an untrained model that saves but instead I get an error:
Traceback (most recent call last):
File "model_tensorflow_2.py", line 24, in <module>
concrete_function = root.rnn.get_concrete_function()#tf.constant(images), tf.constant(captions), tf.constant(cap_lens))
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 782, in get_concrete_function
return self._stateless_fn.get_concrete_function(*args, **kwargs)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 1891, in get_concrete_function
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2658, in bound_method_wrapper
return wrapped_fn(*args, **kwargs)
File "/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 905, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:
model_tensorflow_2.py:13 rnn *
weights = tf.Variable(tf.random.normal([10000, 724]))#, shape=[vocab_size,embedding_dimension], name="embedding_weights")
/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py:260 __call__
return cls._variable_v2_call(*args, **kwargs)
/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py:254 _variable_v2_call
shape=shape)
/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py:65 getter
return captured_getter(captured_previous, **kwargs)
/Users/t.capes/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py:413 invalid_creator_scope
"tf.function-decorated function tried to create "
ValueError: tf.function-decorated function tried to create variables on non-first call.
tf.function does not allow to create variables on non-first call, because the semantics of that are not clear: should the variables be re-created on each call? should they be implicitly cached? (see this bit in the "tf.function and AutoGraph" talk from TF Summit 2019).
A common workaround is to have a helper function creating the variables and ensure that it's called at most once for each instance:
class BasicModel(tf.Module):
# ...
def _create_parameters(self, ...):
self._weights = tf.Variable(...)
self._parameters_created = True
def rnn(self, ...):
if not self._parameters_created:
self._create_parameters(...)
...