简体   繁体   中英

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. 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

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.

See this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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