使用joblib.dump()保存模型后,再使用joblib.load()读取模型,当是简单的模型可以读取,例如线性回归,分类问题等,但是一旦遇到多层模型后保存模型一切正常,但是joblib.load()读取就找不到文件
# 选择模型
from keras.models import Sequential
from keras.layers import Dense,Activation
mlp = Sequential()
# units隐藏层有多少个神经元 input_dim有几个位置参数这里x1,x2 activation激活函数
mlp.add(Dense(units=20,input_dim=2,activation='sigmoid'))
mlp.add(Dense(units=1,activation='sigmoid'))
mlp.summary()
# 配置模型训练参数 例如损失函数,优化方法等
mlp.compile(optimizer='adam',loss = 'binary_crossentropy')
# 模型训练传递数据以及模型训练迭代次数
mlp.fit(x_train,y_train,epochs=10000)
import joblib
joblib.dump(mlp,'mlp.h5')
import joblib
mlp2 = joblib.load('mlp.h5')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Input In [11], in line: 2>()
1 import joblib
----> 2 mlp2 = joblib.load('mlp.h5')
File D:\work\anaconda\envs\imooc_ai\lib\site-packages\joblib\numpy_pickle.py:585, in load(filename, mmap_mode)
579 if isinstance(fobj, str):
580 # if the returned file object is a string, this means we
581 # try to load a pickle file generated with an version of
582 # Joblib so we load it with joblib compatibility function.
583 return load_compatibility(fobj)
--> 585 obj = _unpickle(fobj, filename, mmap_mode)
586 return obj
File D:\work\anaconda\envs\imooc_ai\lib\site-packages\joblib\numpy_pickle.py:504, in _unpickle(fobj, filename, mmap_mode)
502 obj = None
503 try:
--> 504 obj = unpickler.load()
505 if unpickler.compat_mode:
506 warnings.warn("The file '%s' has been generated with a "
507 "joblib version less than 0.10. "
508 "Please regenerate this pickle file."
509 % filename,
510 DeprecationWarning, stacklevel=3)
File D:\work\anaconda\envs\imooc_ai\lib\pickle.py:1213, in _Unpickler.load(self)
1211 raise EOFError
1212 assert isinstance(key, bytes_types)
-> 1213 dispatch[key[0]](self)
1214 except _Stop as stopinst:
1215 return stopinst.value
File D:\work\anaconda\envs\imooc_ai\lib\pickle.py:1590, in _Unpickler.load_reduce(self)
1588 args = stack.pop()
1589 func = stack[-1]
-> 1590 stack[-1] = func(*args)
File D:\work\anaconda\envs\imooc_ai\lib\site-packages\keras\saving\pickle_utils.py:47, in deserialize_model_from_bytecode(serialized_model)
45 with tf.io.gfile.GFile(dest_path, "wb") as f:
46 f.write(archive.extractfile(name).read())
---> 47 model = save_module.load_model(temp_dir)
48 tf.io.gfile.rmtree(temp_dir)
49 return model
File D:\work\anaconda\envs\imooc_ai\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback. .error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File D:\work\anaconda\envs\imooc_ai\lib\site-packages\tensorflow\python\saved_model\load.py:933, in load_partial(export_dir, filters, tags, options)
930 loader = Loader(object_graph_proto, saved_model_proto, export_dir,
931 ckpt_options, options, filters)
932 except errors.NotFoundError as err:
--> 933 raise FileNotFoundError(
934 str(err) + "\n You may be trying to load on a different device "
935 "from the computational device. Consider setting the "
936 "`experimental_io_device` option in `tf.saved_model.LoadOptions` "
937 "to the io_device such as '/job:localhost'.")
938 root = loader.get(0)
939 root.graph_debug_info = loader.adjust_debug_info_func_names(debug_info)
FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ram://d15d67b8-99bd-44be-ab65-451b5e08be7f/variables/variables
You may be trying to load on a different device from the computational device. Consider setting the `experimental_io_device` option in `tf.saved_model.LoadOptions` to the io_device such as '/job:localhost'.
|
尝试了不同版本的joblib,尝试了绝对路径,尝试了./文件名,尝试了pkl .m .h5等文件
可以正常的读取模型