gpt4 book ai didi

python - 仅使用 keras 对一组 float 进行一次性编码

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:19 26 4
gpt4 key购买 nike

首先,我是 stackoverflow 的新手,所以如果有什么方法可以改进我提出问题的方式,或者如果我遗漏了一些明显的东西,请向我指出!

我正在 Keras 中构建分类卷积网络,要求网络预测用于生成图像的参数。这些类以 5 个浮点值编码,例如类列表可能如下所示:

[[0.], [0.76666665], [0.5], [0.23333333], [1.]]

我想使用 keras.utils.to_categorical(y, num_classes=5, dtype='float32') 对这些类进行一次性编码功能。

但是,它返回以下内容:

array(
[
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.]
],
dtype=float32)

它只接受整数作为输入,因此它映射所有值 < 1.0 .我可以通过将所有值乘以一个常数来规避这个问题,这样它们都是整数,我认为在 scikit learn 中也有一种方法可以解决这个问题,但这听起来像是一个巨大的变通方法,对于一个应该很容易解决的问题在 keras 中,这让我相信我遗漏了一些明显的东西。

我希望有人能够指出一个仅使用 Keras 的简单替代方案。

最佳答案

另一种选择是使用 sklearn 中的 OneHotEncoder:

from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(categories='auto')

input = [[0.], [0.76666665], [0.5], [0.23333333], [1.]]
output = encoder.fit_transform(input)

print(input)
print(output.toarray())

输出:

[[0.0], [0.76666665], [0.5], [0.23333333], [1.0]]
[[ 1. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 1.]]

关于python - 仅使用 keras 对一组 float 进行一次性编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com