TypeError: call() got an unexpected keyword argument 'use_causal_mask' ---> getting this error on flickr8k/flickr30k dataset - tensorflow

Error
TypeError Traceback (most recent call last)
/tmp/ipykernel_23/1382744270.py in <module>
2 image_path = tf.keras.utils.get_file('surf.jpg', origin=image_url)
3 image = load_image(image_path)
----> 4 model.simple_gen(image, temperature=1, max_run=1)
/tmp/ipykernel_23/644194926.py in simple_gen(self, image, temperature, max_run)
7 for n in range(max_run):
8 print("interation", n)
----> 9 preds = self((img_features, tokens)).numpy() # (batch, sequence, vocab)
10 preds = preds[:,-1, :] #(batch, vocab)
11 if temperature==0:
/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
/tmp/ipykernel_23/4251248884.py in call(self, inputs)
20 # Look at the image
21 for dec_layer in self.decoder_layers:
---> 22 txt = dec_layer(inputs=(image, txt))
23
24 txt = self.output_layer(txt)
/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
/tmp/ipykernel_23/725126540.py in call(self, inputs, training)
16
17 # Text input
---> 18 out_seq = self.self_attention(out_seq)
19
20 out_seq = self.cross_attention(out_seq, in_seq)
/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
/tmp/ipykernel_23/1379568068.py in call(self, x)
8
9 def call(self, x):
---> 10 attn = self.mha(query=x, value=x, use_causal_mask=True)
11 print("mask", attn, "end_mask")
12 x = self.add([x, attn])
/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
TypeError: call() got an unexpected keyword argument 'use_causal_mask'
Code:
tensorflow tutorial for image captioning (instead of mobilenetv3, I am using inceptionV3)
dataset:
filckr8k dataset from kaggle -> https://www.kaggle.com/datasets/adityajn105/flickr8k
The error is generated from multiheadattention of causal self-attention layer
I have tried removing the use_causal_mask kwarg from call method and it seems to work fine

use_causal_mask was introduced in Tensorflow version 2.10.0.
Read more in this blog
Verify that you are using tensorflow 2.10.0 or above.

Related

AttributeError: 'numpy.ndarray' object has no attribute 'op'

