'Saving a composite model that includes a custom layer results in error - None has NoneType, but expected one of: bytes, unicode
I'm trying to save a model which is a composite model of composite models.
The first model is a sequential model of two sequential models. Both of the two sub-models have custom layers that perform scaling operations like MinMax and cube root. This model saves and loads without any issues.
The first model is then loaded in a different script without compiling. There is not issue with this. Let's call this model MODEL_1.
The next step may be a bit confusing. There are then two more models added in parallel to each other but sequentially with MODEL_1. Let's call these models MODEL_2a and MODEL_2b. The output of MODEL_1 has the input of MODEL_1 concatenated to it and this serves as the input to MODEL_2a and MODEL_2b. It should be noted that that the "MODEL_1 input" goes through a custom scaling layer before being concatenated to the output of MODEL_1. The is scaling layer that was also implemented in MODEL_1 without any issues.
Finally, there is a single, custom layer that performs a 'simple' weighted sum of the outputs of MODEL_2a and MODEL_2b to produce the model output. This weighting happens via alpha*OUTPUT_2a + [1-alpha]*OUTPUT_2b
. 'alpha' is a trainable, scalar parameter. I haven't used this before in a model that I have saved, so I'm guessing this is the cause.
The model compiles and trains, but fails to save.
The custom weighted sum layer is this,
class WeightedSum(krs.layers.Layer):
def __init__( self, n_models = 2, name = 'weighted_sum_0' ):
super( WeightedSum, self ).__init__( name = name)
self.n_models = n_models
self.ensemble_weights = []
self.output_init = tf.Variable(0.,validate_shape=False,trainable=False)
def build(self,input_shape):
for i in range(self.n_models):
self.ensemble_weights.append( self.add_weight(shape=(1,),
initializer = 'ones',
trainable = True) )
def call(self,inputs):
new_normalizer = tf.convert_to_tensor(0.,dtype = inputs[0].dtype)
for i in range(self.n_models):
new_normalizer = new_normalizer + self.ensemble_weights[i]
new_normalizer = tf.constant(1.,dtype=new_normalizer.dtype)/new_normalizer
output = self.output_init
for i in range(self.n_models):
output = tf.add(output,tf.multiply(self.ensemble_weights[i],inputs[i]))
output = tf.multiply( output, new_normalizer )
return output
The save command is this, (NOTE: I use import tensorflow.keras as krs
)
krs.models.save_model(linked_model,"test_failed_save.mdl")
The error that is produced is this.
Traceback (most recent call last):
File "multi_fidelity_training_full_link.py", line 304, in <module>
main()
File "multi_fidelity_training_full_link.py", line 265, in main
krs.models.save_model(linked_model,"test_failed_save.mdl")
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 151, in save_model
signatures, options, save_traces)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save.py", line 90, in save
model, filepath, signatures, options)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 1104, in save_and_return_nodes
raise_metadata_warning))
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 1291, in _build_meta_graph
raise_metadata_warning)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 1225, in _build_meta_graph_impl
options.namespace_whitelist)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 713, in _fill_meta_graph_def
_call_function_with_mapped_captures, resource_map=resource_map)))
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/training/tracking/graph_view.py", line 424, in frozen_saveable_objects
call_with_mapped_captures)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/training/tracking/graph_view.py", line 375, in _serialize_gathered_objects
slot_variables=slot_variables)
File "/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/training/tracking/graph_view.py", line 355, in _fill_object_graph_proto
child_proto.local_name = child.name
TypeError: None has type NoneType, but expected one of: bytes, unicode
How do I fix this?
Solution 1:[1]
when you use add_weight, give a name.[self.add_weight('myname', shape=....)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | liwuzhuangdd |