如何在 Google colaboratory 上使用 GloVe 词嵌入文件

How to use GloVe word-embeddings file on Google colaboratory

我已经用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 的上述代码中解压缩并使用该文件?

很简单,从 SO 中检查这个 older post

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

另一种方法如下。

1。下载 zip 文件

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

post 下载保存在 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。与 google 融合 - 驱动器

!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。将索引向量保存到 google 驱动器以供重复使用

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

如果您已经在本地系统中下载了zip文件,只需解压并上传所需的维度文件到google驱动器->融合gdrive->给出合适的路径,然后使用/make它的索引等

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

from google.colab import files
files.upload()

select 文件并在第 3 步之后使用它。

这是在 google 合作实验室中使用手套词嵌入的方法。希望对你有帮助。

如果您有 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: