Tensorflow2.0 label与one-hot独热编码向量之间的相互转换

label 转 one-hot


import tensorflow as tf

label = tf.stack(5)
one_hot_label = tf.one_hot(label, 10)

print("label: ", label.numpy())
print("one_hot_label: ", one_hot_label.numpy())

'''
output:
label: 5
one_hot_label: [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]
'''


one-hot 转 label

label = tf.stack(5)
one_hot_label = tf.one_hot(label, 10)

new_label = tf.argmax(one_hot_label)
print(new_label.numpy())

# output: 5

 

你可能感兴趣的:(Python,AI/ML/DL)