(fname, vocab, vocab_size=1000000, index_from=3)
return W, embedding_dim, vocab_size
Extract the embedding matrix from the given word2vec binary file and use this
    to initalize a new embedding matrix for words found in vocab.

    Conventions are to save indices for pad, oov, etc.:
    index 0: pad
    index 1: oov (or <unk>)
    index 2: <eos>. But often cases, the <eos> has already been in the
    preprocessed data, so no need to save an index for <eos>
Extract the embedding matrix from the given word2vec binary file and use this
    to initalize a new embedding matrix for words found in vocab.
Extract the embedding matrix from the given word2vec binary file and use this to initalize a new embedding matrix for words found in vocab .
def get_google_word2vec_W(fname, vocab, vocab_size=1000000, index_from=3):
    """
    Extract the embedding matrix from the given word2vec binary file and use this
    to initalize a new embedding matrix for words found in vocab.

    Conventions are to save indices for pad, oov, etc.:
    index 0: pad
    index 1: oov (or <unk>)
    index 2: <eos>. But often cases, the <eos> has already been in the
    preprocessed data, so no need to save an index for <eos>
    """
    f = open(fname, 'rb')
    header = f.readline()
    vocab1_size, embedding_dim = list(map(int, header.split()))
    binary_len = np.dtype('float32').itemsize * embedding_dim
    vocab_size = min(len(vocab) + index_from, vocab_size)
    W = np.zeros((vocab_size, embedding_dim))

    found_words = {}
    for i, line in enumerate(range(vocab1_size)):
        word = []
        while True:
            ch = f.read(1)
            if ch == b' ':
                word = b''.join(word)
                break
            if ch != '\n':
                word.append(ch)
        if word in vocab:
            wrd_id = vocab[word] + index_from
            if wrd_id < vocab_size:
                W[wrd_id] = np.fromstring(
                    f.read(binary_len), dtype='float32')
                found_words[wrd_id] = 1
        else:
            f.read(binary_len)

    cnt = 0
    for wrd_id in range(vocab_size):
        if wrd_id not in found_words:
            W[wrd_id] = np.random.uniform(-0.25, 0.25, embedding_dim)
            cnt += 1
    assert cnt + len(found_words) == vocab_size

    f.close()

    return W, embedding_dim, vocab_size
def get_google_word2vec_W ( fname , vocab , vocab_size = 1000000 , index_from = 3 ) : f = open ( fname , 'rb' ) header = f . readline ( ) vocab1_size , embedding_dim = list ( map ( int , header . split ( ) ) ) binary_len = np . dtype ( 'float32' ) . itemsize * embedding_dim vocab_size = min ( len ( vocab ) + index_from , vocab_size ) W = np . zeros ( ( vocab_size , embedding_dim ) ) found_words = { } for i , line in enumerate ( range ( vocab1_size ) ) : word = [ ] while True : ch = f . read ( 1 ) if ch == b' ' : word = b'' . join ( word ) break if ch != '\n' : word . append ( ch ) if word in vocab : wrd_id = vocab [ word ] + index_from if wrd_id < vocab_size : W [ wrd_id ] = np . fromstring ( f . read ( binary_len ) , dtype = 'float32' ) found_words [ wrd_id ] = 1 else : f . read ( binary_len ) cnt = 0 for wrd_id in range ( vocab_size ) : if wrd_id not in found_words : W [ wrd_id ] = np . random . uniform ( - 0.25 , 0.25 , embedding_dim ) cnt += 1 assert cnt + len ( found_words ) == vocab_size f . close ( ) return W , embedding_dim , vocab_size