一个热编码 returns 最后一个分类值的全 0 向量
one hot encoding returns all 0 vector for last categorical value
tf.one_hot()
正在为第三个 class 可能的分类值生成 [0,0,0]
向量。
我希望 [1,0,0]
。这个功能我做错了什么?
有 3 种可能的分类 class 我想进行 One-Hot 编码。 1,2,3 使用 tf.one_hot()
.
示例:
# 3 possible classes
print(df['sent_score'].unique())
# array([1., 2., 3.])
#original
labels1 = np.asarray(df['sent_score'])
print("Original Labels \n", labels1[25:30])
# Original Labels
# [2. 1. 2. 1. 3.]
# one hot encoded
labels = tf.one_hot(labels1, 3)
print("\nOne Hot labels \n", labels[25:30])
# One Hot labels
# [[0. 0. 1.]
# [0. 1. 0.]
# [0. 0. 1.]
# [0. 1. 0.]
# [0. 0. 0.]] ##WHY IS THIS VECTOR is [0,0,0] and not [1,0,0]
问题是因为 tf.one_hot
也将 0
视为 class,因此假设您的标签是 1-3,当传递给 tf.one_hot
时它只是填充3
class 和 0
s.
简单示例:
indices = [0, 1, 2]
tf.one_hot(indices, 3)
# <tf.Tensor: shape=(3, 3), dtype=float32, numpy=
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]], dtype=float32)>
indices = [0, 1, 2, 3]
tf.one_hot(indices, 3)
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.],
# [0., 0., 0.]], dtype=float32)>
所以你应该把classes改成0-2的范围,然后传给tf.one_hot
tf.one_hot()
正在为第三个 class 可能的分类值生成 [0,0,0]
向量。
我希望 [1,0,0]
。这个功能我做错了什么?
有 3 种可能的分类 class 我想进行 One-Hot 编码。 1,2,3 使用 tf.one_hot()
.
示例:
# 3 possible classes
print(df['sent_score'].unique())
# array([1., 2., 3.])
#original
labels1 = np.asarray(df['sent_score'])
print("Original Labels \n", labels1[25:30])
# Original Labels
# [2. 1. 2. 1. 3.]
# one hot encoded
labels = tf.one_hot(labels1, 3)
print("\nOne Hot labels \n", labels[25:30])
# One Hot labels
# [[0. 0. 1.]
# [0. 1. 0.]
# [0. 0. 1.]
# [0. 1. 0.]
# [0. 0. 0.]] ##WHY IS THIS VECTOR is [0,0,0] and not [1,0,0]
问题是因为 tf.one_hot
也将 0
视为 class,因此假设您的标签是 1-3,当传递给 tf.one_hot
时它只是填充3
class 和 0
s.
简单示例:
indices = [0, 1, 2]
tf.one_hot(indices, 3)
# <tf.Tensor: shape=(3, 3), dtype=float32, numpy=
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]], dtype=float32)>
indices = [0, 1, 2, 3]
tf.one_hot(indices, 3)
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.],
# [0., 0., 0.]], dtype=float32)>
所以你应该把classes改成0-2的范围,然后传给tf.one_hot