NotImplementedError: Layer ModuleWrapper has arguments in `__init__` and therefore must override `get_config`

NotImplementedError: Layer ModuleWrapper has arguments in `__init__` and therefore must override `get_config`

我正在尝试将我的自动编码器模型(用于分类)保存到磁盘,但在执行时出现以下错误:model.save(model_name)

NotImplementedError: Layer ModuleWrapper has arguments in __init__ and therefore must override get_config.

这是我的代码的一部分:

import numpy as np
import tensorflow as tf  # TF 2.5.0
from tensorflow import keras
from tensorflow.keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras import layers
from tensorflow.keras import regularizers
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import pandas as pd
import sklearn
from sklearn.model_selection import train_test_split
from callbacks import all_callbacks
import os, time

print(train_data.shape, train_labels.shape, test_data.shape, test_labels.shape )
# Shape -> (3680, 1024, 1) (3680, 10) (920, 1024, 1) (920, 10)

act_func = 'relu'
out_func = 'softmax'
k_inic = 'glorot_uniform'

def create_model():
    model = Sequential()
    model.add(Dense(512,activation=act_func, kernel_initializer=k_inic))
    model.add(Dense(100,activation=act_func, kernel_initializer=k_inic))  
    model.add(Dense(10, activation=out_func, kernel_initializer=k_inic))

    opt = keras.optimizers.Adam()        
    model.compile(loss='mse', optimizer=opt, metrics=["accuracy"])
    model.build(input_shape=(None, 1024))
    return model    

history = model.fit(train_data, train_labels, epochs = EPOCHS, batch_size = BATCH_SIZE, validation_split=VALIDATION_SPLIT, verbose = 0)
model = create_model()
res = model.evaluate(test_data, test_labels, batch_size = BATCH_SIZE, verbose = 0)[1]

model_name = "autoencoder_crwu"
model.save(model_name)

模型总结:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
module_wrapper_24 (ModuleWra (None, 512)               524800    
_________________________________________________________________
module_wrapper_25 (ModuleWra (None, 100)               51300     
_________________________________________________________________
module_wrapper_26 (ModuleWra (None, 10)                1010      
=================================================================
Total params: 577,110
Trainable params: 577,110

该模型有效,我得到的最佳准确度是 93.8%,但我无法保存它(我确实可以保存权重)。

我在这里读到我需要实现 get_config 但不知道如何为我的代码实现它,因为其他示例使用 类 或其他我不使用的东西理解。有没有简单的方法来实现它?或任何资源如何查看?

此外,为什么层称为 ModuleWrapper 而不是 Dense?

谢谢

ModuleWrapper 层名称是因为您正在混合使用 keras 和 tensorflow 库。只使用其中之一(然后你会得到密集层的密集名称,而且你不需要实现get_config)。

更改此行:

#from keras.layers import Dense             #comment this
from tensorflow.keras.layers import Dense   #add this

此外,注意到数据集的形状会导致错误,因为它们与您定义的模型不兼容,您应该从数据中删除最后一个轴。在 model.fit():

前添加这两行
train_data = tf.squeeze(train_data)
test_data = tf.squeeze(test_data) 

这些线条的形状从 (None,1024,1) 变为 (None,1024)。然后你可以将它们毫无错误地输入到你的模型中。