1

google colaboratory において単語ベクトルの計算をしようとGoogleNews-vectors-negative300を読み込んだところエラーが発生しました.
エラーメッセージは以下の通りです.gzファイルでないといわれていますが,gzファイルを読み込んでいるはずなので,どこがエラーの原因であるか分かってない状況です.(コードはメッセージの下に書いています.)

よろしければお知恵を貸していただければと考えています.

【言語処理100本ノック 2020】第7章: 単語ベクトル

を参考にしています.

OSError                                   Traceback (most recent call last)
<ipython-input-23-87b25b74c23d> in <module>()
      2 import gensim
      3 filename = '/GoogleNews-vectors-negative300.bin.gz'
----> 4 model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)

5 frames
/usr/local/lib/python3.7/dist-packages/gensim/models/keyedvectors.py in load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype)
   1436         return _load_word2vec_format(
   1437             cls, fname, fvocab=fvocab, binary=binary, encoding=encoding, unicode_errors=unicode_errors,
-> 1438             limit=limit, datatype=datatype)
   1439 
   1440     def get_keras_embedding(self, train_embeddings=False):

/usr/local/lib/python3.7/dist-packages/gensim/models/utils_any2vec.py in _load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype)
    170     logger.info("loading projection weights from %s", fname)
    171     with utils.smart_open(fname) as fin:
--> 172         header = utils.to_unicode(fin.readline(), encoding=encoding)
    173         vocab_size, vector_size = (int(x) for x in header.split())  # throws for invalid file format
    174         if limit:

/usr/lib/python3.7/gzip.py in readline(self, size)
    383     def readline(self, size=-1):
    384         self._check_not_closed()
--> 385         return self._buffer.readline(size)
    386 
    387 

/usr/lib/python3.7/_compression.py in readinto(self, b)
     66     def readinto(self, b):
     67         with memoryview(b) as view, view.cast("B") as byte_view:
---> 68             data = self.read(len(byte_view))
     69             byte_view[:len(data)] = data
     70         return len(data)

/usr/lib/python3.7/gzip.py in read(self, size)
    472                 # jump to the next member, if there is one.
    473                 self._init_read()
--> 474                 if not self._read_gzip_header():
    475                     self._size = self._pos
    476                     return b""

/usr/lib/python3.7/gzip.py in _read_gzip_header(self)
    420 
    421         if magic != b'\037\213':
--> 422             raise OSError('Not a gzipped file (%r)' % magic)
    423 
    424         (method, flag,

OSError: Not a gzipped file (b'<!')

コードです.

FILE_ID = "0B7XkCwpI5KDYNlNUTTlSS21pQmM"
FILE_NAME = "GoogleNews-vectors-negative300.bin.gz"
!wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=$FILE_ID' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=$FILE_ID" -O $FILE_NAME && rm -rf /tmp/cookies.txt

from gensim.models import KeyedVectors
import gensim
filename = '/GoogleNews-vectors-negative300.bin.gz'
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
4
  • 実際にダウンロードされたファイル(GoogleNews-vectors-negative300.bin.gz)の中身を確認してみると HTML ファイルになっていて、"Sorry, you can't view or download this file at this time." と書かれています。なお、同じ内容のファイルが https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz にあります。
    – metropolis
    2021年7月21日 19:31
  • 上げてもらったサイトからダウンロードすればうまくいきました.ありがとうございます. 2021年7月22日 3:41
  • すいません,もう一つ質問なのですが他のサイトの説明では上のコードで回っているようなのですが,どのような理由でこのような違いが出るのでしょうか 2021年7月22日 4:00
  • いつのことなのかは判りませんが、当該のファイルが削除されてしまったのではないでしょうか。
    – metropolis
    2021年7月22日 5:03

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。