我最近用joblib将一个模型保存到了s3中。
model_doc是模型对象
import subprocess
import joblib
save_d2v_to_s3_current_doc2vec_model(model_doc,"doc2vec_model")
def save_d2v_to_s3_current_doc2vec_model(model,fname):
model_name = fname
joblib.dump(model,model_name)
s3_base_path = 's3://sd-flikku/datalake/current_doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(model_name,path).split()
print('saving...'+model_name)
subprocess.call(command)
它是成功的,但在那之后,当我试图从s3加载模型回来,它给我一个错误。
model = load_d2v("doc2vec_model")
def load_d2v(fname):
model_name = fname
s3_base_path='s3://sd-flikku/datalake/current_doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(path,model_name).split()
print('loading...'+model_name)
subprocess.call(command)
model=joblib.load(model_name)
return model
这是我得到的错误。
loading...doc2vec_model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in load_d2v
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 339, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
我甚至不明白为什么它说找不到文件 这是我用来保存模型的路径 但现在我无法从s3中找回模型。请帮助我!
我建议,比起你的通用的 print()
行,表明您的意图,您应该打印出 实际 command
你写的,以验证其观察后是否有意义。
如果有道理,那么也可以试试 一模一样 aws ...
指挥 直接在命令提示符下启动你的 python
代码,以确保它以这种方式运行。如果不是,你可能会得到一个更清晰的错误。
请注意,你得到的错误看起来并不特别像是来自于 aws
命令,而不是来自 S3 服务的命令 - 它可能会谈论 "路径 "或 "对象"。相反,它是来自Python的 subprocess
system &.subprocess.call() Popen' call. I think those are via your call to
子进程.调用(), but for some reason your line-of-code isn't shown. (How are you running the block of code with the
load_d2v()`?)
这说明没有找到的文件可能是 aws
命令本身。你确定它是在你的Python运行的工作目录环境中安装的& 可运行的,并且通过 subprocess.call()
?
(另外,如果我的 上一个答案 让你在你的 sklearn.externals.joblib
问题,你最好把答案标记为已接受,省得其他潜在的回答者以为这还是个未解的问题,把你挡住了)。)
试着将你的模型文件的扩展名添加到你的 fname
如果您确信模型文件在那里,例如doc2vec_model.h3。