Skip to content

Instantly share code, notes, and snippets.

@SuvroBaner
Created December 31, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuvroBaner/77d4fc3638ebd0e71df5f94d6464bb11 to your computer and use it in GitHub Desktop.
Save SuvroBaner/77d4fc3638ebd0e71df5f94d6464bb11 to your computer and use it in GitHub Desktop.
def one_hot_matrix(labels, Con):
"""
Creates a matrix where the i-th row corresponds to the ith class number and the jth column
corresponds to the jth training example. So if example j had a label i. Then entry (i,j)
will be 1.
"""
C = tf.constant(Con, name = 'C')
one_hot_matrix = tf.one_hot(indices = labels, depth = C, axis = 0)
with tf.Session() as sess:
one_hot = sess.run(one_hot_matrix)
sess.close()
return one_hot
labels = np.array([1, 2, 0, 1])
one_hot = one_hot_matrix(labels, Con = 3)
print(one_hot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment