如何解决设置Keras模型可获取嵌入文本,LSTM,自动编码器的正确输出张量形状
我正在尝试将LSTM自动编码器放在一起,其中的层(返回的形状)是:
嵌入-> LSTM-> RepeatVector-> LSTM-> TimeDistributed(Dense)。
[6,18,8]-> [6,64]-> [6,18,64]-> [6,18,64]-> [6,18,1]
最后一层是我遇到问题并且似乎无法在线找到答案的地方。我正在寻找可以返回与嵌入层相同尺寸的东西(因为我正在尝试制作一个自动编码器),在这里我可以撤消嵌入以获取单词的预测序列。也许我在这里误会了什么??
下面是我尝试过的一个例子。
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding,LSTM,Dense,RepeatVector,TimeDistributed
dummy_docs = [
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.","When an unknown printer took a galley of type and scrambled it to make a type specimen book.","It has survived not only five centuries,but also the leap into electronic typesetting,remaining essentially unchanged.","It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.","More recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
]
tokenizer = Tokenizer(
num_words=None,filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',lower=True,split=" ",oov_token="<OOV>"
)
tokenizer.fit_on_texts(dummy_docs)
sequences = pad_sequences(
tokenizer.texts_to_sequences(dummy_docs),padding='post'
)
model = Sequential(
[
Embedding(
input_dim=len(tokenizer.word_index) + 1,output_dim=8,input_shape=(sequences.shape[1],)
),LSTM(8,activation='tanh'),# return_sequences..?
RepeatVector(sequences.shape[1]),activation='tanh',return_sequences=True),# return_sequences..?
TimeDistributed(Dense(1))
]
)
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
model.fit(sequences,sequences,epochs=30,batch_size=1)
y = model.predict(sequences)
当我在sequences
上运行拟合模型以获得预测时,我期望的是6x18矩阵,但是我得到了一个长度为18的矢量。我认为Dense(1)
必须是其他原因,但除1
之外的任何其他因素都会导致张量形状产生错误,然后整个模型失败。我应该在这里尝试一种单编码输入吗?任何帮助将不胜感激。
解决方法
解决方案是从模型中删除TimeDistirbuted
层,并在最后一个Dense()
层中使用嵌入尺寸。
此外,我将Embedding
层的详细信息移到Sequential
之外,以使用相同的嵌入详细信息来获取y
的{{1}}。
现在剩下的就是将预测的嵌入工作恢复到原始单词序列,但这超出了原始问题的范围。
注意,这不是用于句子,而是用于(希望)清除文本数据(名称),其中不同的用户以不同的方式输入了相同的网站名称,因此regex不是一个选择,因为没有固定的名称遵循的模式。最终的解决方案仍需要做一些工作。
下面是代码的更新部分。
model.fit(X,y,...)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。