在 Keras NLP 中保存 TransformerDecoder 模型时出错:带有非 JSON 可序列化参数的类型错误

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

我在从

TransformerDecoder
保存
keras_nlp
模型时遇到问题。您能告诉我如何解决这个问题吗?

这是我的代码:

from keras_nlp.layers import TransformerDecoder
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D, Input

decoder_input = Input(shape=(128,), name="decoder_input")
embedding = Embedding(input_dim=10000, output_dim=32, name="embedding")(decoder_input)
decoder = TransformerDecoder(intermediate_dim=64, num_heads=2, name="decoder")(embedding)
pooled = GlobalAveragePooling1D(name="pooling")(decoder)
output = Dense(token_size, activation="softmax", name="output_dense")(pooled)
model = Model(inputs=decoder_input, outputs=output)

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

model.save("model.keras")

我收到错误:

TypeError: Layer tf.__operators__.add was passed non-JSON-serializable arguments. 
Arguments had types: {'y': <class 'keras.src.backend.tensorflow.core.Variable'>, 'name': <class 'NoneType'>}. 
They cannot be serialized out when saving the model.

软件包版本:

print(tensorflow.__version__)
print(keras_nlp.__version__)
# 2.17.0
# 0.14.1
tensorflow keras keras-nlp
1个回答
0
投票

我无法在协作环境中重现该错误,它确实适用于指定的软件包版本tensorflow 2.17.0和keras_nlp 0.14.1。

result of the execution of the code in a collab environment

© www.soinside.com 2019 - 2024. All rights reserved.