在张量流中保存和调用具有自定义损失函数的模型

问题描述 投票:0回答:1

我在张量流中为 LSTM 模型提供了以下自定义损失函数:

#Custom Loss Function

def custom_loss(y_true, y_pred):
    # Calculate the aggregate difference between predictions and actuals
    loss = K.sum(K.abs(y_true - y_pred))  
    
    return loss

# Define a composite loss function that combines MSE and custom loss

def composite_loss(alpha):
    def nested_loss(y_true, y_pred):
        mse_loss = K.mean(K.square(y_true - y_pred))  # Mean Squared Error
        custom_loss_val = custom_loss(y_true, y_pred)
        composite = mse_loss * alpha + custom_loss_val  # You can adjust the weight with 'alpha'
        return composite
    return nested_loss


  # Adjust the weight of custom loss relative to MSE as needed
    
alpha = 0.5

我的模型编译看起来像这样

model = Sequential()

model.add(LSTM(128, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(Dropout(0.3))
model.add(LSTM(64,return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(32,return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(16,return_sequences=False))
model.add(Dense(1))


# Create an instance of the custom loss class

optimizer = Adam(lr=0.001)

model.compile(loss=composite_loss(alpha), optimizer=optimizer)

我的保存看起来像这样:

utils.get_custom_objects()['composite_loss'] = composite_loss
model.save('SYM-Revised-CAS-30')

我的重新加载是这样的:

model = load_model('SYM-Revised-CAS-30',custom_objects={'composite_loss': composite_loss})

当我重新加载模型时,出现此错误:

ValueError: Unknown loss function: 'nested_loss'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details

我尝试了几种不同的方法来修复,包括将损失函数放在一个类中。总是有不同的错误。

有人可以指导一下吗?

tensorflow serialization tf.keras
1个回答
0
投票

在经历了难以置信的困难后,我发现我可以在没有自定义损失函数的情况下保存模型。但是,为了避免 keras 保存函数尝试保存自定义损失函数,我必须仅将模型架构和权重单独保存为 json 文件。

这是代码:

保存:

# Save the model architecture as JSON
model_json = model.to_json()
with open("SYM-Revised-CAS-30.json", "w") as json_file:
    json_file.write(model_json)

# Save the model weights only
model.save_weights("SYM-Revised-CAS-30_model_weights.json")

重新加载:

from tensorflow.keras.models import model_from_json

# Load the model architecture from JSON
with open('SYM-Revised-CAS-30.json', 'r') as json_file:
    model = model_from_json(json_file.read())

# Load the model weights
model.load_weights('SYM-Revised-CAS-30_model_weights.json')
© www.soinside.com 2019 - 2024. All rights reserved.