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

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'

Related

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

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.

How to fix TypeError: x and y must have the same dtype, got tf.int32 != tf.float32 with tensorflow using PyGad?

I have defined a new layer to be put as an input layer using Keras. The code is:
class layer(tensorflow.keras.layers.Layer):
def __init__(self):
super(layer, self).__init__()
H_init = tf.random_normal_initializer()
self.H = tf.Variable(
initial_value=H_init(shape=(1,), dtype="float32"),
trainable=True,
)
b_init = tf.zeros_initializer()
self.b = tf.Variable(
initial_value=b_init(shape=(1,), dtype="float32"), trainable=True
)
n_init = tf.zeros_initializer()
self.n = tf.Variable(
initial_value=n_init(shape=(1,), dtype="float32"), trainable=True
)
def call(self, z):
return self.H**2 / (1+(1/self.b)**(2/self.n)) *(1+((1+z)/self.b)**(2/self.n))
I intend to put this layer as input in my generation callback function, which is
`def callback_generation(ga_instance):
print("Generation"= {generation}".format(generation=ga_instance.generations_completed))
print("Fitness =
{fitness}".format(fitness=ga_instance.best_solution()[1]))
inputs = tensorflow.keras.Input(shape=(1,), name="inputs")
targets = tensorflow.keras.Input(shape=(1,), name="targets")
logits = tensorflow.keras.layers.Dense(15)(inputs)
predictions = layer(name="predictions")(logits, targets)
model = keras.Model(inputs=[inputs, targets],
outputs=predictions)
data = {
"inputs": z,
"targets": H_z,
}
model = tensorflow.keras.Sequential(inputs=input_layer,
outputs=output_layer)
weights_vector =
tensorflow.pygad.kerasga.model_weights_as_vector(model=model)
keras_ga = pygad.kerasga.KerasGA(model=model,
num_solutions=12)
model.summary()`
This gives me an error reporting that the tf__call() method takes 2 positional arguments but 3 were given.
This is the detailed error traceback:
TypeError Traceback (most
recent call last)
<ipython-input-101-e09c7d4f3228> in <module>
20 targets = tensorflow.keras.Input(shape=(1,),
name="targets")
21 logits = tensorflow.keras.layers.Dense(15)(inputs)
---> 22 predictions = layer(name="predictions")(logits,
targets)
23
24 model = keras.Model(inputs=[inputs, targets],
outputs=predictions)
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in
__call__(self, *args, **kwargs)
950 if _in_functional_construction_mode(self, inputs,
args, kwargs, input_list):
951 return self._functional_construction_call(inputs,
args, kwargs,
--> 952
input_list)
953
954 # Maintains info about the `Layer.call` stack.
~/anaconda3/lib/python3.7/site-
packages/tensorflow/python/keras/engine/base_layer.py in
_functional_construction_call(self, inputs, args, kwargs,
input_list)
1089 # Check input assumptions set after layer
building, e.g. input shape.
1090 outputs = self._keras_tensor_symbolic_call(
-> 1091 inputs, input_masks, args, kwargs)
1092
1093 if outputs is None:
~/anaconda3/lib/python3.7/site-
packages/tensorflow/python/keras/engine/base_layer.py in
_keras_tensor_symbolic_call(self, inputs, input_masks, args,
kwargs)
820 return
nest.map_structure(keras_tensor.KerasTensor, output_signature)
821 else:
--> 822 return self._infer_output_signature(inputs, args,
kwargs, input_masks)
823
824 def _infer_output_signature(self, inputs, args,
kwargs, input_masks):
~/anaconda3/lib/python3.7/site-
packages/tensorflow/python/keras/engine/base_layer.py in
_infer_output_signature(self, inputs, args, kwargs,
input_masks)
861 # TODO(kaftan): do we maybe_build here, or
have we already done it?
862 self._maybe_build(inputs)
--> 863 outputs = call_fn(inputs, *args, **kwargs)
864
865 self._handle_activity_regularization(inputs,
outputs)
~/anaconda3/lib/python3.7/site-
packages/tensorflow/python/autograph/impl/api.py in
wrapper(*args, **kwargs)
668 except Exception as e: # pylint:disable=broad-
except
669 if hasattr(e, 'ag_error_metadata'):
--> 670 raise e.ag_error_metadata.to_exception(e)
671 else:
672 raise
TypeError: in user code:
TypeError: tf__call() takes 2 positional arguments but 3 were given

TypeError: _logger_find_caller() takes from 0 to 1 positional arguments but 2 were given

