tensorflow 将one_hot标签和数字(整数)标签之间进行相互转化
这里写自定义目录标题
- tensorflow自带one_hot标签函数
- 将one-hot转化为数字标签
tensorflow自带one_hot标签函数
tensorflow 有封装好的函数可以直接将整数标签转化为one_hot标签
import tensorflow as tf label = [1,0,2,3,0]y = tf.one_hot(label, depth=4).numpy()
#使用ont_hot返回的是tensor张量,再用numpy取出数组print(y)
得到: [[0. 1. 0. 0.]
[1. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]
[1. 0. 0. 0.]]。
将one-hot转化为数字标签
将one-hot转化为数字标签没有封装好的函数,可以遍历并使用soft argmax取出整数:
label = [np.argmax(i) for i in y] print(label)
得到:[1, 0, 2, 3, 0]
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