I am have a time series data and I am trying to build and train an LSTM model over it. I have 1 input and 1 Output corresponding to my model. I am trying to build a Many to Many model where Input length is exactly equal to output length.
The shape of my inputs are
print(np.shape(X))
(1700,70,401)
#(examples, Timestep, Features)
Shape of my output is
print(np.shape(Y_1))
(1700,70,3)
#(examples, Timestep, Features)
Now When I am trying to approach this problem via sequential API everything is running fine.
model = Sequential()
model.add(LSTM(32, input_shape=(70,401), return_sequences=True))
model.add(Dense(3,activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),loss=tf.keras.losses.CategoricalCrossentropy())
model.fit(X, Y_1, epochs=2,verbose=1)
But When I am approaching it from the functional API approach then it is showing the error
AttributeError: 'numpy.ndarray' object has no attribute 'op'
input_layer = Input(shape=(70,401))
hidden = LSTM(32,return_sequences=True)(input_layer)
output_1 = Dense(3, activation='softmax')(hidden)
# output_2 = Dense(np.shape(Y_2)[2], activation='softmax')(hidden)
model_lstm = Model(inputs=X, outputs = Y_1)
My question is How do I resolve the error?
I can not use the sequential API to solve the problem because I want to use Multiple Outputs to train i.e. I have 2 different outputs on which I want to train(But for the scope of this question let's just assume I have one set of input and one set of output)!!
The Entire error that I am getting is
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-66-df3a5a1656f0> in <module>
----> 1 model_lstm = Model(X, Y_1)
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in __init__(self, *args, **kwargs)
144
145 def __init__(self, *args, **kwargs):
--> 146 super(Model, self).__init__(*args, **kwargs)
147 _keras_api_gauge.get_cell('model').set(True)
148 # initializing _distribution_strategy here since it is possible to call
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in __init__(self, *args, **kwargs)
165 'inputs' in kwargs and 'outputs' in kwargs):
166 # Graph network
--> 167 self._init_graph_network(*args, **kwargs)
168 else:
169 # Subclassed network
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
268
269 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs):
--> 270 base_layer_utils.create_keras_history(self._nested_outputs)
271
272 self._base_init(name=name, **kwargs)
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
182 keras_tensors: The Tensors found that came from a Keras Layer.
183 """
--> 184 _, created_layers = _create_keras_history_helper(tensors, set(), [])
185 return created_layers
186
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
208 if getattr(tensor, '_keras_history', None) is not None:
209 continue
--> 210 op = tensor.op # The Op that created this Tensor.
211 if op not in processed_ops:
212 # Recursively set `_keras_history`.
AttributeError: 'numpy.ndarray' object has no attribute 'op'
Update
I tried type cast X and Y_1 to the tensor objects as suggested in the comments. It is perfectly working in the case of Sequential API but failing for Fnctional API.
X_tensor = tf.convert_to_tensor(X, dtype=tf.float32)
y_tensor=tf.convert_to_tensor(Y_1, dtype=tf.int32)
model_lstm = Model(X_tensor, y_tensor)
Error
AttributeError: Tensor.op is meaningless when eager execution is enabled.
AttributeError Traceback (most recent call last)
<ipython-input-100-d090ea2b5a90> in <module>
----> 1 model_lstm = Model(X_tensor, y_tensor)
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in __init__(self, *args, **kwargs)
144
145 def __init__(self, *args, **kwargs):
--> 146 super(Model, self).__init__(*args, **kwargs)
147 _keras_api_gauge.get_cell('model').set(True)
148 # initializing _distribution_strategy here since it is possible to call
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in __init__(self, *args, **kwargs)
165 'inputs' in kwargs and 'outputs' in kwargs):
166 # Graph network
--> 167 self._init_graph_network(*args, **kwargs)
168 else:
169 # Subclassed network
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
268
269 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs):
--> 270 base_layer_utils.create_keras_history(self._nested_outputs)
271
272 self._base_init(name=name, **kwargs)
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
182 keras_tensors: The Tensors found that came from a Keras Layer.
183 """
--> 184 _, created_layers = _create_keras_history_helper(tensors, set(), [])
185 return created_layers
186
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
208 if getattr(tensor, '_keras_history', None) is not None:
209 continue
--> 210 op = tensor.op # The Op that created this Tensor.
211 if op not in processed_ops:
212 # Recursively set `_keras_history`.
/root/anaconda3/envs/TensorPy36/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in op(self)
1078 def op(self):
1079 raise AttributeError(
-> 1080 "Tensor.op is meaningless when eager execution is enabled.")
1081
1082 #property
AttributeError: Tensor.op is meaningless when eager execution is enabled.
I made a mistake in the code itself while executing the Model part of in the functional API version.
model_lstm = Model(inputs=X, outputs = Y_1)
I have given the variables inside the above part of the code only!! While in this part we just define what our model is going to be. Hence Here we will just write what is our input layer that needs to be considered and what will be my output layer here!! Input layer while constructing my model will be input_layer in the code and output layer will be output_1. Hence code should be
model_lstm = Model(inputs=input_layer, outputs = output_1)
and after that we can do
model_lstm.fit(X,Y_1)
This will work perfectly fine now!!

How to set Keras layer to include `None` in return tuple

