예제 #1
0
def predict(sentence):
    checkpoint_dir = 'models'
    seq2seqModel.checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
    sentence = preprocess_sentence(sentence)
    inputs = [
        input_word_index.get(i, input_word_index['unk'])
        for i in jieba.lcut(sentence)
    ]
    inputs = tf.keras.preprocessing.sequence.pad_sequences(
        [inputs], maxlen=max_length_inp, padding='post')
    inputs = tf.convert_to_tensor(inputs)

    result = ''

    hidden = [tf.zeros((1, units))]
    enc_out, enc_hidden = seq2seqModel.encoder(inputs, hidden)

    dec_hidden = enc_hidden
    dec_input = tf.expand_dims([target_word_index['start']], 0)

    for t in range(max_length_tar):
        predictions, dec_hidden, attention_weights = seq2seqModel.decoder(
            dec_input, dec_hidden, enc_out)

        predicted_id = tf.argmax(predictions[0]).numpy()

        if target_index_word[predicted_id] == 'end':
            break
        result += target_index_word[predicted_id]

        dec_input = tf.expand_dims([predicted_id], 0)

    return result
예제 #2
0
def predict(sentence):
    sentence = preprocess_sentence(sentence)
    inputs = [input_lang.word_index.get(i, 3) for i in sentence.split(' ')]
    inputs = tf.keras.preprocessing.sequence.pad_sequences(
        [inputs], maxlen=max_length_input, padding='post')
    inputs = tf.convert_to_tensor(inputs)

    result = ''
    hidden = [tf.zeros((1, units))]
    enc_out, enc_hidden = seq2seqModel.encoder(inputs, hidden)
    dec_hidden = enc_hidden
    dec_input = tf.expand_dims([target_lang.word_index['start']], 0)

    for t in range(max_length_tar):
        predictions, dec_hidden, attention_weights = seq2seqModel.decoder(
            dec_input, dec_hidden, enc_out)

        predicted_id = tf.argmax(predictions[0]).numpy()

        if target_lang.index_word[predicted_id] == 'end':
            break
        result += target_lang.index_word[predicted_id] + ' '

        dec_input = tf.expand_dims([predicted_id], 0)

    return result
예제 #3
0
def predict(sentence):
    checkpoint_dir = gConfig['model_data']

    # seq2seqModel.checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
    manager = tf.train.CheckpointManager(seq2seqModel.checkpoint,
                                         directory=checkpoint_dir,
                                         max_to_keep=5)
    seq2seqModel.checkpoint.restore(manager.latest_checkpoint)

    sentence = preprocess_sentence(sentence)
    inputs = [input_token.word_index.get(i, 3) for i in sentence.split(' ')]
    inputs = tf.keras.preprocessing.sequence.pad_sequences(
        [inputs], maxlen=max_length_inp, padding='post')
    inputs = tf.convert_to_tensor(inputs)

    result = ''

    hidden = [tf.zeros((1, units))]
    enc_out, enc_hidden = seq2seqModel.encoder(inputs, hidden)

    dec_hidden = enc_hidden
    dec_input = tf.expand_dims([target_token.word_index['start']], 0)

    for t in range(max_length_tar):
        predictions, dec_hidden, attention_weights = seq2seqModel.decoder(
            dec_input, dec_hidden, enc_out)

        predicted_id = tf.argmax(predictions[0]).numpy()

        if target_token.index_word[predicted_id] == 'end':
            break

        val = target_token.index_word[predicted_id]
        if isinstance(val, int):
            val = str(val)

        result += val

        dec_input = tf.expand_dims([predicted_id], 0)

    return result