【问题标题】:How do I decode the output of my seq-to-seq model if I'm using an embedding layer?如果我使用嵌入层,如何解码我的 seq-to-seq 模型的输出?
【发布时间】:2021-02-20 21:10:47
【问题描述】:

我有一个由一些聪明的机器人数据训练的 seq to seq 模型:

justphrases_X 是句子列表,justphrases_Y 是对这些句子的响应列表。

maxlen = 62   
#low is a list of all the unique words.




def Convert_To_Encoding(just_phrases): 
    encodings = []
    for sentence in just_phrases:
        onehotencoded = one_hot(sentence, len(low))
        encodings.append(np.array(onehotencoded))
        
    encodings_padded = pad_sequences(encodings, maxlen=maxlen, padding='post', value = 0.0)

    return encodings_padded

encodings_X_padded = Convert_To_Encoding(just_phrases_X)
encodings_y_padded = Convert_To_Encoding(just_phrases_y)

model = Sequential()       
embedding_layer = Embedding(len(low), output_dim=8, input_length=maxlen)
model.add(embedding_layer)
model.add(GRU(128)) # input_shape=(None, 496)
model.add(RepeatVector(numberofwordsoutput)) #number of characters?
model.add(GRU(128, return_sequences = True)) 
model.add(Flatten())
model.add(Dense(62, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer= 'adam', metrics=['accuracy'])
model.summary()

model.fit(encodings_X_padded, encodings_y_padded, batch_size = 1, epochs=1) #, validation_data = (testX, testy)
model.save("cleverbottheseq-uel.h5")

当我使用这个模型进行预测时,由于我使用了 softmax,输出将介于 0 和 1 之间。但是,由于我有大约 3000 个独特的单词,每个单词都分配有一个单独的整数,我如何从本质上重复模型在训练期间所做的并将输出转换回一个分配有单词的整数?

【问题讨论】:

    标签: python-3.x tensorflow keras word-embedding sequence-to-sequence


    【解决方案1】:

    我认为不可能使用 Sequential API 创建 seq2seq。尝试使用功能 API 分别创建编码器和解码器。您需要两个输入 - 第一个用于编码器,第二个用于解码器。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2022-09-26
      • 2015-02-02
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多