yanwencai001 2019-05-23 11:03 采纳率: 0%
浏览 13970

TypeError: expected str, bytes or os.PathLike object, not tuple此报错有大神遇到过吗怎么解决

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
  • 写回答

2条回答

  • 黄嘻嘻 2020-08-18 10:15
    关注

    你是不是在文件路径后面多写了一个逗号?

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