[英]No such file or directory while working with Glove (Deep learning)
我目前正在嘗試從 model 添加從 Glove 嵌入的預加載,並且似乎無法加載手套文本文件來解析它的數據。 我總是收到找不到文件的錯誤。
我的代碼如下:
outname = 'glove.6B.100d.txt'
outdir = './Downloads'
if not os.path.exists(outdir):
os.mkdir(outdir)
fullname = os.path.join(outdir, outname)
def getEmbeddedMatrix(num_words, embedding_size):
embeddings_index = {}
with open(fullname, "r") as file_directory:
for line in file_directory:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
embedding_matrix = np.zeros((num_words, embedded_size))
for word, index in imdb.get_word_index.items():
if index >= num_words:
continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
return embedding_matrix
我得到的錯誤信息如下:
[Errno 2] No such file or directory: './Downloads/glove.6B.100d.txt'
關於在這里做什么的任何建議? .txt 文件目前在我的下載中,我正在使用 Google Colab
編輯:
我也嘗試過執行以下操作,但仍然會引發相同的錯誤
def getEmbeddedMatrix(num_words, embedding_size):
embeddings_index = {}
File_object = open(r"/Users/______/downloads/glove.6B.100d.txt","r")
with File_object as file_directory:
for line in file_directory:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
embedding_matrix = np.zeros((num_words, embedded_size))
for word, index in imdb.get_word_index.items():
if index >= num_words:
continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
return embedding_matrix
您必須將文本文件上傳到 google-colab 的工作文件夾,而不是桌面的下載文件夾。
請參閱此鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.