def load_with_vocab(fin, vocab, dtype=np.float32):
arr = None<EOL>for line in fin:<EOL><INDENT>try:<EOL><INDENT>token, v = _parse_line(line, dtype)<EOL><DEDENT>except (ValueError, IndexError):<EOL><INDENT>raise ParseError(b'<STR_LIT>' + line)<EOL><DEDENT>if token in vocab:<EOL><INDENT>if arr is None:<EOL><INDENT>arr = np.empty((len(vocab), len(v)), dtype=dtype)<EOL>arr.fill(np.NaN)<EOL><DEDENT>elif arr.shape[<NUM_LIT:1>] != len(v):<EOL><INDENT>raise ParseError(b'<STR_LIT>' + line)<EOL><DEDENT>arr[vocab[token], :] = np.array(v, dtype=dtype).reshape(<NUM_LIT:1>, -<NUM_LIT:1>)<EOL><DEDENT><DEDENT>return arr<EOL>
Load word embedding file with predefined vocabulary

Args:
    fin (File): File object to read. File should be open for reading ascii.
    vocab (dict): Mapping from words (``bytes``) to vector indices
        (``int``).
    dtype (numpy.dtype): Element data type to use for the array.

Returns:
    numpy.ndarray: Word embedding representation vectors