Keras seq2seq - 单词嵌入

时间:2018-03-25 14:45:39

标签: python neural-network keras chatbot embedding

我正在基于Keras的seq2seq开发一个生成式聊天机器人。我使用了此网站的代码:https://machinelearningmastery.com/develop-encoder-decoder-model-sequence-sequence-prediction-keras/

我的模特看起来像这样:

# define training encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = LSTM(n_units, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]

# define training decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = LSTM(n_units, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(n_output, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)

# define inference decoder
decoder_state_input_h = Input(shape=(n_units,))
decoder_state_input_c = Input(shape=(n_units,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs [decoder_outputs] + decoder_states)

这个神经网络设计用于处理一个热编码向量,输入到这个网络似乎是这样的:

[[[0. 0. 0. 0. 1. 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. 1. 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. 1. 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. 1. 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. 1. 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. 1. 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.]]]

如何重建这些模型以使用单词?我想使用word嵌入层,但我不知道如何将嵌入层连接到这些模型。

我的输入应该是[[1,5,6,7,4], [4,5,7,5,4], [7,5,4,2,1]],其中int数字是单词的表示。

我尝试了一切,但我仍然遇到错误。你能帮我吗?

2 个答案:

答案 0 :(得分:4)

我终于做到了。这是代码:

import tornado.web
import tornado.gen
import time
from tornado.ioloop import IOLoop

## Run this function in different process
def blocking_get(var1):
    print("blocking function")
    time.sleep(2)
    return {"res":"some result"}

class rootHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self, *args, **kwargs):
        print("Get called for {}".format(self))
        var1 = self.get_argument('var1', None)
        ## ************************************
        ## get values from blocking call blocking_get()
        resp_dict = getRespFromDifferentProcess()
        self.write(resp_dict)
        self.finish()

    def _call_later_something(self):
        print("_call_later_something")


class TestApp(tornado.web.Application):
    def __init__(self, test=False):
        handlers = [
            (r"/", rootHandler),
        ]
        tornado_settings = dict(
            debug=True,
            serve_traceback=True,
        )

        tornado.web.Application.__init__(self, handlers, **tornado_settings)


if __name__ == "__main__":
    PORT = 8888
    print("Tornado on port {}".format(PORT))
    http_server = TestApp()
    http_server.listen(PORT)
    IOLoop.instance().start()

"模型"是培训模式 encoder_model和decoder_model是推理模型

答案 1 :(得分:1)

下面在本例的FAQ部分中,他们提供了一个如何使用seq2seq嵌入的示例。我现在正在弄清楚推理步骤。当我明白时,我会在这里发布。 https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html