Related
I want to load FaceNet in Keras but I am getting errors.
the modal facenet_keras.h5 is ready but I can't load it.
you can get facenet_keras.h5 from this link:
https://drive.google.com/drive/folders/1pwQ3H4aJ8a6yyJHZkTwtjcL4wYWQb7bn
My tensorflow version is:
tensorflow.__version__
'2.2.0'
and when i want to load data:
from tensorflow.keras.models import load_model
load_model('facenet_keras.h5')
get this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-2a20f38e8217> in <module>
----> 1 load_model('facenet_keras.h5')
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
182 if (h5py is not None and (
183 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 184 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
185
186 if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
175 raise ValueError('No model found in config file.')
176 model_config = json.loads(model_config.decode('utf-8'))
--> 177 model = model_config_lib.model_from_config(model_config,
178 custom_objects=custom_objects)
179
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects)
53 '`Sequential.from_config(config)`?')
54 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
---> 55 return deserialize(config, custom_objects=custom_objects)
56
57
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
103 config['class_name'] = _DESERIALIZATION_TABLE[layer_class_name]
104
--> 105 return deserialize_keras_object(
106 config,
107 module_objects=globs,
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
367
368 if 'custom_objects' in arg_spec.args:
--> 369 return cls.from_config(
370 cls_config,
371 custom_objects=dict(
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py in from_config(cls, config, custom_objects)
984 ValueError: In case of improperly formatted config dict.
985 """
--> 986 input_tensors, output_tensors, created_layers = reconstruct_from_config(
987 config, custom_objects)
988 model = cls(inputs=input_tensors, outputs=output_tensors,
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py in reconstruct_from_config(config, custom_objects, created_layers)
2017 # First, we create all layers and enqueue nodes to be processed
2018 for layer_data in config['layers']:
-> 2019 process_layer(layer_data)
2020 # Then we process nodes in order of layer depth.
2021 # Nodes that cannot yet be processed (if the inbound node
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py in process_layer(layer_data)
1999 from tensorflow.python.keras.layers import deserialize as deserialize_layer # pylint: disable=g-import-not-at-top
2000
-> 2001 layer = deserialize_layer(layer_data, custom_objects=custom_objects)
2002 created_layers[layer_name] = layer
2003
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
103 config['class_name'] = _DESERIALIZATION_TABLE[layer_class_name]
104
--> 105 return deserialize_keras_object(
106 config,
107 module_objects=globs,
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
367
368 if 'custom_objects' in arg_spec.args:
--> 369 return cls.from_config(
370 cls_config,
371 custom_objects=dict(
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py in from_config(cls, config, custom_objects)
988 def from_config(cls, config, custom_objects=None):
989 config = config.copy()
--> 990 function = cls._parse_function_from_config(
991 config, custom_objects, 'function', 'module', 'function_type')
992
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py in _parse_function_from_config(cls, config, custom_objects, func_attr_name, module_attr_name, func_type_attr_name)
1040 elif function_type == 'lambda':
1041 # Unsafe deserialization from bytecode
-> 1042 function = generic_utils.func_load(
1043 config[func_attr_name], globs=globs)
1044 elif function_type == 'raw':
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py in func_load(code, defaults, closure, globs)
469 except (UnicodeEncodeError, binascii.Error):
470 raw_code = code.encode('raw_unicode_escape')
--> 471 code = marshal.loads(raw_code)
472 if globs is None:
473 globs = globals()
ValueError: bad marshal data (unknown type code)
thank you.
The possible solutions to this error are shown below:
The Model might have been built and saved in Python 2.x and you might be using Python 3.x. Solution is to use the same Python Version using which the Model has been Built and Saved.
Use the same version of Keras (and, may be, tensorflow), on which your Model was Built and Saved.
The Saved Model might contain Custom Objects. If so, you need to load the Model using the code,
new_model = tf.keras.models.load_model('model.h5', custom_objects={'CustomLayer': CustomLayer})
If you can recreate the architecture (i.e. you have the original code used to generate it), you can instantiate the model from that code and then use model.load_weights('your_model_file.hdf5') to load in the weights. This isn't an option if you don't have the code used to create the original architecture.
For more details, please refer this Github Issue. For more details regarding Saving and Loading the Model with Custom Objects, please refer this Tensorflow Documentation and this Stack Overflow Answer.
I change python version(3.10 to 3.7) and its solved for me.
I'm trying to save a Keras model that consists of two other Keras models ensembled. I'm using keras==2.3.1 and tensorflow==2.0.0 on Python 3.7.
text_in = tensorflow.keras.layers.Input(shape=(MAX_SEQ_LEN,), dtype="int32", name='myinput')
mods = [model_0, model_1]
model_outputs = [model(text_in) for model in mods]
ensemble_output = tf.keras.layers.Average()(model_outputs)
ensemble_model = tf.keras.Model(inputs=text_in, outputs=ensemble_output)
When I do ensemble_model.save(fname), I get the following traceback.
ValueError Traceback (most recent call last)
<ipython-input-65-0212c05a2372> in <module>
1 fname = 'model_multiclass_multilabel_{}.h5'.format(datetime.utcnow())
2 base = fname.split('.h5')[0]
----> 3 model.save(fname)
/opt/miniconda3/envs/ml/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):
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
110 'or using `save_weights`.')
111 hdf5_format.save_model_to_hdf5(
--> 112 model, filepath, overwrite, include_optimizer)
113 else:
114 saved_model_save.save(model, filepath, overwrite, include_optimizer,
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in save_model_to_hdf5(model, filepath, overwrite, include_optimizer)
97
98 try:
---> 99 model_metadata = saving_utils.model_metadata(model, include_optimizer)
100 for k, v in model_metadata.items():
101 if isinstance(v, (dict, list, tuple)):
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saving_utils.py in model_metadata(model, include_optimizer, require_config)
160 model_config = {'class_name': model.__class__.__name__}
161 try:
--> 162 model_config['config'] = model.get_config()
163 except NotImplementedError as e:
164 if require_config:
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in get_config(self)
885 if not self._is_graph_network:
886 raise NotImplementedError
--> 887 return copy.deepcopy(get_network_config(self))
888
889 #classmethod
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in get_network_config(network, serialize_layer_fn)
1940 filtered_inbound_nodes.append(node_data)
1941
-> 1942 layer_config = serialize_layer_fn(layer)
1943 layer_config['name'] = layer.name
1944 layer_config['inbound_nodes'] = filtered_inbound_nodes
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in serialize_keras_object(instance)
138 if hasattr(instance, 'get_config'):
139 return serialize_keras_class_and_config(instance.__class__.__name__,
--> 140 instance.get_config())
141 if hasattr(instance, '__name__'):
142 return instance.__name__
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in get_config(self)
885 if not self._is_graph_network:
886 raise NotImplementedError
--> 887 return copy.deepcopy(get_network_config(self))
888
889 #classmethod
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in get_network_config(network, serialize_layer_fn)
1956 model_inputs.append(
1957 tf_utils.ListWrapper([layer.name, new_node_index, tensor_index]))
-> 1958 model_inputs = nest.pack_sequence_as(network._nested_inputs, model_inputs)
1959 # Preserve external Keras compat for Models with single input.
1960 if not nest.is_sequence(model_inputs):
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py in pack_sequence_as(structure, flat_sequence, expand_composites)
454 if len(flat_sequence) != 1:
455 raise ValueError("Structure is a scalar but len(flat_sequence) == %d > 1"
--> 456 % len(flat_sequence))
457 return flat_sequence[0]
458
ValueError: Structure is a scalar but len(flat_sequence) == 0 > 1
Any help would be greatly appreciated. I've made sure the models were all trained using the same version of Keras/TF and they all share the same types of inputs.
Any idea how I can save this model properly?
I have one working tensorflow RNN model (called my_model) and I save it using my_model.save("<dir_to_my_model>").
However, when I reload this model with
model = tf.keras.models.load_model("<dir_to_my_model>")
and run a few incremental learning epochs with
with tf.GradientTape() as tape:
logits = model(x_test_batch, training=True)
I got an error (very long, please see at the bottom).
But, if I save the weights of this model to a checkpoint using model.save_weights(), create a new model and load the weights using model.load_weights from that checkpoint. The code above could run successfully.
My question is why these 2 methods work differently and what should I do to make method 1 work?
InvalidArgumentError Traceback (most recent call last)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
2325 with c_api_util.tf_buffer() as buf:
-> 2326 c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
2327 data = c_api.TF_GetBuffer(buf)
InvalidArgumentError: Operation 'StatefulPartitionedCall' has no attr named '_XlaCompile'.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
330 try:
--> 331 xla_compile = op.get_attr("_XlaCompile")
332 xla_separate_compiled_gradients = op.get_attr(
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
2329 # Convert to ValueError for backwards compatibility.
-> 2330 raise ValueError(str(e))
2331 x = attr_value_pb2.AttrValue()
ValueError: Operation 'StatefulPartitionedCall' has no attr named '_XlaCompile'.
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
2325 with c_api_util.tf_buffer() as buf:
-> 2326 c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
2327 data = c_api.TF_GetBuffer(buf)
InvalidArgumentError: Operation 'StatefulPartitionedCall' has no attr named '_XlaCompile'.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
330 try:
--> 331 xla_compile = op.get_attr("_XlaCompile")
332 xla_separate_compiled_gradients = op.get_attr(
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
2329 # Convert to ValueError for backwards compatibility.
-> 2330 raise ValueError(str(e))
2331 x = attr_value_pb2.AttrValue()
ValueError: Operation 'StatefulPartitionedCall' has no attr named '_XlaCompile'.
During handling of the above exception, another exception occurred:
LookupError Traceback (most recent call last)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
606 try:
--> 607 grad_fn = ops.get_gradient_function(op)
608 except LookupError:
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in get_gradient_function(op)
2494 op_type = op.type
-> 2495 return _gradient_registry.lookup(op_type)
2496
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/registry.py in lookup(self, name)
96 raise LookupError(
---> 97 "%s registry has no entry for: %s" % (self._name, name))
LookupError: gradient registry has no entry for: While
During handling of the above exception, another exception occurred:
LookupError Traceback (most recent call last)
<ipython-input-7-55225939f96a> in <module>
10 mode = 'DNN' if 'ANN' in m else 'RNN'
11 c_datasets = continual_dataset(datasets[i], mode=mode)
---> 12 res = learning_rate_test(continual_fine_tune, mname, c_datasets, lr_list=lrs)
13 dfs.append(res)
<ipython-input-5-f4d4cc64dc17> in learning_rate_test(continual_func, model_fname, c_datasets, epochs, lr_list, loss)
14 model.save_weights('Model/tmp')
15 model.load_weights('Model/tmp')
---> 16 test_predictions = continual_func(c_datasets, model, loss_fn, lr, epochs=e, save_model='')
17 test_predictions = (np.array(test_predictions) > 0.5).astype(int)
18 epoch_test_dict[e].append(np.sum(np.equal(y_test_list, test_predictions)) / len(y_test_list) * 100)
<ipython-input-4-9ae956b3e918> in continual_fine_tune(c_datasets, model, loss_fn, lr, epochs, save_model)
12 for e in range(epochs):
13 with tf.GradientTape() as tape:
---> 14 logits = model(x_test_batch, training=True)
15 loss_value = loss_fn(y_test_batch, logits)
16 grads = tape.gradient(loss_value, model.trainable_weights)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
820 with base_layer_utils.autocast_context_manager(
821 self._compute_dtype):
--> 822 outputs = self.call(cast_inputs, *args, **kwargs)
823 self._handle_activity_regularization(inputs, outputs)
824 self._set_mask_metadata(inputs, outputs, input_masks)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py in call(self, inputs, training, mask)
265 if not self.built:
266 self._init_graph_network(self.inputs, self.outputs, name=self.name)
--> 267 return super(Sequential, self).call(inputs, training=training, mask=mask)
268
269 outputs = inputs # handle the corner case where self.layers is empty
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in call(self, inputs, training, mask)
715 return self._run_internal_graph(
716 inputs, training=training, mask=mask,
--> 717 convert_kwargs_to_constants=base_layer_utils.call_context().saving)
718
719 def compute_output_shape(self, input_shape):
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in _run_internal_graph(self, inputs, training, mask, convert_kwargs_to_constants)
889
890 # Compute outputs.
--> 891 output_tensors = layer(computed_tensors, **kwargs)
892
893 # Update tensor_dict.
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
820 with base_layer_utils.autocast_context_manager(
821 self._compute_dtype):
--> 822 outputs = self.call(cast_inputs, *args, **kwargs)
823 self._handle_activity_regularization(inputs, outputs)
824 self._set_mask_metadata(inputs, outputs, input_masks)
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in return_outputs_and_add_losses(*args, **kwargs)
57 inputs = args[inputs_arg_index]
58 args = args[inputs_arg_index + 1:]
---> 59 outputs, losses = fn(inputs, *args, **kwargs)
60 layer.add_loss(losses, inputs)
61 return outputs
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in wrap_with_training_arg(*args, **kwargs)
111 training,
112 lambda: replace_training_and_call(True),
--> 113 lambda: replace_training_and_call(False))
114
115 # Create arg spec for decorated function. If 'training' is not defined in the
~/work/tf/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
~/work/tf/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()
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in <lambda>()
110 return tf_utils.smart_cond(
111 training,
--> 112 lambda: replace_training_and_call(True),
113 lambda: replace_training_and_call(False))
114
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in replace_training_and_call(training)
106 def replace_training_and_call(training):
107 set_training_arg(training, training_arg_index, args, kwargs)
--> 108 return wrapped_call(*args, **kwargs)
109
110 return tf_utils.smart_cond(
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
566 xla_context.Exit()
567 else:
--> 568 result = self._call(*args, **kwds)
569
570 if tracing_count == self._get_tracing_count():
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds)
604 # In this case we have not created variables on the first call. So we can
605 # run the first trace but we should fail if variables are created.
--> 606 results = self._stateful_fn(*args, **kwds)
607 if self._created_variables:
608 raise ValueError("Creating variables on a non-first call to a function"
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in __call__(self, *args, **kwargs)
2361 with self._lock:
2362 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2363 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2364
2365 #property
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _filtered_call(self, args, kwargs)
1609 if isinstance(t, (ops.Tensor,
1610 resource_variable_ops.BaseResourceVariable))),
-> 1611 self.captured_inputs)
1612
1613 def _call_flat(self, args, captured_inputs, cancellation_manager=None):
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1695 possible_gradient_type,
1696 executing_eagerly)
-> 1697 forward_function, args_with_tangents = forward_backward.forward()
1698 if executing_eagerly:
1699 flat_outputs = forward_function.call(
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in forward(self)
1421 """Builds or retrieves a forward function for this call."""
1422 forward_function = self._functions.forward(
-> 1423 self._inference_args, self._input_tangents)
1424 return forward_function, self._inference_args + self._input_tangents
1425
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in forward(self, inference_args, input_tangents)
1183 (self._forward, self._forward_graph, self._backward,
1184 self._forwardprop_output_indices, self._num_forwardprop_outputs) = (
-> 1185 self._forward_and_backward_functions(inference_args, input_tangents))
1186 return self._forward
1187
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _forward_and_backward_functions(self, inference_args, input_tangents)
1329 outputs = self._func_graph.outputs[:self._num_inference_outputs]
1330 return self._build_functions_for_outputs(
-> 1331 outputs, inference_args, input_tangents)
1332
1333
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _build_functions_for_outputs(self, outputs, inference_args, input_tangents)
888 self._func_graph.inputs,
889 grad_ys=gradients_wrt_outputs,
--> 890 src_graph=self._func_graph)
891
892 captures_from_forward = [
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
667 # functions.
668 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 669 lambda: grad_fn(op, *out_grads))
670 else:
671 # For function call ops, we add a 'SymbolicGradient'
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
334 xla_scope = op.get_attr("_XlaScope").decode()
335 except ValueError:
--> 336 return grad_fn() # Exit early
337
338 if not xla_compile:
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
667 # functions.
668 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 669 lambda: grad_fn(op, *out_grads))
670 else:
671 # For function call ops, we add a 'SymbolicGradient'
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _rewrite_forward_and_call_backward(self, op, *doutputs)
705 def _rewrite_forward_and_call_backward(self, op, *doutputs):
706 """Add outputs to the forward call and feed them to the grad function."""
--> 707 forward_function, backwards_function = self.forward_backward(len(doutputs))
708 if not backwards_function.outputs:
709 return backwards_function.structured_outputs
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in forward_backward(self, num_doutputs)
614 if forward_backward is not None:
615 return forward_backward
--> 616 forward, backward = self._construct_forward_backward(num_doutputs)
617 self._cached_function_pairs[num_doutputs] = (forward, backward)
618 return forward, backward
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _construct_forward_backward(self, num_doutputs)
662 args=[], kwargs={},
663 signature=signature,
--> 664 func_graph=backwards_graph)
665 backwards_graph_captures = backwards_graph.external_captures
666 captures_from_forward = [
~/work/tf/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)
976 converted_func)
977
--> 978 func_outputs = python_func(*func_args, **func_kwargs)
979
980 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _backprop_function(*grad_ys)
652 self._func_graph.inputs,
653 grad_ys=grad_ys,
--> 654 src_graph=self._func_graph)
655
656 with self._func_graph.as_default():
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
667 # functions.
668 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 669 lambda: grad_fn(op, *out_grads))
670 else:
671 # For function call ops, we add a 'SymbolicGradient'
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
334 xla_scope = op.get_attr("_XlaScope").decode()
335 except ValueError:
--> 336 return grad_fn() # Exit early
337
338 if not xla_compile:
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
667 # functions.
668 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 669 lambda: grad_fn(op, *out_grads))
670 else:
671 # For function call ops, we add a 'SymbolicGradient'
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _rewrite_forward_and_call_backward(self, op, *doutputs)
705 def _rewrite_forward_and_call_backward(self, op, *doutputs):
706 """Add outputs to the forward call and feed them to the grad function."""
--> 707 forward_function, backwards_function = self.forward_backward(len(doutputs))
708 if not backwards_function.outputs:
709 return backwards_function.structured_outputs
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in forward_backward(self, num_doutputs)
614 if forward_backward is not None:
615 return forward_backward
--> 616 forward, backward = self._construct_forward_backward(num_doutputs)
617 self._cached_function_pairs[num_doutputs] = (forward, backward)
618 return forward, backward
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _construct_forward_backward(self, num_doutputs)
662 args=[], kwargs={},
663 signature=signature,
--> 664 func_graph=backwards_graph)
665 backwards_graph_captures = backwards_graph.external_captures
666 captures_from_forward = [
~/work/tf/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)
976 converted_func)
977
--> 978 func_outputs = python_func(*func_args, **func_kwargs)
979
980 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _backprop_function(*grad_ys)
652 self._func_graph.inputs,
653 grad_ys=grad_ys,
--> 654 src_graph=self._func_graph)
655
656 with self._func_graph.as_default():
~/work/tf/lib/python3.7/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
621 raise LookupError(
622 "No gradient defined for operation '%s' (op type: %s)" %
--> 623 (op.name, op.type))
624 if loop_state:
625 loop_state.EnterGradWhileContext(op, before=False)
LookupError: No gradient defined for operation 'while' (op type: While)
I have run into that in the past. The following is how I save/load keras models.
Note that the architecture and the weights are separate.
#saving model
open('model_weights.h5', 'w').write(model.to_json())
model.save_weights('architecture.json', overwrite=True)
from tensorflow.keras.models import model_from_json
#loading model
model = model_from_json(open('architecture.json').read())
model.load_weights('model_weights.h5')
I was creating a custom layer based off the NALU paper, but when testing my code in Google Colab (tensorflow version 1.10.0) I got a type error. This did not hapen in my local Jupyter notebook(tensorflow cpu version 1.8.0).
The type error appears to occurring when adding a weight in the build function of Layer. When adding the weight a type error occurs when code tries to divide the Dimension by a float. I thought this might be an error with how Keras handles arguments, so I dropped the initializer. Even without the initializer, it is not possible to add a weight to the layer. I got the structure of my code from tensorflow's keras tutorial and I am wondering if there is anything that I need to do in order to update my code to work with the most recent version of tensorflow.
TypeError Traceback (most recent call last)
<ipython-input-7-55fb94a8f3b1> in <module>()
82 y_test = x_test[:, 0] * x_test[:, 1]
83
---> 84 model = nalu_model()
85
86 model.compile(optimizer='RMSProp',
<ipython-input-7-55fb94a8f3b1> in nalu_model()
48 def nalu_model():
49 inp = tf.keras.layers.Input(shape=(2,))
---> 50 out = NALU(1)(inp)
51
52 model = tf.keras.models.Model(inputs=inp, outputs=out)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
726 if all(hasattr(x, 'shape') for x in input_list):
727 input_shapes = nest.map_structure(lambda x: x.shape, inputs)
--> 728 self.build(input_shapes)
729 self.built = True
730
<ipython-input-7-55fb94a8f3b1> in build(self, input_shape)
9 shape = tf.TensorShape((input_shape[1], self.num_outputs))
10 get = tf.keras.initializers.get
---> 11 self.W_ = self.add_variable("W_", shape=shape, initializer=get('glorot_uniform'))
12 self.M_ = self.add_variable("M_", shape=shape, initializer=get('glorot_uniform'))
13 self.GAM = self.add_variable("GAM", shape=shape, initializer=get('glorot_uniform')) # Gate add & multiply
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in add_variable(self, *args, **kwargs)
459 def add_variable(self, *args, **kwargs):
460 """Alias for `add_weight`."""
--> 461 return self.add_weight(*args, **kwargs)
462
463 def add_weight(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, getter)
563 use_resource=use_resource,
564 synchronization=synchronization,
--> 565 aggregation=aggregation)
566
567 if regularizer is not None:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/checkpointable/base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
533 new_variable = getter(
534 name=name, shape=shape, dtype=dtype, initializer=initializer,
--> 535 **kwargs_for_getter)
536
537 # If we set an initializer and the variable processed it, tracking will not
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in make_variable(name, shape, dtype, initializer, partition_info, trainable, caching_device, validate_shape, constraint, use_resource, synchronization, aggregation, partitioner)
1916 use_resource=use_resource,
1917 synchronization=synchronization,
-> 1918 aggregation=aggregation)
1919 return v
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in variable(initial_value, trainable, collections, validate_shape, caching_device, name, dtype, constraint, use_resource, synchronization, aggregation)
2441 use_resource=use_resource,
2442 synchronization=synchronization,
-> 2443 aggregation=aggregation)
2444
2445
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in <lambda>(**kwargs)
2423 synchronization=VariableSynchronization.AUTO,
2424 aggregation=VariableAggregation.NONE):
-> 2425 previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
2426 for getter in ops.get_default_graph()._variable_creator_stack: # pylint: disable=protected-access
2427 previous_getter = _make_getter(getter, previous_getter)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in default_variable_creator(next_creator, **kwargs)
2393 collections=collections, validate_shape=validate_shape,
2394 caching_device=caching_device, name=name, dtype=dtype,
-> 2395 constraint=constraint)
2396 elif not use_resource and context.executing_eagerly():
2397 raise RuntimeError(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint)
310 name=name,
311 dtype=dtype,
--> 312 constraint=constraint)
313
314 # pylint: disable=unused-argument
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, constraint)
415 with ops.name_scope("Initializer"), ops.device(None):
416 initial_value = ops.convert_to_tensor(
--> 417 initial_value(), name="initial_value", dtype=dtype)
418 self._handle = _eager_safe_variable_handle(
419 shape=initial_value.get_shape(),
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in <lambda>()
1901 initializer = initializer(dtype=dtype)
1902 init_val = lambda: initializer( # pylint: disable=g-long-lambda
-> 1903 shape, dtype=dtype, partition_info=partition_info)
1904 variable_dtype = dtype.base_dtype
1905 if use_resource is None:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py in __call__(self, shape, dtype, partition_info)
474 scale /= max(1., fan_out)
475 else:
--> 476 scale /= max(1., (fan_in + fan_out) / 2.)
477 if self.distribution == "normal" or self.distribution == "truncated_normal":
478 # constant taken from scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'
Code:
https://gist.github.com/Carliceiv/0b68fe0d9f2a9fc9a1b901e8a722f6cd
The stacktrace indicates that somewhere in the code a Dimension value is given as n argument where you usually expect an integer/float.
I think that this might be caused by this line
shape = tf.TensorShape((input_shape[1], self.num_outputs))
please try to change it to
shape = (input_shape[1], self.num_outputs)
Edit: As pointed out in the comments, this is not working of input_shape is a Dimension object and not an int. To solve this, use this modification:
shape = tf.TensorShape((input_shape[1], self.num_outputs)).as_list()
This will at first create a new TensorShape which is able to deal with the Dimension data type and then convert this to a list of integers.
I have tried to run the exact same code (on Neural Machine Translation with Attention) on my local machine as here:
https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/nmt_with_attention/nmt_with_attention.ipynb
I am getting the following error when training the model and trying to save checkpoints:
Epoch 1 Batch 0 Loss 0.3238
---------------------------------------------------------------------------
_FallbackException Traceback (most recent call last)
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in save_v2(prefix, tensor_names, shape_and_slices, tensors, name)
1809 _ctx._post_execution_callbacks, prefix, tensor_names,
-> 1810 shape_and_slices, tensors)
1811 return _result
_FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-28-bb3738bfb3d8> in <module>()
43 # saving (checkpoint) the model every 2 epochs
44 if epoch % 2 == 0:
---> 45 checkpoint.save(file_prefix = checkpoint_prefix)
46
47 print('Epoch {} Loss {:.4f}'.format(epoch + 1,
C:\Anaconda3\lib\site-packages\tensorflow\python\training\checkpointable\util.py in save(self, file_prefix, session)
1485 file_prefix=file_prefix,
1486 checkpoint_number=self.save_counter,
-> 1487 session=session)
1488
1489 def restore(self, save_path):
C:\Anaconda3\lib\site-packages\tensorflow\python\training\checkpointable\util.py in save(self, file_prefix, checkpoint_number, session)
1184 save_path=file_prefix,
1185 write_meta_graph=False,
-> 1186 global_step=checkpoint_number)
1187 return save_path
1188
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in save(self, sess, save_path, global_step, latest_filename, meta_graph_suffix, write_meta_graph, write_state, strip_default_attrs)
1613 if context.executing_eagerly():
1614 self._build_eager(
-> 1615 checkpoint_file, build_save=True, build_restore=False)
1616 model_checkpoint_path = self.saver_def.save_tensor_name
1617 else:
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in _build_eager(self, checkpoint_path, build_save, build_restore)
1295 def _build_eager(self, checkpoint_path, build_save, build_restore):
1296 self._build(
-> 1297 checkpoint_path, build_save=build_save, build_restore=build_restore)
1298
1299 def _build(self, checkpoint_path, build_save, build_restore):
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in _build(self, checkpoint_path, build_save, build_restore)
1328 restore_sequentially=self._restore_sequentially,
1329 filename=checkpoint_path,
-> 1330 build_save=build_save, build_restore=build_restore)
1331 elif self.saver_def and self._name:
1332 # Since self._name is used as a name_scope by builder(), we are
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in _build_internal(self, names_to_saveables, reshape, sharded, max_to_keep, keep_checkpoint_every_n_hours, name, restore_sequentially, filename, build_save, build_restore)
773 else:
774 if build_save:
--> 775 save_tensor = self._AddSaveOps(filename_tensor, saveables)
776 if build_restore:
777 restore_op = self._AddRestoreOps(filename_tensor, saveables,
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in _AddSaveOps(self, filename_tensor, saveables)
273 A tensor with the filename used to save.
274 """
--> 275 save = self.save_op(filename_tensor, saveables)
276 return control_flow_ops.with_dependencies([save], filename_tensor)
277
C:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py in save_op(self, filename_tensor, saveables)
191 # of a V2 checkpoint: e.g. "/fs/train/ckpt-<step>/tmp/worker<i>-<step>".
192 return io_ops.save_v2(filename_tensor, tensor_names, tensor_slices,
--> 193 tensors)
194 else:
195 raise RuntimeError("Unexpected write_version: " + self._write_version)
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in save_v2(prefix, tensor_names, shape_and_slices, tensors, name)
1813 return save_v2_eager_fallback(
1814 prefix, tensor_names, shape_and_slices, tensors, name=name,
-> 1815 ctx=_ctx)
1816 except _core._NotOkStatusException as e:
1817 if name is not None:
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in save_v2_eager_fallback(prefix, tensor_names, shape_and_slices, tensors, name, ctx)
1834 _attrs = ("dtypes", _attr_dtypes)
1835 _result = _execute.execute(b"SaveV2", 0, inputs=_inputs_flat, attrs=_attrs,
-> 1836 ctx=_ctx, name=name)
1837 _result = None
1838 return _result
C:\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 tensors = pywrap_tensorflow.TFE_Py_Execute(ctx._handle, device_name,
59 op_name, inputs, attrs,
---> 60 num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
TypeError: __init__() missing 2 required positional arguments: 'message' and 'code'