gpt4 book ai didi

python - 未实现错误: Layers with arguments in `__init__` must override `get_config`

转载 作者:行者123 更新时间:2023-12-02 08:29:06 30 4
gpt4 key购买 nike

我尝试使用 model.save() 保存我的 TensorFlow 模型,但是 - 我收到此错误。

此处提供了模型摘要: Model Summary

变压器模型的代码:

def transformer(vocab_size, num_layers, units, d_model, num_heads, dropout, name="transformer"):
inputs = tf.keras.Input(shape=(None,), name="inputs")
dec_inputs = tf.keras.Input(shape=(None,), name="dec_inputs")

enc_padding_mask = tf.keras.layers.Lambda(
create_padding_mask, output_shape=(1, 1, None),
name='enc_padding_mask')(inputs)
# mask the future tokens for decoder inputs at the 1st attention block
look_ahead_mask = tf.keras.layers.Lambda(
create_look_ahead_mask,
output_shape=(1, None, None),
name='look_ahead_mask')(dec_inputs)
# mask the encoder outputs for the 2nd attention block
dec_padding_mask = tf.keras.layers.Lambda(
create_padding_mask, output_shape=(1, 1, None),
name='dec_padding_mask')(inputs)

enc_outputs = encoder(
vocab_size=vocab_size,
num_layers=num_layers,
units=units,
d_model=d_model,
num_heads=num_heads,
dropout=dropout,
)(inputs=[inputs, enc_padding_mask])

dec_outputs = decoder(
vocab_size=vocab_size,
num_layers=num_layers,
units=units,
d_model=d_model,
num_heads=num_heads,
dropout=dropout,
)(inputs=[dec_inputs, enc_outputs, look_ahead_mask, dec_padding_mask])

outputs = tf.keras.layers.Dense(units=vocab_size, name="outputs")(dec_outputs)

return tf.keras.Model(inputs=[inputs, dec_inputs], outputs=outputs, name=name)

我不明白为什么会出现此错误,因为模型训练得非常好。任何帮助将不胜感激。

我的保存代码供引用:

print("Saving the model.")
saveloc = "C:/tmp/solar.h5"
model.save(saveloc)
print("Model saved to: " + saveloc + " succesfully.")

最佳答案

这不是一个错误,而是一个功能。

此错误让您知道 TF 无法保存您的模型,因为它无法加载它。
具体来说,它将无法重新实例化您的自定义 Layer 类:encoderdecoder

要解决此问题,只需覆盖其 get_config方法根据您添加的新参数。

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

<小时/>

例如,如果您的 encoder 类如下所示:

class encoder(tf.keras.layers.Layer):

def __init__(
self,
vocab_size, num_layers, units, d_model, num_heads, dropout,
**kwargs,
):
super().__init__(**kwargs)
self.vocab_size = vocab_size
self.num_layers = num_layers
self.units = units
self.d_model = d_model
self.num_heads = num_heads
self.dropout = dropout

# Other methods etc.

那么你只需要重写这个方法:

    def get_config(self):

config = super().get_config().copy()
config.update({
'vocab_size': self.vocab_size,
'num_layers': self.num_layers,
'units': self.units,
'd_model': self.d_model,
'num_heads': self.num_heads,
'dropout': self.dropout,
})
return config

当 TF 看到此信息(对于两个类)时,您将能够保存模型。

因为现在加载模型时,TF 将能够从配置重新实例化同一层。

<小时/>

Layer.from_configsource code可以更好地理解它是如何工作的:

@classmethod
def from_config(cls, config):
return cls(**config)

关于python - 未实现错误: Layers with arguments in `__init__` must override `get_config` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678836/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com