def load_bin_vec(fname, vocab):
 word_vecs = {}
 with open(fname, 'rb') as f:
  header = f.readline()
  (vocab_size, layer1_size) = map(int, header.split())
  binary_len = (np.dtype('float32').itemsize * layer1_size)
  for line in xrange(vocab_size):
   word = []
   while True:
    ch = f.read(1)
    if (ch == ' '):
     word = ''.join(word)
     break
    if (ch != '\n'):
     word.append(ch)
   if (word in vocab):
    word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32')
   else:
    f.read(binary_len)
 return word_vecs

def load_bin_vec ( fname , vocab ) : word_vecs = { } with open ( fname , 'rb' ) as f : header = f . readline ( ) ( vocab_size , layer1_size ) = map ( int , header . split ( ) ) binary_len = ( np . dtype ( 'float32' ) . itemsize * layer1_size ) for line in xrange ( vocab_size ) : word = [ ] while True : ch = f . read ( 1 ) if ( ch == ' ' ) : word = '' . join ( word ) break if ( ch != '\n' ) : word . append ( ch ) if ( word in vocab ) : word_vecs [ word ] = np . fromstring ( f . read ( binary_len ) , dtype = 'float32' ) else : f . read ( binary_len ) return word_vecs
loads 300x1 word vecs from google word2vec .