简体   繁体   English

如何正确处理tensorflow RNN中的one-hot-encoding和多维数据

[英]How to correctly deal with one-hot-encoding and multi-dimensional data in tensorflow RNN

I'm creating a binary classifier that classifiers letter sequences eg 'BA'.我正在创建一个二元分类器,用于对字母序列进行分类,例如“BA”。

Each sequence is made up of 2 letters encoded as one-hot vectors.每个序列由编码为 one-hot 向量的 2 个字母组成。 For example, the sequence 'BA' is [[0, 1, 0, 0], [1, 0, 0, 0]] .例如,序列 'BA' 是[[0, 1, 0, 0], [1, 0, 0, 0]]

(The sequences are longer in my original code but I want to keep my question simple) (我的原始代码中的序列更长,但我想让我的问题保持简单)

I'm reasonably new to machine learning, I've done some stuff before but only with flat inputs.我对机器学习相当陌生,我以前做过一些东西,但只使用平面输入。 I'm struggling to understand how I can pass a multi-dimensional input into my network.我正在努力理解如何将多维输入传递到我的网络中。 Do I need to flatten it somehow?我需要以某种方式压平它吗?

I've created a minimally reproducible example here - https://www.dropbox.com/s/dhg3huw6bh7dfjd/Example.py?dl=0 , Sequences only contains 3 examples of training data to keep it simple.我在这里创建了一个最低限度可重复的示例 - https://www.dropbox.com/s/dhg3huw6bh7dfjd/Example.py?dl=0Sequences仅包含 3 个训练数据示例以保持简单。 I just need help working out how to pass 3d inputs into my model.我只需要帮助弄清楚如何将 3d 输入传递到我的模型中。

In the code I've provided, the entire dataset is just the following:在我提供的代码中,整个数据集如下:

Sequences = [  [[0, 1, 0, 0], [1, 0, 0, 0]]   ,  [[0, 1, 0, 0], [1, 0, 0, 0]]  ,   [[0, 0, 0, 1], [0, 1, 0, 0]]  ]

It is a binary classifier, Targets for these sequences are:它是一个二元分类器,这些序列的目标是:

Targets = [1.0, 1.0, 0.0]

If you run the code I've provided you should get this error:如果您运行我提供的代码,您应该会收到此错误:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 2, 2, 4]

If someone could help figure out how to correctly pass the sequences into my network that would be great.如果有人能帮助弄清楚如何正确地将序列传递到我的网络中,那就太好了。 Thank you :)谢谢 :)

This line is the culprit.这条线是罪魁祸首。 LSTM expects 3 dimensional input. LSTM 需要 3 维输入。 In your case you are trying to pass a 4 dimensional input (remember that Keras add an additional batch dimension to the input shape).在您的情况下,您正在尝试传递 4 维输入(请记住,Keras 向输入形状添加了一个额外的批次维度)。 So, getting rid of the first 2 will solve your issue.因此,摆脱前2将解决您的问题。

model.add(LSTM(128,
                   input_shape=(2, 2, 4), return_sequences=True))

Simply change this to,只需将其更改为,

model.add(LSTM(128,
                   input_shape=(2, 4), return_sequences=True))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM