Mat*_*amp 6 python tensorflow one-hot-encoding
一直在寻找,但似乎无法找到任何关于如何从TensorFlow中的单热值解码或转换回单个整数的示例.
我用过tf.one_hot并且能够训练我的模型,但是在分类之后我对如何理解标签感到有点困惑.我的数据是通过TFRecords我创建的文件输入的.我想过在文件中存储文本标签但是无法让它工作.看起来似乎TFRecords无法存储文本字符串或者我错了.
mar*_*ars 15
您可以使用找出矩阵中最大元素的索引tf.argmax.由于你的一个热矢量将是一维的,并且只有一个1和另一个0s,这将有效,假设你正在处理单个向量.
index = tf.argmax(one_hot_vector, axis=0)
Run Code Online (Sandbox Code Playgroud)
对于更标准的矩阵batch_size * num_classes,用于axis=1获得大小的结果batch_size * 1.
由于单热编码通常只是一个包含batch_size行和num_classes列的矩阵,并且每行都是零,并且对应于所选类的单个非零,您可以使用tf.argmax()恢复整数标签的向量:
BATCH_SIZE = 3
NUM_CLASSES = 4
one_hot_encoded = tf.constant([[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])
# Compute the argmax across the columns.
decoded = tf.argmax(one_hot_encoded, axis=1)
# ...
print sess.run(decoded) # ==> array([1, 0, 3])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13263 次 |
| 最近记录: |