I'm trying to run a baseline model which can be found here: https://github.com/gniknoil/FG2020-kinship/tree/master/Track1
here is the code:
def baseline_model():
input_1 = Input(shape=(224, 224, 3))
input_2 = Input(shape=(224, 224, 3))
base_model = VGGFace(model='resnet50', include_top=False)
for x in base_model.layers[:-3]:
x.trainable = True
x1 = base_model(input_1)
x2 = base_model(input_2)
x1=GlobalMaxPool2D()(x1)
x2=GlobalAvgPool2D()(x2)
x3 = Subtract()([x1, x2])
x3 = Multiply()([x3, x3])
x1_ = Multiply()([x1, x1])
x2_ = Multiply()([x2, x2])
x4 = Subtract()([x1_, x2_])
x5 = Multiply()([x1, x2])
x = Concatenate(axis=-1)([x3, x4, x5])
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.03)(x)
x = Dense(128, activation="relu")(x)
x = Dropout(0.02)(x)
out = Dense(1, activation="sigmoid")(x)
model = Model([input_1, input_2], out)
model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=adamv2.Adam(0.00001))
#model.compile(loss=[focal_loss(alpha=.25, gamma=2)], metrics=['acc'], optimizer=Adam(0.00003))
#model.compile(loss=[focal_loss(alpha=.25, gamma=2)], metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model
I got the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/1x/xq_p1_3x2wj0drhvr109gkfh0000gn/T/ipykernel_7112/1473799896.py in <module>
----> 1 model = baseline_model()
/var/folders/1x/xq_p1_3x2wj0drhvr109gkfh0000gn/T/ipykernel_7112/726929419.py in baseline_model()
31
32 def baseline_model():
---> 33 input_1 = Input(shape=(224, 224, 3))
34 input_2 = Input(shape=(224, 224, 3))
35
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/engine/input_layer.py in Input(shape, batch_shape, name, dtype, sparse, tensor)
173 if not dtype:
174 dtype = K.floatx()
--> 175 input_layer = InputLayer(batch_input_shape=batch_shape,
176 name=name, dtype=dtype,
177 sparse=sparse,
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/engine/input_layer.py in __init__(self, input_shape, batch_size, batch_input_shape, dtype, input_tensor, sparse, name)
37 if not name:
38 prefix = 'input'
---> 39 name = prefix + '_' + str(K.get_uid(prefix))
40 super(InputLayer, self).__init__(dtype=dtype, name=name)
41
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/tensorflow/python/util/deprecation_wrapper.py in __getattr__(self, name)
115 call_location = _call_location()
116 if not call_location.startswith('<'): # skip locations in Python source
--> 117 logging.warning(
118 'From %s: The name %s is deprecated. Please use %s instead.\n',
119 _call_location(), full_name, rename)
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/tensorflow/python/platform/tf_logging.py in warning(msg, *args, **kwargs)
164 #tf_export(v1=["logging.error"])
165 def error(msg, *args, **kwargs):
--> 166 get_logger().error(msg, *args, **kwargs)
167
168
~/opt/anaconda3/envs/env-kinship/lib/python3.8/logging/__init__.py in warning(self, msg, *args, **kwargs)
1456 """
1457 if self.isEnabledFor(WARNING):
-> 1458 self._log(WARNING, msg, args, **kwargs)
1459
1460 def warn(self, msg, *args, **kwargs):
~/opt/anaconda3/envs/env-kinship/lib/python3.8/logging/__init__.py in _log(self, level, msg, args, exc_info, extra, stack_info, stacklevel)
1575 #IronPython can use logging.
1576 try:
-> 1577 fn, lno, func, sinfo = self.findCaller(stack_info, stacklevel)
1578 except ValueError: # pragma: no cover
1579 fn, lno, func = "(unknown file)", 0, "(unknown function)"
TypeError: _logger_find_caller() takes from 0 to 1 positional arguments but 2 were given
How can I fix this?
My Keras version is 2.2.4
My tensor flow version is 1.14.0
I'm using python 3.8.1 and an anaconda environment.
And I have tried the solution here: https://medium.com/the-rising-tilde/typeerror-logger-find-caller-takes-from-0-to-1-positional-arguments-but-2-were-given-cb24b74a6125
which doesn't work
You are using Tensorflow 1.14 with python 3.8 which is not compatible as mentioned in this doc.
Please upgrade the Tensorflow version as per installed python version 3.8 in your system which will be Tensorflow >= 2.2 and try again executing the same code.
!pip install --upgrade tensorflow

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

Using tf.data.Dataset with tf Hub Modules

How do I feed a tf.keras model, that includes a 1D input TF Hub module, with a tf.data.Dataset?
(Ultimately, the aim is to use a single tf.data.Dataset with a multi-input, multi-output keras funtional api model.)
Tried this:
import tensorflow as tf
import tensorflow_hub as hub
embed = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
hub_layer = hub.KerasLayer(embed, output_shape=[20], input_shape=[],
dtype=tf.string, trainable=True, name='hub_layer')
# From tf hub webpage: "The module takes a batch of sentences in a 1-D tensor of strings as input."
input_tensor = tf.keras.Input(shape=(), dtype=tf.string)
hub_tensor = hub_layer(input_tensor)
x = tf.keras.layers.Dense(16, activation='relu')(hub_tensor)#(x)
main_output = tf.keras.layers.Dense(units=4, activation='softmax', name='main_output')(x)
model = tf.keras.models.Model(inputs=[input_tensor], outputs=[main_output])
# This works as expected.
X_tensor = tf.constant(['Hello World', 'The Quick Brown Fox'])
model(X_tensor)
# This fails
X_ds = tf.data.Dataset.from_tensors(X_tensor)
X_ds.element_spec
model(X_ds)
Expectation was that the 1D tensor in the dataset would be automatically extracted and consumed by the model.
Error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
21 X_ds = tf.data.Dataset.from_tensors(X_tensor)
22 X_ds.element_spec
---> 23 model(X_ds)
24
25
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
966 with base_layer_utils.autocast_context_manager(
967 self._compute_dtype):
--> 968 outputs = self.call(cast_inputs, *args, **kwargs)
969 self._handle_activity_regularization(inputs, outputs)
970 self._set_mask_metadata(inputs, outputs, input_masks)
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in call(self, inputs, training, mask)
717 return self._run_internal_graph(
718 inputs, training=training, mask=mask,
--> 719 convert_kwargs_to_constants=base_layer_utils.call_context().saving)
720
721 def compute_output_shape(self, input_shape):
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in _run_internal_graph(self, inputs, training, mask, convert_kwargs_to_constants)
835 tensor_dict = {}
836 for x, y in zip(self.inputs, inputs):
--> 837 y = self._conform_to_reference_input(y, ref_input=x)
838 x_id = str(id(x))
839 tensor_dict[x_id] = [y] * self._tensor_usage_count[x_id]
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in _conform_to_reference_input(self, tensor, ref_input)
959 # Dtype handling.
960 if isinstance(ref_input, (ops.Tensor, composite_tensor.CompositeTensor)):
--> 961 tensor = math_ops.cast(tensor, dtype=ref_input.dtype)
962
963 return tensor
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
178 """Call target, and fall back on dispatchers if there is a TypeError."""
179 try:
--> 180 return target(*args, **kwargs)
181 except (TypeError, ValueError):
182 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in cast(x, dtype, name)
785 # allows some conversions that cast() can't do, e.g. casting numbers to
786 # strings.
--> 787 x = ops.convert_to_tensor(x, name="x")
788 if x.dtype.base_dtype != base_type:
789 x = gen_math_ops.cast(x, base_type, name=name)
~/projects/email_analysis/email_venv/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:
~/projects/email_analysis/email_venv/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
~/projects/email_analysis/email_venv/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
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
268 ctx = context.context()
269 if ctx.executing_eagerly():
--> 270 t = convert_to_eager_tensor(value, ctx, dtype)
271 if shape is None:
272 return t
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
94 dtype = dtypes.as_dtype(dtype).as_datatype_enum
95 ctx.ensure_initialized()
---> 96 return ops.EagerTensor(value, ctx.device_name, dtype)
97
98
ValueError: Attempt to convert a value () with an unsupported type () to a Tensor.
The point of a dataset is to provide a sequence of tensors, like here:
all_data = tf.constant([['Hello', 'World'], ['Brown Fox', 'lazy dog']])
ds = tf.data.Dataset.from_tensor_slices(all_data)
for tensor in ds:
print(tensor)
which outputs
tf.Tensor([b'Hello' b'World'], shape=(2,), dtype=string)
tf.Tensor([b'Brown Fox' b'lazy dog'], shape=(2,), dtype=string)
Instead of just printing tensor, you can compute with it:
for tensor in ds:
print(hub_layer(tensor))
which outputs 2 tensors of shape (2,20) each.
For more, see https://www.tensorflow.org/guide/data.