Keras中的Lambda层带有keras.backend.one_hot给出TypeError [英] Lambda layer in Keras with keras.backend.one_hot gives TypeError

查看:184
本文介绍了Keras中的Lambda层带有keras.backend.one_hot给出TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras训练角色等级的CNN.我将一个词作为输入.我已经将单词转换为索引列表,但是当我尝试将其输入one_hot时,得到的是TypeError.

I'm trying to train a character level CNN using Keras. I take as input a single word. I have already transformed the words into lists of indices, but when I try to feed it into one_hot, I get a TypeError.

>>> X_train[0]
array([31, 14, 23, 29, 27, 18, 12, 30, 21, 10, 27,  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,  0,  0,  0,  0], dtype=uint8)
>>> X_train.shape
(2226641, 98)

但是当我尝试像这样创建我的模型时:

But when I try to create my model like this:

k_model = Sequential()
k_model.add(Lambda(K.one_hot, arguments={'num_classes': 100}, input_shape=(98,), output_shape=(98,100)))
k_model.add(Conv1D(filters=16, kernel_size=5, strides=1, padding='valid'))

我得到TypeError: Value passed to parameter 'indices' has DataType float32 not in list of allowed values: uint8, int32, int64.

很明显,它没有达到读取X_train的地步,所以它在哪里获得浮点值?

It's obviously not making it to a point where X_train is even read, so where is it getting a float value?

我希望实例的形状为(98, 100),其中类的数量为100.

I would like to have an instance shape of (98, 100), where 100 is the number of classes.

我无法在内存中容纳整个数据集.

I can't fit the entire dataset in memory.

推荐答案

我建议一种更清洁的解决方案,该解决方案可以实现相同的结果,怎么做:

I would suggest a cleaner solution that would achieve the same result, how about:

k_model.add(Embedding(num_classes, num_classes,
                      embeddings_initializer='identity',
                      trainable=False,
                      name='onehot'))

您实质上是在嵌入东西,因此使用固定权重的东西会更有意义.它还使您可以灵活地将来进行嵌入训练.

You are essentially embedding things, it would make more sense to use one with fixed weights. It also gives you the flexibility to make the embedding trainable in the future.

这篇关于Keras中的Lambda层带有keras.backend.one_hot给出TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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