This commit is contained in:
Blake Elias 2019-08-15 18:57:54 +00:00 коммит произвёл Caleb Robinson
Родитель d14c1b793b
Коммит 4f7dce4c1c
1 изменённых файлов: 9 добавлений и 1 удалений

Просмотреть файл

@ -2,10 +2,18 @@ import numpy as np
def one_hot_from_indices(index_ids):
'''
Turn an n-dimensional tensor of integers into an (n+1)-dimensional array of one-hot vectors, containing a 1 in the given index and 0 everywhere else.
'''
return np.eye(index_ids.max() + 1)[index_ids]
def make_one_hot(array, axis=-1):
def soft_to_hard(array, axis=-1):
'''
Turn an array of floats (perhaps probabilities) into a one-hot vector, with each vector along `axis` having its max value turned into a 1, and the rest turned to 0s.
Only tested for axis=-1, as this is usually what we want to do.
'''
return one_hot_from_indices(array.argmax(axis=axis))