我已经用wget下载了数据
!wget http://nlp.stanford.edu/data/glove.6B.zip
- ‘glove.6B.zip’ saved [862182613/862182613]它被保存为压缩文件,我想使用压缩文件中的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上解压并使用上面代码中的文件?
发布于 2018-04-27 18:20:13
这很简单,从SO中检出这个older post。
import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()发布于 2018-09-03 18:42:50
另一种你可以做的方法如下。
1.下载zip文件
!wget http://nlp.stanford.edu/data/glove.6B.zip下载压缩文件后,它将保存在google Collab的/content目录中。
2.解压缩
!unzip glove*.zip3.获取嵌入向量提取位置的确切路径
!ls
!pwd4.索引向量
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 - drive融合
!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 drive6.将索引后的向量保存到google驱动器中,以便重复使用
import pickle
pickle.dump({'embeddings_index' : embeddings_index } , open('drive/path/to/your/file/location', 'wb'))如果您已经在本地系统中下载了压缩文件,只需将其解压缩并将所需的维度文件上传到google drive -> fuse gdrive ->,然后给出适当的路径,然后使用它/建立索引,依此类推。
此外,如果已经通过collab中的代码下载到本地系统中,则可以使用另一种方法
from google.colab import files
files.upload()选择该文件并使用它,如步骤3所示。
这就是在google collaboratory中使用glove word嵌入的方法。希望能有所帮助。
发布于 2018-11-09 22:36:39
如果您有Google Drive,您可以:
例如,从google.colab导入驱动器drive.mount('/content/gdrive')
“我的Drive/Place/Of/Your/Choice/glove.6B.300d.txt"
使用io.open('/content/gdrive/Place/Of/Your/Choice/glove.6B.300d.txt',编码=‘utf8’)作为f:
https://stackoverflow.com/questions/50060241
复制相似问题