tf.one_hot()函数简介
2018-02-04 01:07
1321 查看
tf.one_hot()函数是将input转化为one-hot类型数据输出,相当于将多个数值联合放在一起作为多个相同类型的向量,可用于表示各自的概率分布,通常用于分类任务中作为最后的FC层的输出,有时翻译成“独热”编码。
tensorflow的help中相关说明如下:
indices表示输入的多个数值,通常是矩阵形式;depth表示输出的尺寸。
由于one-hot类型数据长度为depth位,其中只用一位数字表示原输入数据,这里的on_value就是这个数字,默认值为1,one-hot数据的其他位用off_value表示,默认值为0。
tf.one_hot()函数规定输入的元素indices从0开始,最大的元素值不能超过(depth - 1),因此能够表示(depth + 1)个单位的输入。若输入的元素值超出范围,输出的编码均为 [0, 0 … 0, 0]。
indices = 0 对应的输出是[1, 0 … 0, 0], indices = 1 对应的输出是[0, 1 … 0, 0], 依次类推,最大可能值的输出是[0, 0 … 0, 1]。
代码示例如下:
tensorflow的help中相关说明如下:
one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None) Returns a one-hot tensor.
indices表示输入的多个数值,通常是矩阵形式;depth表示输出的尺寸。
由于one-hot类型数据长度为depth位,其中只用一位数字表示原输入数据,这里的on_value就是这个数字,默认值为1,one-hot数据的其他位用off_value表示,默认值为0。
tf.one_hot()函数规定输入的元素indices从0开始,最大的元素值不能超过(depth - 1),因此能够表示(depth + 1)个单位的输入。若输入的元素值超出范围,输出的编码均为 [0, 0 … 0, 0]。
indices = 0 对应的输出是[1, 0 … 0, 0], indices = 1 对应的输出是[0, 1 … 0, 0], 依次类推,最大可能值的输出是[0, 0 … 0, 1]。
代码示例如下:
import tensorflow as tf classes = 3 labels = tf.constant([0,1,2]) # 输入的元素值最小为0,最大为2 output = tf.one_hot(labels,classes) sess = tf.Session() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) output = sess.run(output) print("output of one-hot is : ",output) # ('output of one-hot is : ', array([[ 1., 0., 0.], # [ 0., 1., 0.], # [ 0., 0., 1.]], dtype=float32))
相关文章推荐
- tensorflow--tf.one_hot()函数示例
- tf.one_hot()进行独热编码
- tf.one_hot 实例
- tensorflow API: tf.one_hot
- tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None)
- 【tensorflow】tf.one_hot,tf.reduce_mean,tf.argmax,tf.equal,tf.cast学习
- One-hot 编码/TF-IDF 值来提取特征,LAD/梯度下降法(Gradient Descent),Sigmoid
- Tensorflow 的tf.one_hot()功能:dense to one hot
- tensorflow——tf.one_hot以及tf.sparse_to_dense函数
- tensorflow学习笔记--tf.one_hot
- tf.one_hot()使用
- 朴素贝叶斯理论、One-Hot表达与TF-IDF算法
- 【ES6学习】— (5)异步编程async函数简介
- 机器学习之离散型特征的处理-one hot encoder
- LoadRunner函数简介--事务型函数
- tensorflow下的tf.cast()函数
- Winsock 函数简介
- one-hot编码(独热编码)
- TensorFlow 多分类标签转换成One-hot
- SQL中 decode()函数简介