有时我们的样本标签,都是标记从0开始直至到类别的个数。在模型训练的时候,这些标签需要变成one_hot向量,这样才能够跟softmax出来的概率做互熵损失,计算loss。

  那么,映射的方法如下:

1 def to_one_hot(y, n_class):
2     return np.eye(n_class)[y]

  

  y: 类型是list,样本的类别标签序列

  n_class: 类别的个数

    x = to_one_hot([0,1,2], 3)
    print(x)

  输出:

深度学习中将类别标签映射到one_hot向量

 

相关文章:

  • 2021-06-16
  • 2021-11-16
  • 2022-01-06
  • 2021-12-21
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
猜你喜欢
  • 2021-07-16
  • 2021-12-22
  • 2021-05-17
  • 2021-06-16
  • 2021-06-13
  • 2021-04-16
  • 2021-10-06
相关资源
相似解决方案