python - 如何在训练时尝试保存模型权重时修复“无法创建文件”错误
问题描述
我在训练时正在做图像字幕项目我尝试在每个时期后通过 model.save('./model_weights1/model_'+ str(i) + '.h5') 保存模型重量,但我得到错误
它返回
无法创建文件(无法打开文件:name = './model_weights1/model_0.h5',errno = 2,错误消息 = '没有这样的文件或目录',flags = 13,o_flags = 602)
我检查了目录路径是否存在,路径似乎也正确
代码:
for i in range(epochs):
generator = data_loader(train_descriptions,encoded_images,maxlen,batch_size,word_to_idx)
model.fit_generator(generator,epochs=1,steps_per_epoch=steps,verbose = 1)
model.save('./model_weights1/model_'+ str(i) + '.h5')
错误
OSError Traceback (most recent call last)
<ipython-input-58-00c04165c7ca> in <module>()
2 generator = data_loader(train_descriptions,encoded_images,maxlen,batch_size,word_to_idx)
3 model.fit_generator(generator,epochs=1,steps_per_epoch=steps,verbose = 1)
----> 4 model.save('./model_weights1/model_'+ str(i) + '.h5',overwrite=True)
~/anaconda3/lib/python3.6/site-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
1088 raise NotImplementedError
1089 from ..models import save_model
-> 1090 save_model(self, filepath, overwrite, include_optimizer)
1091
1092 def save_weights(self, filepath, overwrite=True):
~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
377 opened_new_file = False
378
--> 379 f = h5dict(filepath, mode='w')
380
381 try:
~/anaconda3/lib/python3.6/site-packages/keras/utils/io_utils.py in __init__(self, path, mode)
184 self._is_file = False
185 elif isinstance(path, str):
--> 186 self.data = h5py.File(path, mode=mode)
187 self._is_file = True
188 elif isinstance(path, dict):
~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
267 with phil:
268 fapl = make_fapl(driver, libver, **kwds)
--> 269 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
270
271 if swmr_support:
~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
103 fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
104 elif mode == 'w':
--> 105 fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
106 elif mode == 'a':
107 # Open in append mode (read/write).
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/h5f.pyx in h5py.h5f.create()
OSError: Unable to create file (unable to open file: name = './model_weights1/model_0.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 602)
解决方案
如果你只是想节省重量,比做起来更容易
import os
然后定义您的 wieghts_path 并保存它们:
weights_path = os.getcwd() + "\\model_weights\\your_model.h5"
model.save_weights(weights_path)
推荐阅读
- javascript - 如何在jquery的下拉菜单中构造一个带有字母的单词
- python - 我希望使用 python 在 selenium 中单击一个按钮。我得到一个错误“列表”对象没有属性“点击”
- wordpress - 在 Apache 上下文中从 bash 脚本调用时,防止 wp-cli 将 gzip'ed 流写入标准输出?
- python-3.x - 需要通过带有上传文件选项的机器人框架发出 POST 请求
- javascript - 有什么办法可以循环这个吗?
- java - jsf中不能调用Bean?
- lua - '结束'预计在'附近'
' 我的代码中的错误 - java - 如何使用 Azure Java SDK 从 Java 后端连接到 Azure Key Vault?
- python - Scipy.linalg 矩阵乘法实现,全部在 C 中还是在 Fortran 中?
- angular - 无法到达这一边 - Windows 10 上的 docker 中的角度