TensorFlow 保存模型错误“无法同步创建数据集(名称已存在)”

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

`我开发了一个人工智能模型并成功训练了它,但是,我在尝试保存模型时遇到了问题。错误消息指出 **h5 ** 文件名已存在,但情况似乎不一致,因为无论文件是否存在,错误仍然存在。”有趣的是,当我在其他项目中测试类似代码时,并没有遇到这个问题。

Error: Unable to synchronously create dataset (name already exists)
Traceback (most recent call last):
  File "C:\Users\Lenovo-Z\Documents\Text\Voice Line\main.py", line 333, in main
    model.save('VoiceLine_Model.h5')
  File "C:\Users\Lenovo-Z\.conda\envs\voiceline_myenv2\lib\site-packages\keras\src\utils\traceback_utils.py", line 123, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Lenovo-Z\AppData\Roaming\Python\Python310\site-packages\h5py\_hl\group.py", line 183, in create_dataset
    dsid = dataset.make_new_dset(group, shape, dtype, data, name, **kwds)
  File "C:\Users\Lenovo-Z\AppData\Roaming\Python\Python310\site-packages\h5py\_hl\dataset.py", line 163, in make_new_dset
    dset_id = h5d.create(parent.id, name, tid, sid, dcpl=dcpl, dapl=dapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5d.pyx", line 137, in h5py.h5d.create
ValueError: Unable to synchronously create dataset (name already exists)

code:

def save_artifacts(tokenizer, encoder, model, embedding_matrix):
    # Save tokenizer as JSON
    tokenizer_data = {
        "word_index": tokenizer.word_index,
        "index_word": tokenizer.index_word,
        "word_counts": tokenizer.word_counts,
        "document_count": tokenizer.document_count
    }
    with open("tokenizer.pkl", "wb") as tokenizer_file:
        pickle.dump(tokenizer_data, tokenizer_file)

    # Save label encoder using pickle
    with open("label_encoder.pkl", "wb") as label_file:
        pickle.dump(encoder, label_file)

    # Save model architecture as JSON
    model_json = model.to_json()
    with open("model_architecture.json", "w") as json_file:
        json_file.write(model_json)

    # Save words using pickle
    with open("words.pkl", "wb") as words_file:
        pickle.dump(tokenizer.word_index, words_file)

    # Save classes using pickle
    with open("classes.pkl", "wb") as classes_file:
        pickle.dump(encoder.classes_, classes_file)

    # Save embedding matrix
    np.save("embedding_matrix.npy", embedding_matrix)

...

model = build_combined_model(embedding_dim=EMBEDDING_DIM, num_classes=len(encoder.classes_), vocab_size=len(tokenizer.word_index) + 1)

compile_model(model)

callbacks = get_callbacks()

model.fit(train_tokens, train_labels_one_hot, epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(test_tokens, test_labels_one_hot), callbacks=callbacks)

print("Model training completed.")
model.save('VoiceLine_Model.h5')

python tensorflow artificial-intelligence
1个回答
0
投票

我在尝试保存模型时遇到错误

ValueError: Unable to synchronously create dataset (name already exists)

my_model.save('filename.h5')

...但是当我使用

keras
作为文件扩展名时,保存成功了:

my_model.save('filename.keras')
© www.soinside.com 2019 - 2024. All rights reserved.