我试图在我的VQA项目中实现跳过思想向量,但是我在从指定的位置读取文件时遇到了问题。这是一段跳过的代码。
path_to_models = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought'
path_to_tables = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought'
path_to_umodel = path_to_models + 'uni_skip.npz'
path_to_bmodel = path_to_models + 'bi_skip.npz'
def load_model():
"""
Load the model with saved tables
"""
# Load model options
print ('Loading model parameters...')
with open('%s.pkl'%path_to_umodel, 'rb') as f:
uoptions = pkl.load(f)
with open('%s.pkl'%path_to_bmodel, 'rb') as f:
boptions = pkl.load(f)这是我调用skipthoughts.py时的错误消息
File "C:/Users/Downloads/Stacked Attention
Networks/skip_thought/new_skip_thought.py", line 11, in <module>
model = skipthoughts.load_model()
File "skipthoughts.py", line 37, in load_model
with open('%s.pkl'%path_to_umodel, 'rb') as f:
IOError: [Errno 2] No such file or directory:
'C:/Users/Downloads/Stacked Attention Networks/skip_thoughtuni_skip.npz.pkl'文件位置不正确。
‘c:/用户/下载/注意力叠加Networks/skip_thoughtuni_skip.npz.pkl’
它应该是
‘c:/用户/下载/注意力叠加Networks/skip_thought/uni_skip.npz.pkl’
发布于 2017-05-03 07:05:54
程序中唯一的问题是保存在path_to_umodel和path_to_bmodel中的文件路径错误。为了解决这个问题,您可以修改变量path_to_models和path_to_tables,在它们的末尾添加一个/,如下所示:
path_to_models = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought/'和
path_to_tables = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought/'或者可以将/添加到uni_skip.npz和bi_skip.npz的开头,然后,这些行变成:
path_to_umodel = path_to_models + '/uni_skip.npz'
path_to_bmodel = path_to_models + '/bi_skip.npz'更好的是,您可以使用内置到python的os模块中的os函数。只需在程序开始时使用import os,并将其用作:
path_to_umodel = os.path.join(path_to_models, os.sep, 'uni_skip.npz')
path_to_bmodel = os.path.join(path_to_models, os.sep, 'bi_skip.npz')在这里,os.sep是在运行代码的平台上使用的文件分隔符。对于windows,它是\,对于linux和unix,它是/。最好是使用os.sep,而不是用硬编码代替它。
发布于 2017-05-03 06:43:44
您需要使用os.path.join,如下所示:
import os
path_to_models = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought'
path_to_tables = 'C:/Users/Downloads/Stacked Attention Networks/skip_thought'
path_to_umodel = os.path.join(path_to_models, 'uni_skip.npz')
path_to_bmodel = os.path.join(path_to_models, 'bi_skip.npz')https://stackoverflow.com/questions/43752778
复制相似问题