Я построил модель для решения проблемы с несколькими метками. Модель работает нормально, однако проблема возникает, когда я загружаю модель, используя метод load_from_json()
.
Модель принимает последовательный ввод и выводит метку или выводит список меток.
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) (None, 150) 0
_________________________________________________________________
embedding_layer (Embedding) (None, 150, 300) 30000300
_________________________________________________________________
bidirectional_lstm (Bidirect (None, 150, 400) 801600
_________________________________________________________________
batch_normalization (BatchNo (None, 150, 400) 1600
_________________________________________________________________
attention_multi_with_context (31, None, 400) 172800
_________________________________________________________________
dropout_5 (Dropout) (31, None, 400) 0
_________________________________________________________________
paralleldenseblock_5 (Parall (None, 31) 1246231
=================================================================
Параллельный плотный блочный слой создает плотный слой / слои для каждой метки над уровнем внимания, выполняется операция конкатенации для каскадирования всех выходных данных в одну матрицу. который дает матрицу баллов.
Проблема возникает, когда я загружаю модель из JSON. get_config()
Функция не записана надлежащим образом. и я не сделал, как это написать.
Я делюсь сценарием класса Parallel_dense_block
class ParallelDenseBlock(Model):
def __init__(self, nb_classes):
super(ParallelDenseBlock, self).__init__(name='')
self.nb_classes = nb_classes
self.dense_layers = []
for i in range(nb_classes): #create dense nn for each class
d1 = Dense(100, activation='sigmoid') #200 ->100
setattr(self, d1.name, d1)
d2 = Dense(1, activation='sigmoid') #100 -> 1
setattr(self, d2.name, d2)
self.dense_layers.append([d1.name,d2.name])
def call(self, input_tensor, training=False):
x = input_tensor[0]
layer = self.dense_layers[0]
x = getattr(self,layer[0])(x)
merged = getattr(self,layer[1])(x)
for i in range(1,self.nb_classes):
# add more layer
x = input_tensor[i]
layer = self.dense_layers[i]
x = getattr(self,layer[0])(x)
merged = concatenate([merged, getattr(self,layer[1])(x)], axis=-1)
return merged
def compute_output_shape(self, input_shape):
self.output_dim = [input_shape[1], self.nb_classes]
return input_shape[1], self.nb_classes
def get_config(self):
config={}
config['output_dim'] = self.output_dim
config['nb_classes'] = self.nb_classes
config["layers"]=self.dense_layers
#base_config = super(ParallelDenseBlock, self).get_config()
return dict(list(config.items()))
ошибка
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-53501cecf94a> in <module>
5 loaded_model_json = json_file.read()
6 json_file.close()
----> 7 loaded_model = model_from_json(loaded_model_json,custom_objects={"AttentionMultiWithContext":AttentionMultiWithContext,"ParallelDenseBlock":ParallelDenseBlock})
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/saving.py in model_from_json(json_string, custom_objects)
662 config = json.loads(json_string)
663 from ..layers import deserialize
--> 664 return deserialize(config, custom_objects=custom_objects)
665
666
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
149 return cls.from_config(config['config'])
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
1054 # First, we create all layers and enqueue nodes to be processed
1055 for layer_data in config['layers']:
-> 1056 process_layer(layer_data)
1057
1058 # Then we process nodes in order of layer depth.
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/network.py in process_layer(layer_data)
1040
1041 layer = deserialize_layer(layer_data,
-> 1042 custom_objects=custom_objects)
1043 created_layers[layer_name] = layer
1044
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
149 return cls.from_config(config['config'])
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
1054 # First, we create all layers and enqueue nodes to be processed
1055 for layer_data in config['layers']:
-> 1056 process_layer(layer_data)
1057
1058 # Then we process nodes in order of layer depth.
~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/network.py in process_layer(layer_data)
1034 ValueError: In case of improperly formatted `layer_data` dict.
1035 """
-> 1036 layer_name = layer_data['name']
1037
1038 # Instantiate layer.
TypeError: list indices must be integers or slices, not str