import gensim
import torch
from util import read_caption_data, char_table_to_sentence, word2vec
from torch.utils.serialization import load_lua
import os
def save_embeddings(filepath, filename, embeddings):
if not os.path.exists(filepath):
os.mkdir(filepath)
target_path = os.path.join(filepath, filename)
torch.save({'embeds': embeddings}, target_path) # 保存整个网络,包括整个计算图
return True
class Word_Embeddings():
def __init__(self, root_dir, caption_dir, split_file, alphabet):
self.caption_dir = caption_dir
self.split_file = split_file
self.alphabet = alphabet
self.dir_path = os.path.join(root_dir, 'pretrained_embeddings')
self.model = gensim.models.KeyedVectors.load_word2vec_format('/data0/Masters/yanwencai/CrossModalRetrieval-master/models/GoogleNews-vectors-negative300.bin',
binary=True) #加载Google训练的词向量
def load_caption(self, cap):
assert (os.path.isfile(cap))
cls, fn = cap.split('/')[-2], cap.split('/')[-1]
file_path = os.path.join((self.dir_path, cls)) #报错行
caption = load_lua(cap)
char = caption['char']
sentence = char_table_to_sentence(self.alphabet, char)
embeds = word2vec(self.model, sentence, sen_size=16, emb_size=300)
return file_path, fn, embeds
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\|_@#$%^&*~`+-=<>()[]{} "
data_dir = '/data0/Masters/yanwencai/CrossModalRetrieval-master/datasets/CUB_200_2011/'
split_file = os.path.join(data_dir, 'train_val.txt')
caption_dir = os.path.join(data_dir, 'cub_icml')
caption_list = read_caption_data(caption_dir, split_file)
WE = Word_Embeddings(data_dir, caption_dir, split_file, alphabet)
for idx, cap in enumerate(caption_list):
filepath, fn, embeds = WE.load_caption(cap) #报错行
if save_embeddings(filepath, fn, embeds):
print(filepath, fn, 'saved')
错误描述:Traceback (most recent call last):
File "word_embeddings.py", line 61, in <module>
filepath, fn, embeds,type = WE.load_caption(cap)
File "word_embeddings.py", line 36, in load_caption
file_path,type = os.path.join((self.dir_path, cls))
File "/home/amax/anaconda2/lib/python3.6/posixpath.py", line 80, in join
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not tuple
请各位大神帮忙看一下,xie'xie
你是不是在文件路径后面多写了一个逗号?
需要字符串,字节,或者路径对象,而不是元组,posixpath.py的第80行,好好检查一下