简体   繁体   English

使用 Glove 时没有这样的文件或目录(深度学习)

[英]No such file or directory while working with Glove (Deep learning)

I am currently trying to add a preloaded embedded from Glove from a model and can't seem to load the glove text file to parse it's data.我目前正在尝试从 model 添加从 Glove 嵌入的预加载,并且似乎无法加载手套文本文件来解析它的数据。 I always get the file not found error.我总是收到找不到文件的错误。

My code is as follows:我的代码如下:

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

The error message I get is as follows:我得到的错误信息如下:

[Errno 2] No such file or directory: './Downloads/glove.6B.100d.txt'

Any suggestions on what to do here?关于在这里做什么的任何建议? The.txt file is currently in my Downloads and I am using Google Colab .txt 文件目前在我的下载中,我正在使用 Google Colab

Edit:编辑:

I have also tried doing the following but it still throws up the same error我也尝试过执行以下操作,但仍然会引发相同的错误

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

You have to upload the text file at google-colab's working folder, instead of your Desktop's Downloads folder.您必须将文本文件上传到 google-colab 的工作文件夹,而不是桌面的下载文件夹。

See this link.请参阅链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM