【问题标题】:tensorflow 0.8 one hot encodingtensorflow 0.8 一热编码
【发布时间】:2016-12-26 12:07:11
【问题描述】:

我要编码的数据如下:

print (train['labels'])

[0 0 0 ..., 42 42 42]

从 0 到 42 共有 43 个类

现在我读到 0.8 版中的 tensorflow 有一个热编码的新功能,所以我尝试如下使用它:

trainhot=tf.one_hot(train['labels'], 43, on_value=1, off_value=0)

唯一的问题是我认为输出不是我需要的

print (trainhot[1])

Tensor("strided_slice:0", shape=(43,), dtype=int32)

有人能把我推到正确的方向吗:)

【问题讨论】:

    标签: tensorflow neural-network one-hot-encoding


    【解决方案1】:

    输出正确且符合预期。 trainhot[1] 是第二个(从 0 开始的索引)训练样本的标签,它是一维形状 (43,)。您可以使用下面的代码来更好地理解 tf.one_hot:

      onehot = tf.one_hot([0, 0, 41, 42], 43, on_value=1, off_value=0)                  
      with tf.Session() as sess:                                                        
        onehot_v = sess.run(onehot)                                                  
      print("v: ", onehot_v)                                                            
      print("v shape: ", onehot_v.shape)                                                
      print("v[1] shape: ", onehot[1])
    
    output:
    v:  [[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0]
     [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 1 0]
     [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 1]]
    v shape:  (4, 43)
    v[1] shape:  Tensor("strided_slice:0", shape=(43,), dtype=int32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-19
      • 2021-12-01
      • 2016-02-14
      • 2019-05-02
      • 2018-10-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多