(fname, vocab, index_from=2)
return W, embedding_dim, found_words
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>)
    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, index_from=2):
    """
    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>)
    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()
    orig_w2v_size, embedding_dim = map(int, header.split())
    binary_len = np.dtype('float32').itemsize * embedding_dim
    vocab_size = len(vocab) + index_from
    W = np.zeros((vocab_size, embedding_dim))

    found_words_idx = defaultdict(int)
    found_words = defaultdict(int)

    for i in range(vocab_size):
        word = []
        while True:
            ch = f.read(1)
            if ch == b' ':
                word = (b''.join(word)).decode('utf-8')
                break
            if ch != b'\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_idx[wrd_id] += 1
                found_words[word] += 1
        else:
            f.read(binary_len)

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

    unfound_words = list()
    for wrd in vocab:
        if wrd not in found_words:
            unfound_words += [wrd]

    assert cnt + len(found_words_idx) == vocab_size

    f.close()

    return W, embedding_dim, found_words
def get_google_word2vec_W ( fname , vocab , index_from = 2 ) : f = open ( fname , 'rb' ) header = f . readline ( ) orig_w2v_size , embedding_dim = map ( int , header . split ( ) ) binary_len = np . dtype ( 'float32' ) . itemsize * embedding_dim vocab_size = len ( vocab ) + index_from W = np . zeros ( ( vocab_size , embedding_dim ) ) found_words_idx = defaultdict ( int ) found_words = defaultdict ( int ) for i in range ( vocab_size ) : word = [ ] while True : ch = f . read ( 1 ) if ch == b' ' : word = ( b'' . join ( word ) ) . decode ( 'utf-8' ) break if ch != b'\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_idx [ wrd_id ] += 1 found_words [ word ] += 1 else : f . read ( binary_len ) cnt = 0 for wrd_id in range ( vocab_size ) : if wrd_id not in found_words_idx : cnt += 1 W [ wrd_id ] = np . random . uniform ( - 1.0 , 1.0 , embedding_dim ) unfound_words = list ( ) for wrd in vocab : if wrd not in found_words : unfound_words += [ wrd ] assert cnt + len ( found_words_idx ) == vocab_size f . close ( ) return W , embedding_dim , found_words