I am trying to make a Keras layer that returns None in its tuple.
class transformer_IO(tf.keras.layers.Layer):
def call(self, input):
return (input, None, None, None)
However, when I try to compile with this error, I get
AttributeError: 'NoneType' object has no attribute 'shape'
Here is an example
!pip install transformers
from transformers import TFBertModel
import tensorflow as tf
from copy import deepcopy
class transformer_IO(tf.keras.layers.Layer):
def call(self, input):
return (input, None, None, None)
def get_functional_model_protoFix():
bioRoberta_f = TFBertModel.from_pretrained('bert-base-uncased', from_pt=True)
Q_Tlayer0_f = deepcopy(bioRoberta_f.layers[0].encoder.layer[8])
Q_Tlayer0_f._name = Q_Tlayer0_f._name + 'Query_f'
Q_Tlayer1_f = deepcopy(bioRoberta_f.layers[0].encoder.layer[9])
Q_Tlayer1_f._name = Q_Tlayer1_f._name + 'Query_f'
transIO = transformer_IO()
inputIds = tf.keras.Input(shape=(None,), dtype=tf.int32, name='input_Q')
Q_outputs = bioRoberta_f(inputIds)[0]
Q_outputs = transIO(Q_outputs)
Q_outputs = Q_Tlayer0_f(Q_outputs)[0]
Q_outputs = transIO(Q_outputs)
Q_outputs = Q_Tlayer1_f(Q_outputs)[0]
modelNew = tf.keras.Model(inputs=inputIds, outputs=Q_outputs)
return modelNew
model_functional = get_functional_model_protoFix()
model_functional.compile(loss=loss_fn,
optimizer=tfa.optimizers.AdamW(weight_decay=1e-4, learning_rate=1e-5,
epsilon=1e-06))
Full error message
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-a029c18cecf9> in <module>()
----> 1 model_functional_new = get_functional_model_protoFix()
2 model_functional_new.compile(loss=loss_fn,
3 optimizer=tfa.optimizers.AdamW(weight_decay=1e-4, learning_rate=1e-5,
4 epsilon=1e-06))
7 frames
<ipython-input-34-693ee085f848> in get_functional_model_protoFix()
13
14 Q_outputs = bioRoberta_f(inputIds)[0]
---> 15 Q_outputs = transIO(Q_outputs)
16 Q_outputs = Q_Tlayer0_f(Q_outputs)[0]
17 Q_outputs = transIO(Q_outputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
952 kwargs.pop('mask')
953 inputs, outputs = self._set_connectivity_metadata_(
--> 954 inputs, outputs, args, kwargs)
955 self._handle_activity_regularization(inputs, outputs)
956 self._set_mask_metadata(inputs, outputs, input_masks)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
2312 # This updates the layer history of the output tensor(s).
2313 self._add_inbound_node(
-> 2314 input_tensors=inputs, output_tensors=outputs, arguments=arguments)
2315 return inputs, outputs
2316
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
2342 input_tensors=input_tensors,
2343 output_tensors=output_tensors,
-> 2344 arguments=arguments)
2345
2346 # Update tensor history metadata.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/node.py in __init__(self, outbound_layer, inbound_layers, node_indices, tensor_indices, input_tensors, output_tensors, arguments)
108 self.input_shapes = nest.map_structure(backend.int_shape, input_tensors)
109 # Nested structure of shape tuples, shapes of output_tensors.
--> 110 self.output_shapes = nest.map_structure(backend.int_shape, output_tensors)
111
112 # Optional keyword arguments to layer's `call`.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in int_shape(x)
1201 """
1202 try:
-> 1203 shape = x.shape
1204 if not isinstance(shape, tuple):
1205 shape = tuple(shape.as_list())
AttributeError: 'NoneType' object has no attribute 'shape'

TypeError: Expected any non-tensor type, got a tensor instead

I Was following a post on 'Training a transformer model for a chatbot with TensorFlow 2.0'. I have encountered an error on my local machine although the code seems to work fine in colab. Below is the code snippet.
def encoder_layer(units, d_model, num_heads, dropout, name="encoder_layer"):
inputs = tf.keras.Input(shape=(None, d_model), name="inputs")
padding_mask = tf.keras.Input(shape=(1, 1, None), name="padding_mask")
attention = MultiHeadAttention(
d_model, num_heads, name="attention")({
'query': inputs,
'key': inputs,
'value': inputs,
'mask': padding_mask
})
attention = tf.keras.layers.Dropout(rate=dropout)(attention)
attention = tf.keras.layers.LayerNormalization(
epsilon=1e-6)(inputs + attention)
outputs = tf.keras.layers.Dense(units=units, activation='relu')(attention)
outputs = tf.keras.layers.Dense(units=d_model)(outputs)
outputs = tf.keras.layers.Dropout(rate=dropout)(outputs)
outputs = tf.keras.layers.LayerNormalization(
epsilon=1e-6)(attention + outputs)
return tf.keras.Model(
inputs=[inputs, padding_mask], outputs=outputs, name=name)
I called above function with the following function call;
sample_encoder_layer = encoder_layer(
units=512,
d_model=128,
num_heads=4,
dropout=0.3,
name="sample_encoder_layer")
Below is the traceback of the error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
323 try:
--> 324 fn(values)
325 except ValueError as e:
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _check_not_tensor(values)
275 def _check_not_tensor(values):
--> 276 _ = [_check_failed(v) for v in nest.flatten(values)
277 if isinstance(v, ops.Tensor)]
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in <listcomp>(.0)
276 _ = [_check_failed(v) for v in nest.flatten(values)
--> 277 if isinstance(v, ops.Tensor)]
278 # pylint: enable=invalid-name
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _check_failed(v)
247 # it is safe to use here.
--> 248 raise ValueError(v)
249
ValueError: Tensor("attention_1/Identity:0", shape=(None, None, 128), dtype=float32)
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-20-3fa05a9bbfda> in <module>
----> 1 sample_encoder_layer = encoder_layer(units=512, d_model=128, num_heads=4, dropout=0.3, name='sample_encoder_layer')
2
3 tf.keras.utils.plot_model(
4 sample_encoder_layer, to_file='encoder_layer.png', show_shapes=True)
<ipython-input-18-357ca53de1c0> in encoder_layer(units, d_model, num_heads, dropout, name)
10 'mask': padding_mask
11 })
---> 12 attention = tf.keras.layers.Dropout(rate=dropout)(attention)
13 attention = tf.keras.layers.LayerNormalization(
14 epsilon=1e-6)(inputs + attention)
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
920 not base_layer_utils.is_in_eager_or_tf_function()):
921 with auto_control_deps.AutomaticControlDependencies() as acd:
--> 922 outputs = call_fn(cast_inputs, *args, **kwargs)
923 # Wrap Tensors in `outputs` in `tf.identity` to avoid
924 # circular dependencies.
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs, training)
209 output = tf_utils.smart_cond(training,
210 dropped_inputs,
--> 211 lambda: array_ops.identity(inputs))
212 return output
213
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf_utils.py in smart_cond(pred, true_fn, false_fn, name)
63 pred, true_fn=true_fn, false_fn=false_fn, name=name)
64 return smart_module.smart_cond(
---> 65 pred, true_fn=true_fn, false_fn=false_fn, name=name)
66
67
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
57 else:
58 return control_flow_ops.cond(pred, true_fn=true_fn, false_fn=false_fn,
---> 59 name=name)
60
61
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
505 'in a future version' if date is None else ('after %s' % date),
506 instructions)
--> 507 return func(*args, **kwargs)
508
509 doc = _add_deprecated_arg_notice_to_docstring(
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in cond(pred, true_fn, false_fn, strict, name, fn1, fn2)
1175 if (util.EnableControlFlowV2(ops.get_default_graph()) and
1176 not context.executing_eagerly()):
-> 1177 return cond_v2.cond_v2(pred, true_fn, false_fn, name)
1178
1179 # We needed to make true_fn/false_fn keyword arguments for
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/cond_v2.py in cond_v2(pred, true_fn, false_fn, name)
82 true_name, collections=ops.get_default_graph()._collections), # pylint: disable=protected-access
83 add_control_dependencies=add_control_dependencies,
---> 84 op_return_value=pred)
85 false_graph = func_graph_module.func_graph_from_py_func(
86 false_name,
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/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)
979 _, original_func = tf_decorator.unwrap(python_func)
980
--> 981 func_outputs = python_func(*func_args, **func_kwargs)
982
983 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in dropped_inputs()
205 noise_shape=self._get_noise_shape(inputs),
206 seed=self.seed,
--> 207 rate=self.rate)
208
209 output = tf_utils.smart_cond(training,
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
505 'in a future version' if date is None else ('after %s' % date),
506 instructions)
--> 507 return func(*args, **kwargs)
508
509 doc = _add_deprecated_arg_notice_to_docstring(
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in dropout(x, keep_prob, noise_shape, seed, name, rate)
4341 raise ValueError("You must provide a rate to dropout.")
4342
-> 4343 return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)
4344
4345
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in dropout_v2(x, rate, noise_shape, seed, name)
4422 raise ValueError("rate must be a scalar tensor or a float in the "
4423 "range [0, 1), got %g" % rate)
-> 4424 x = ops.convert_to_tensor(x, name="x")
4425 x_dtype = x.dtype
4426 if not x_dtype.is_floating:
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1339
1340 if ret is None:
-> 1341 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1342
1343 if ret is NotImplemented:
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
319 as_ref=False):
320 _ = as_ref
--> 321 return constant(v, dtype=dtype, name=name)
322
323
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
260 """
261 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 262 allow_broadcast=True)
263
264
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
298 tensor_util.make_tensor_proto(
299 value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 300 allow_broadcast=allow_broadcast))
301 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
302 const_tensor = g._create_op_internal( # pylint: disable=protected-access
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
449 nparray = np.empty(shape, dtype=np_dt)
450 else:
--> 451 _AssertCompatible(values, dtype)
452 nparray = np.array(values, dtype=np_dt)
453 # check to them.
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
326 [mismatch] = e.args
327 if dtype is None:
--> 328 raise TypeError("Expected any non-tensor type, got a tensor instead.")
329 else:
330 raise TypeError("Expected %s, got %s of type '%s' instead." %
TypeError: Expected any non-tensor type, got a tensor instead.
I had this error when I converted a function argument of int datatype to tf.constant . I resolved the issue in my case by undoing it. I faced this issue when I was converting TF1 codes to TF2.3.0 . Looking at your error trace I can see it's pointed to handling some constants in tf-chatbot. Kindly check how that constant is handled.
This is a fixed issue in TensorFlow 2.3.0 onwards. Can you upgrade your TensorFlow version?
pip install tensorflow==2.3.0
pip install --upgrade tensorflow

"TypeError: unsupported callable" when saving keras model using tensorlow

When saving a Keras model defined like this:
# define model
model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(200, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))
model.compile(optimizer='adam', loss='mse')
model.save(path)
I got this following message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/anaconda3/envs/topic_forecaster/lib/python3.7/inspect.py in getfullargspec(func)
1125 skip_bound_arg=False,
-> 1126 sigcls=Signature)
1127 except Exception as ex:
~/anaconda3/envs/topic_forecaster/lib/python3.7/inspect.py in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, sigcls)
2287 return _signature_from_builtin(sigcls, obj,
-> 2288 skip_bound_arg=skip_bound_arg)
2289
~/anaconda3/envs/topic_forecaster/lib/python3.7/inspect.py in _signature_from_builtin(cls, func, skip_bound_arg)
2111 if not s:
-> 2112 raise ValueError("no signature found for builtin {!r}".format(func))
2113
ValueError: no signature found for builtin <tensorflow.python.keras.saving.saved_model.save_impl.LayerCall object at 0x7f8c1f357190>
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-9-3fe80778ab16> in <module>
3 path = Path.cwd().parent / 'models' / 'tpi'
4 Path(path).mkdir(parents=True, exist_ok=True)
----> 5 model.save(str(path))
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options)
973 """
974 saving.save_model(self, filepath, overwrite, include_optimizer, save_format,
--> 975 signatures, options)
976
977 def save_weights(self, filepath, overwrite=True, save_format=None):
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
113 else:
114 saved_model_save.save(model, filepath, overwrite, include_optimizer,
--> 115 signatures, options)
116
117
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options)
72 # default learning phase placeholder.
73 with K.learning_phase_scope(0):
---> 74 save_lib.save(model, filepath, signatures, options)
75
76 if not include_optimizer:
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/saved_model/save.py in save(obj, export_dir, signatures, options)
868 if signatures is None:
869 signatures = signature_serialization.find_function_to_export(
--> 870 checkpoint_graph_view)
871
872 signatures = signature_serialization.canonicalize_signatures(signatures)
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/saved_model/signature_serialization.py in find_function_to_export(saveable_view)
62 # If the user did not specify signatures, check the root object for a function
63 # that can be made into a signature.
---> 64 functions = saveable_view.list_functions(saveable_view.root)
65 signature = functions.get(DEFAULT_SIGNATURE_ATTR, None)
66 if signature is not None:
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/saved_model/save.py in list_functions(self, obj)
139 if obj_functions is None:
140 obj_functions = obj._list_functions_for_serialization( # pylint: disable=protected-access
--> 141 self._serialization_cache)
142 self._functions[obj] = obj_functions
143 return obj_functions
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in _list_functions_for_serialization(self, serialization_cache)
2420 def _list_functions_for_serialization(self, serialization_cache):
2421 return (self._trackable_saved_model_saver
-> 2422 .list_functions_for_serialization(serialization_cache))
2423
2424
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/base_serialization.py in list_functions_for_serialization(self, serialization_cache)
89 `ConcreteFunction`.
90 """
---> 91 fns = self.functions_to_serialize(serialization_cache)
92
93 # The parent AutoTrackable class saves all user-defined tf.functions, and
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in functions_to_serialize(self, serialization_cache)
77 def functions_to_serialize(self, serialization_cache):
78 return (self._get_serialized_attributes(
---> 79 serialization_cache).functions_to_serialize)
80
81 def _get_serialized_attributes(self, serialization_cache):
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes(self, serialization_cache)
92
93 object_dict, function_dict = self._get_serialized_attributes_internal(
---> 94 serialization_cache)
95
96 serialized_attr.set_and_validate_objects(object_dict)
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/model_serialization.py in _get_serialized_attributes_internal(self, serialization_cache)
51 objects, functions = (
52 super(ModelSavedModelSaver, self)._get_serialized_attributes_internal(
---> 53 serialization_cache))
54 functions['_default_save_signature'] = default_signature
55 return objects, functions
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes_internal(self, serialization_cache)
101 """Returns dictionary of serialized attributes."""
102 objects = save_impl.wrap_layer_objects(self.obj, serialization_cache)
--> 103 functions = save_impl.wrap_layer_functions(self.obj, serialization_cache)
104 # Attribute validator requires that the default save signature is added to
105 # function dict, even if the value is None.
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in wrap_layer_functions(layer, serialization_cache)
154 # Reset the losses of the layer and its children. The call function in each
155 # child layer is replaced with tf.functions.
--> 156 original_fns = _replace_child_layer_functions(layer, serialization_cache)
157 original_losses = _reset_layer_losses(layer)
158
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in _replace_child_layer_functions(layer, serialization_cache)
246 layer_fns = (
247 child_layer._trackable_saved_model_saver._get_serialized_attributes(
--> 248 serialization_cache).functions)
249 else:
250 layer_fns = (
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes(self, serialization_cache)
92
93 object_dict, function_dict = self._get_serialized_attributes_internal(
---> 94 serialization_cache)
95
96 serialized_attr.set_and_validate_objects(object_dict)
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes_internal(self, serialization_cache)
101 """Returns dictionary of serialized attributes."""
102 objects = save_impl.wrap_layer_objects(self.obj, serialization_cache)
--> 103 functions = save_impl.wrap_layer_functions(self.obj, serialization_cache)
104 # Attribute validator requires that the default save signature is added to
105 # function dict, even if the value is None.
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in wrap_layer_functions(layer, serialization_cache)
164 call_fn_with_losses = call_collection.add_function(
165 _wrap_call_and_conditional_losses(layer),
--> 166 '{}_layer_call_and_return_conditional_losses'.format(layer.name))
167 call_fn = call_collection.add_function(
168 _extract_outputs_from_fn(layer, call_fn_with_losses),
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in add_function(self, call_fn, name)
492 # Manually add traces for layers that have keyword arguments and have
493 # a fully defined input signature.
--> 494 self.add_trace(*self._input_signature)
495 return fn
496
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in add_trace(self, *args, **kwargs)
411 fn.get_concrete_function(*args, **kwargs)
412
--> 413 trace_with_training(True)
414 trace_with_training(False)
415 else:
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in trace_with_training(value, fn)
409 utils.set_training_arg(value, self._training_arg_index, args, kwargs)
410 with K.learning_phase_scope(value):
--> 411 fn.get_concrete_function(*args, **kwargs)
412
413 trace_with_training(True)
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in get_concrete_function(self, *args, **kwargs)
536 if not self.call_collection.tracing:
537 self.call_collection.add_trace(*args, **kwargs)
--> 538 return super(LayerCall, self).get_concrete_function(*args, **kwargs)
539
540
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in get_concrete_function(self, *args, **kwargs)
774 if self._stateful_fn is None:
775 initializer_map = object_identity.ObjectIdentityDictionary()
--> 776 self._initialize(args, kwargs, add_initializers_to=initializer_map)
777 self._initialize_uninitialized_variables(initializer_map)
778
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
406 self._concrete_stateful_fn = (
407 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 408 *args, **kwds))
409
410 def invalid_creator_scope(*unused_args, **unused_kwds):
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
1846 if self.input_signature:
1847 args, kwargs = None, None
-> 1848 graph_function, _, _ = self._maybe_define_function(args, kwargs)
1849 return graph_function
1850
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2148 graph_function = self._function_cache.primary.get(cache_key, None)
2149 if graph_function is None:
-> 2150 graph_function = self._create_graph_function(args, kwargs)
2151 self._function_cache.primary[cache_key] = graph_function
2152 return graph_function, args, kwargs
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2039 arg_names=arg_names,
2040 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2041 capture_by_value=self._capture_by_value),
2042 self._function_attributes,
2043 # Tell the ConcreteFunction to clean up its graph once it goes out of
~/anaconda3/envs/topic_forecaster/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)
913 converted_func)
914
--> 915 func_outputs = python_func(*func_args, **func_kwargs)
916
917 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
356 # __wrapped__ allows AutoGraph to swap in a converted function. We give
357 # the function a weak reference to itself to avoid a reference cycle.
--> 358 return weak_wrapped_fn().__wrapped__(*args, **kwds)
359 weak_wrapped_fn = weakref.ref(wrapped_fn)
360
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in wrapper(*args, **kwargs)
513 layer, inputs=inputs, build_graph=False, training=training,
514 saving=True):
--> 515 ret = method(*args, **kwargs)
516 _restore_layer_losses(original_losses)
517 return ret
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in wrap_with_training_arg(*args, **kwargs)
109 training,
110 lambda: replace_training_and_call(True),
--> 111 lambda: replace_training_and_call(False))
112
113 # Create arg spec for decorated function. If 'training' is not defined in the
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/tf_utils.py in smart_cond(pred, true_fn, false_fn, name)
57 pred, true_fn=true_fn, false_fn=false_fn, name=name)
58 return smart_module.smart_cond(
---> 59 pred, true_fn=true_fn, false_fn=false_fn, name=name)
60
61
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/framework/smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
52 if pred_value is not None:
53 if pred_value:
---> 54 return true_fn()
55 else:
56 return false_fn()
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in <lambda>()
108 return tf_utils.smart_cond(
109 training,
--> 110 lambda: replace_training_and_call(True),
111 lambda: replace_training_and_call(False))
112
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in replace_training_and_call(training)
104 def replace_training_and_call(training):
105 set_training_arg(training, training_arg_index, args, kwargs)
--> 106 return wrapped_call(*args, **kwargs)
107
108 return tf_utils.smart_cond(
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/save_impl.py in call_and_return_conditional_losses(inputs, *args, **kwargs)
555 layer_call = _get_layer_call_method(layer)
556 def call_and_return_conditional_losses(inputs, *args, **kwargs):
--> 557 return layer_call(inputs, *args, **kwargs), layer.get_losses_for(inputs)
558 return _create_call_fn_decorator(layer, call_and_return_conditional_losses)
559
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/wrappers.py in call(self, inputs, training, mask)
218 def call(self, inputs, training=None, mask=None):
219 kwargs = {}
--> 220 if generic_utils.has_arg(self.layer.call, 'training'):
221 kwargs['training'] = training
222
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in has_arg(fn, name, accept_all)
302 bool, whether `fn` accepts a `name` keyword argument.
303 """
--> 304 arg_spec = tf_inspect.getfullargspec(fn)
305 if accept_all and arg_spec.varkw is not None:
306 return True
~/anaconda3/envs/topic_forecaster/lib/python3.7/site-packages/tensorflow_core/python/util/tf_inspect.py in getfullargspec(obj)
255 if d.decorator_argspec is not None:
256 return _convert_maybe_argspec_to_fullargspec(d.decorator_argspec)
--> 257 return _getfullargspec(target)
258
259
~/anaconda3/envs/topic_forecaster/lib/python3.7/inspect.py in getfullargspec(func)
1130 # else. So to be fully backwards compatible, we catch all
1131 # possible exceptions here, and reraise a TypeError.
-> 1132 raise TypeError('unsupported callable') from ex
1133
1134 args = []
TypeError: unsupported callable
However, when saving a regular model without a TimeDistributed layer as below, it worked fine:
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
model.save('temp', save_format='tf')
By upgrading Tensorflow to 2.1 with CUDA10 using Anaconda as below, the problem is solved.
conda create -n tf-gpu-cuda10 tensorflow-gpu=2.1 cudatoolkit=10
conda activate tf-gpu-cuda10

Input tensors to a Model must come from `tf.layers.Input` when I concatenate two models with Keras API on Tensorflow

I'm creating a wide and deep model using Keras functional API on tensorflow.
When I try to merge the two models, the below error occurred.
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) in ()
1 merged_out = tf.keras.layers.concatenate([wide_model.output, deep_model.output])
2 merged_out = tf.keras.layers.Dense(1)(merged_out)
----> 3 combined_model = tf.keras.Model(inputs=wide_model.input + [deep_model.input], outputs=merged_out)
4 print(combined_model.summary())
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py
in init(self, *args, **kwargs)
111
112 def init(self, *args, **kwargs):
--> 113 super(Model, self).init(*args, **kwargs)
114 # Create a cache for iterator get_next op.
115 self._iterator_get_next = weakref.WeakKeyDictionary()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py
in init(self, *args, **kwargs)
77 'inputs' in kwargs and 'outputs' in kwargs):
78 # Graph network
---> 79 self._init_graph_network(*args, **kwargs)
80 else:
81 # Subclassed network
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/checkpointable/base.py
in _method_wrapper(self, *args, **kwargs)
362 self._setattr_tracking = False # pylint: disable=protected-access
363 try:
--> 364 method(self, *args, **kwargs)
365 finally:
366 self._setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py
in _init_graph_network(self, inputs, outputs, name)
193 'must come from tf.layers.Input. '
194 'Received: ' + str(x) +
--> 195 ' (missing previous layer metadata).')
196 # Check that x is an input tensor.
197 # pylint: disable=protected-access
ValueError: Input tensors to a Model must come from tf.layers.Input.
Received: Tensor("add_1:0", shape=(1, ?, 163), dtype=float32) (missing
previous layer metadata).
Here is the code for concatenating the two.
merged_out = tf.keras.layers.concatenate([wide_model.output, deep_model.output])
merged_out = tf.keras.layers.Dense(1)(merged_out)
combined_model = tf.keras.Model(inputs=wide_model.input + [deep_model.input], outputs=merged_out)
print(combined_model.summary())
For each model's inputs, I tried using tf.layers.Inputwith
inputs = tf.placeholder(tf.float32, shape=(None,X_resampled.shape[1]))
deep_inputs = tf.keras.Input(tensor=(inputs))
to make them tf.layers.Input as this page mentions.
But I'm still facing the same issue.
I'm using tensorflow==1.10.0
Could someone help me solving this issue?
Thanks!
In inputs=wide_model.input + [deep_model.input], wide.model.input is probably not a list, so that you are passing a new Add tensor instead of a list of inputs. Try passing inputs=[wide_model.input] + [deep_model.input] instead