one_hot( indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None ) indices中的代表索引 depth代表每个维度的长度,比如axis=0,depth=4则代表行的长度为4,有四个元素 那indices每个元素则依次代表第一行第二行第三行...每行索引处的值为1。 如下代码所示:
```
y_hat = np.array([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
y = np.array([0, 2], dtype='int32')
print("---------")
print(tf.one_hot(y, depth=3))
print(tf.boolean_mask(y_hat, tf.one_hot(y, depth=3)))
print("---------")
tf.boolean_mask(y_hat, tf.one_hot(y, depth=3))
```
tf.boolean_mask(a, b) 则是把a中位置与b中位置相同且b中位置的值为ture的保留下来,且如果同一行有好几个为true,则结果会自动有几个值就是几行。例如
a = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
b = np.array([[0,1,0],
[0,3,1],
[0,0,1]])
print("--------")
print(tf.boolean_mask(a, b))
print("--------")