Tensorflow的三条和one_hot有关的写法:
tf.keras.utils.to_categorical
a = tf.keras.utils.to_categorical([0, 1, 2, 3], num_classes=4)
a = tf.constant(a, shape=[4, 4])
print(a)
tf.keras.layers.IntegerLookup
vocab = [12, 36, 1138, 42]
data = tf.constant([12, 36, 1138, 42, 7]) # Note OOV tokens
layer = tf.keras.layers.IntegerLookup(vocabulary=vocab, output_mode='one_hot')
layer(data)
tf.one_hot
indices = [0, 1, 2]
depth = 3
tf.one_hot(indices, depth) # output: [3 x 3]
# [[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]]