简体   繁体   中英

File not found in pickle

I want to save my Machine learning mode using pickle

using this code

picklepath='project_folder/cache/'
def get_model():
    try:
        foo = pickle.load(open(picklepath+'f_path.pkl', "rb"))
        return foo
    except (OSError, IOError) as e:
        model=Model("model")
        pickle.dump(model, open(picklepath+'f_path.pkl', "wb"))
        return model

model = get_model()

on running my application getting this error:

pickle.dump(model, open(picklepath+'f_path.pkl', "wb"))\r: C:/...../example/index.py
[Fri Dec 10 11:41:07.530565 2021] [cgi:error] [pid 3728:tid 1188] [client ::1:49914] AH01215: FileNotFoundError: [Errno 2] No such file or directory: '

Any solution, Thanks

Assuming you are using Keras for ML model, Instead of using pickle directly why not use the Keras built-in model loading and saving functions

model = Sequential()
#model initializeation, layers and compiling goes here

#saving the weights of model
model.save_weights("project_folder/cache/model.h5")

#later when you want it just initialize empty model as usual
model = Sequential()
#your layers defination goes here
model.load_weights("project_folder/cache/model.h5")

If you want to skip the model creation and layers defining each time you can save it's structure in the form of json

with open("project_folder/cache/model.json", "w") as json_file:
    json_file.write(model.to_json())

#and later to load model structure
from keras.models import model_from_json
json_file = open('project_folder/cache/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM