beg*_*ner 3 python word-embedding google-colaboratory
我已经用wget下载了数据
!wget http://nlp.stanford.edu/data/glove.6B.zip
- ‘glove.6B.zip’ saved [862182613/862182613]
Run Code Online (Sandbox Code Playgroud)
它另存为zip,我想使用zip文件中的Gloves.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
Run Code Online (Sandbox Code Playgroud)
我当然有这个错误:
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'
Run Code Online (Sandbox Code Playgroud)
如何在上面的Google colab代码中解压缩并使用该文件?
小智 9
您可以做的另一种方法如下。
!wget http://nlp.stanford.edu/data/glove.6B.zip
Run Code Online (Sandbox Code Playgroud)
下载zip文件后,将其保存在google collab的/ content目录中。
!unzip glove*.zip
Run Code Online (Sandbox Code Playgroud)
!ls
!pwd
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
!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
Run Code Online (Sandbox Code Playgroud)
import pickle
pickle.dump({'embeddings_index' : embeddings_index } , open('drive/path/to/your/file/location', 'wb'))
Run Code Online (Sandbox Code Playgroud)
如果您已经在本地系统中下载了zip文件,只需将其解压缩,然后将所需的尺寸文件上传到google drive->保险丝gdrive->提供适当的路径,然后使用它/为其建立索引,依此类推。
同样,另一种方式是如果已经通过collab中的代码下载到本地系统中
from google.colab import files
files.upload()
Run Code Online (Sandbox Code Playgroud)
选择该文件并按照第3步开始使用它。
这就是您如何在Google合作实验室中使用手套词嵌入的方法。希望能帮助到你。
小智 4
很简单,查看SO 的这篇旧帖子。
import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4681 次 |
| 最近记录: |