Created
April 23, 2020 21:31
-
-
Save t27/40b0bb7c24cffd491d1816e38d215a00 to your computer and use it in GitHub Desktop.
A simple way to convert a list of indices to a one-hot encoded vector using numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
q_word_idx = [1,2,4,7,6,2] | |
a_idx = [1,1,5,6,4,2] | |
q_word_idx = np.array(q_word_idx) | |
a_idx = np.array(a_idx) | |
q_vector_size = 8 | |
a_vector_size = 8 | |
q_emb=np.zeros((len(q_word_idx),q_vector_size)) | |
a_emb = np.zeros((len(a_idx), a_vector_size)) | |
q_emb[np.arange(len(q_word_idx)), q_word_idx] = 1 | |
a_emb[np.arange(len(a_idx)),a_idx] = 1 | |
# >>> q_emb | |
# array([[0., 1., 0., 0., 0., 0., 0., 0.], | |
# [0., 0., 1., 0., 0., 0., 0., 0.], | |
# [0., 0., 0., 0., 1., 0., 0., 0.], | |
# [0., 0., 0., 0., 0., 0., 0., 1.], | |
# [0., 0., 0., 0., 0., 0., 1., 0.], | |
# [0., 0., 1., 0., 0., 0., 0., 0.]]) | |
# >>> a_emb | |
# array([[0., 1., 0., 0., 0., 0., 0., 0.], | |
# [0., 1., 0., 0., 0., 0., 0., 0.], | |
# [0., 0., 0., 0., 0., 1., 0., 0.], | |
# [0., 0., 0., 0., 0., 0., 1., 0.], | |
# [0., 0., 0., 0., 1., 0., 0., 0.], | |
# [0., 0., 1., 0., 0., 0., 0., 0.]]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment