【问题标题】:How to use GloVe word-embeddings file on Google colaboratory如何在 Google colaboratory 上使用 GloVe 词嵌入文件
【发布时间】:2018-10-08 04:07:17
【问题描述】:

我已经用 wget 下载了数据

!wget http://nlp.stanford.edu/data/glove.6B.zip
 - ‘glove.6B.zip’ saved [862182613/862182613]

它保存为 zip,我想使用 zip 文件中的 glove.6B.300d.txt 文件。我想要实现的是:

embeddings_index = {}
with io.open('glove.6B.300d.txt', encoding='utf8') as f:
    for line in f:
        values = line.split()
        word = values[0]
        coefs = np.asarray(values[1:],dtype='float32')
        embeddings_index[word] = coefs

当然我有这个错误:

IOErrorTraceback (most recent call last)
<ipython-input-47-d07cafc85c1c> in <module>()
      1 embeddings_index = {}
----> 2 with io.open('glove.6B.300d.txt', encoding='utf8') as f:
      3     for line in f:
      4         values = line.split()
      5         word = values[0]

IOError: [Errno 2] No such file or directory: 'glove.6B.300d.txt'

如何在 Google colab 上的上述代码中解压缩并使用该文件?

【问题讨论】:

标签: python google-colaboratory word-embedding


【解决方案1】:

您可以做的另一种方法如下。

1。下载压缩包

!wget http://nlp.stanford.edu/data/glove.6B.zip

下载后保存在 google Collab 的 /content 目录中的 zip 文件。

2。解压

!unzip glove*.zip

3。获取使用提取嵌入向量的确切路径

!ls
!pwd

4。索引向量

print('Indexing word vectors.')

embeddings_index = {}
f = open('glove.6B.100d.txt', encoding='utf-8')
for line in f:
    values = line.split()
    word = values[0]
    coefs = np.asarray(values[1:], dtype='float32')
    embeddings_index[word] = coefs
f.close()

print('Found %s word vectors.' % len(embeddings_index))

5。与谷歌融合 - 驱动器

!pip install --upgrade pip
!pip install -U -q pydrive
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null

!apt-get -y install -qq google-drive-ocamlfuse fuse

from google.colab import auth
auth.authenticate_user()
# Generate creds for the Drive FUSE library.
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

!mkdir -p drive
!google-drive-ocamlfuse drive

6。将索引向量保存到谷歌驱动器以供重复使用

import pickle
pickle.dump({'embeddings_index' : embeddings_index } , open('drive/path/to/your/file/location', 'wb'))

如果您已经在本地系统中下载了zip文件,只需将其解压缩并将所需的尺寸文件上传到google drive -> fuse gdrive -> 给出适当的路径然后使用它/对其进行索引等.

另外,如果已经通过协作中的代码在本地系统中下载了另一种方法

from google.colab import files
files.upload()

选择文件并在步骤 3 之后使用它。

这是您可以在 google 协作中使用 glove word embedding 的方法。希望有帮助。

【讨论】:

  • 如果我已经有文件,我尝试将其上传到 colab,即使是 50D 文件也需要很多时间。还有其他方法吗?另外如果我使用wget方法直接下载glove文件到colab,每次打开和关闭notebook都要下载吗?
【解决方案2】:

如果您有 Google 云端硬盘,您可以:

  1. 安装您的 Google 云端硬盘,以便在 Colab 笔记本中使用它

    from google.colab import drive
    drive.mount('/content/gdrive')
    
  2. 下载 glove.6B.zip 并将其解压缩到您在 Google 云端硬盘中选择的位置,例如

    "My Drive/Place/Of/Your/Choice/glove.6B.300d.txt"
    
  3. 直接从 Colab 笔记本打开文件

    with io.open('/content/gdrive/Place/Of/Your/Choice/glove.6B.300d.txt', encoding='utf8') as f:
    

【讨论】:

    【解决方案3】:

    很简单,从 SO 签出这个 older post

    import zipfile
    zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
    zip_ref.extractall(directory_to_extract_to)
    zip_ref.close()
    

    【讨论】:

    • 我想在 Google colab 上执行此操作。我不认为手套拉链会保存到我的电脑中。
    • 假设 zipfile 进入了wget 命令提到的当前目录,只需指定glove.6B.zip 作为路径——我认为它应该可以工作
    •  文件“”,第 2 行 zip_ref = zipfile.ZipFile(glove.6B.zip, 'r') ^ SyntaxError: invalid syntax}
    • 这需要更正为zipfile.ZipFile("glove.6B.zip", 'r') ,您已经注意到为文件名指定了"
    • 非常感谢您的指导!现在我没有遇到任何错误。我接受您的回答作为正确答案。祝你有美好的一天!
    猜你喜欢
    • 2023-01-12
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2022-08-18
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多