tf.one_hot输入参数为:
one_hot(
indices,
depth,
on_value=None,
off_value=None,
axis=None,
dtype=None,
name=None
)
1.如果indices为一个数值,则tf.one_hot返回值为一个长度为depth的向量.
2.如果indices为一个长度为features的向量,那么tf.one_hot的返回值一个矩阵,矩阵大小为:
axis=-1,shapa=[features,depth]
axis=0,shapa=[depth,features]
3.如果indices的唯一个矩阵,shape为[batch, features], 则返回值一个三维矩阵,矩阵大小为:
axis=-1,shape = bathc, features, depth]
axis=1,shape = [bathc, depth, features]
axis=0,shape = [depth, bathc, features]
首先根据上述条件,初始化一个值为off_value的向量或者矩阵, 并在其对应索引indices处的职位on_value,其他处的值为off_value,如果on_value,off_value为None,则对应的值为0.
例如对于下面代码:
logits = tf.convert_to_tensor([[0.2, 0.3, 0.4, 0.5], [0.5, 0.4, 0.3, 0.2]])
labels = slim.one_hot_encoding([1, 2], 4)
bbox = tf.ones_like(logits)
with tf.Session() as sess:
print sess.run(labels)
sess.close()
得到输出矩阵为: