可以在Keras模型中执行单热编码吗? [英] Can one-hot encoding be performed within a Keras model?

查看:57
本文介绍了可以在Keras模型中执行单热编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理一个相对较大的数据集的项目,并且在整数编码的一次热编码方面遇到了问题.输出矩阵太大,无法放在我的ram上.

I've been working on a project that works with a relatively large dataset and I've been facing problems when it comes to one-hot encoding the integer encodings. The output matrix is too big to fit on my ram.

我想知道是否可以在Keras模型中执行一次热编码,因为整数编码确实适合作为我的内存中的数组.

I was wondering if it was possible to perform the one-hot encoding within the Keras model because the integer encoding does fit nicely as an array on my memory.

推荐答案

您可以在模型内部进行一键编码:

you can make one-hot encoding inside your model:

X = np.asarray([1,2,3,4,4,4,4,5])

inp = Input((1,), dtype='int32')
x = Lambda(lambda x: tf.one_hot(x[:,0], len(set(X))))(inp)
out = Dense(20)(x)

model = Model(inp,out)
model.compile('adam','mse')
print(model.summary())

model.fit(X, np.random.uniform(0,1, (len(X),20)), epochs=3)

这篇关于可以在Keras模型中执行单热编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