国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署(三)

這篇具有很好參考價值的文章主要介紹了【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署(三)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

系列文章

【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯seq2seq字符編碼(一)
【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型訓(xùn)練與保存(二)
【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署(三)
【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署之onnx(python)(四)


模型部署也是很重要的一部分,這里先講基于python的部署,后面我們還要將模型部署到移動端。
細心的小伙伴會發(fā)現(xiàn)前面的文章在模型保存之后進行模型推理時,我們使用的數(shù)據(jù)是在訓(xùn)練之前我們對數(shù)據(jù)進行處理的encoder_input_data中讀取,而不是我們手動輸入的,那么這一章主要來解決自定義輸入推理的問題

1、加載字符文件

首先,我們根據(jù) 【如何訓(xùn)練一個中譯英翻譯器】LSTM機器翻譯模型訓(xùn)練與保存(二)的操作,到最后
會得到這樣的三個文件:input_words.txt,target_words.txt,config.json
需要逐一進行加載
進行加載

# 加載字符
# 從 input_words.txt 文件中讀取字符串
with open('input_words.txt', 'r') as f:
    input_words = f.readlines()
    input_characters = [line.rstrip('\n') for line in input_words]

# 從 target_words.txt 文件中讀取字符串
with open('target_words.txt', 'r', newline='') as f:
    target_words = [line.strip() for line in f.readlines()]
    target_characters = [char.replace('\\t', '\t').replace('\\n', '\n') for char in target_words]

#字符處理,以方便進行編碼
input_token_index = dict([(char, i) for i, char in enumerate(input_characters)])
target_token_index = dict([(char, i) for i, char in enumerate(target_characters)])

# something readable.
reverse_input_char_index = dict(
    (i, char) for char, i in input_token_index.items())
reverse_target_char_index = dict(
    (i, char) for char, i in target_token_index.items())
num_encoder_tokens = len(input_characters) # 英文字符數(shù)量
num_decoder_tokens = len(target_characters) # 中文文字數(shù)量

讀取配置文件

import json
with open('config.json', 'r') as file:
    loaded_data = json.load(file)

# 從加載的數(shù)據(jù)中獲取max_encoder_seq_length和max_decoder_seq_length的值
max_encoder_seq_length = loaded_data["max_encoder_seq_length"]
max_decoder_seq_length = loaded_data["max_decoder_seq_length"]

2、加載權(quán)重文件

# 加載權(quán)重
from keras.models import load_model
encoder_model = load_model('encoder_model.h5')
decoder_model = load_model('decoder_model.h5')

3、推理模型搭建

def decode_sequence(input_seq):
    # Encode the input as state vectors.
    states_value = encoder_model.predict(input_seq)

    # Generate empty target sequence of length 1.
    target_seq = np.zeros((1, 1, num_decoder_tokens))
    # Populate the first character of target sequence with the start character.
    target_seq[0, 0, target_token_index['\t']] = 1.
    # this target_seq you can treat as initial state

    # Sampling loop for a batch of sequences
    # (to simplify, here we assume a batch of size 1).
    stop_condition = False
    decoded_sentence = ''
    while not stop_condition:
        output_tokens, h, c = decoder_model.predict([target_seq] + states_value)

        # Sample a token
        # argmax: Returns the indices of the maximum values along an axis
        # just like find the most possible char
        sampled_token_index = np.argmax(output_tokens[0, -1, :])
        # find char using index
        sampled_char = reverse_target_char_index[sampled_token_index]
        # and append sentence
        decoded_sentence += sampled_char

        # Exit condition: either hit max length
        # or find stop character.
        if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length):
            stop_condition = True

        # Update the target sequence (of length 1).
        # append then ?
        # creating another new target_seq
        # and this time assume sampled_token_index to 1.0
        target_seq = np.zeros((1, 1, num_decoder_tokens))
        target_seq[0, 0, sampled_token_index] = 1.

        # Update states
        # update states, frome the front parts
        states_value = [h, c]

    return decoded_sentence

4、進行推理

import numpy as np

input_text = "Call me."
encoder_input_data = np.zeros(
    (1,max_encoder_seq_length, num_encoder_tokens),
    dtype='float32')
for t, char in enumerate(input_text):
    print(char)
    # 3D vector only z-index has char its value equals 1.0
    encoder_input_data[0,t, input_token_index[char]] = 1.


input_seq = encoder_input_data
decoded_sentence = decode_sequence(input_seq)
print('-')
print('Input sentence:', input_text)
print('Decoded sentence:', decoded_sentence)

運行結(jié)果:
【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署(三),如何訓(xùn)練一個中英翻譯模型,lstm,機器翻譯,機器學(xué)習(xí)

以上的代碼可在kaggle上運行:how-to-train-a-chinese-to-english-translator-iii文章來源地址http://www.zghlxwxcb.cn/news/detail-606758.html

到了這里,關(guān)于【如何訓(xùn)練一個中英翻譯模型】LSTM機器翻譯模型部署(三)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 基于transformer的Seq2Seq機器翻譯模型訓(xùn)練、預(yù)測教程

    基于transformer的Seq2Seq機器翻譯模型訓(xùn)練、預(yù)測教程

    機器翻譯(Machine Translation, MT)是一類將某種語言(源語言,source language)的句子 x x x 翻譯成另一種語言(目標(biāo)語言,target language)的句子 y y y 的任務(wù)。機器翻譯的相關(guān)研究早在上世紀(jì)50年代美蘇冷戰(zhàn)時期就開始了,當(dāng)時的機器翻譯系統(tǒng)是基于規(guī)則的,利用兩種語言的單詞、

    2024年02月03日
    瀏覽(19)
  • 【GPT】文本生成任務(wù)(生成摘要、文本糾錯、機器翻譯等的模型微調(diào))

    【GPT】文本生成任務(wù)(生成摘要、文本糾錯、機器翻譯等的模型微調(diào))

    NLG:自然語言生成任務(wù),很多NLP任務(wù)可以被描述為NLG任務(wù),如經(jīng)典的T5模型(text to text transfer transformer模型)就是NLG模型,如文本糾錯任務(wù),輸出正確的文本描述、智能問答根據(jù)一定背景進行推理,然后回答。 主要分為三種: 抽取式摘要:從原文檔中提取現(xiàn)成的句子作為摘要

    2023年04月26日
    瀏覽(30)
  • 【2022吳恩達機器學(xué)習(xí)課程視頻翻譯筆記】3.2線性回歸模型-part-2

    【2022吳恩達機器學(xué)習(xí)課程視頻翻譯筆記】3.2線性回歸模型-part-2

    Let’s look in this video at the process of how supervised learning works. Supervised learning algorithm will input a dataset and then what exactly does it do and what does it output? Let’s find out in this video. Recall that a training set in supervised learning includes both the input features, such as the size of the house and also the output targets,

    2024年02月12日
    瀏覽(15)
  • OJ# 376 機器翻譯

    OJ# 376 機器翻譯

    題目描述 ? 小李的電腦上安裝了一個機器翻譯軟件,他經(jīng)常用這個軟件來翻譯英語文章。 ?這個翻譯軟件的原理很簡單,它只是從頭到尾,依次將每個英文單詞用對應(yīng)的中文含義來替換。對于每個英文單詞,軟件會先在內(nèi)存中查找這個單詞的中文含義, 如果內(nèi)存中有,軟件

    2024年02月11日
    瀏覽(23)
  • NLP——Translation 機器翻譯

    NLP——Translation 機器翻譯

    基于統(tǒng)計的機器翻譯任務(wù)通常通過翻譯模型(Translation Model)和語言模型(Language Model)的結(jié)合來學(xué)習(xí)更強大的翻譯模型。這種結(jié)合被稱為統(tǒng)計機器翻譯(SMT)。 翻譯模型(Translation Model):翻譯模型主要關(guān)注如何將源語言句子翻譯成目標(biāo)語言句子。它使用雙語語料庫進行訓(xùn)練

    2024年02月09日
    瀏覽(20)
  • 機器學(xué)習(xí)&&深度學(xué)習(xí)——機器翻譯(序列生成策略)

    機器學(xué)習(xí)&&深度學(xué)習(xí)——機器翻譯(序列生成策略)

    ?????作者簡介:一位即將上大四,正專攻機器學(xué)習(xí)的保研er ??上期文章:機器學(xué)習(xí)深度學(xué)習(xí)——seq2seq實現(xiàn)機器翻譯(詳細實現(xiàn)與原理推導(dǎo)) ??訂閱專欄:機器學(xué)習(xí)深度學(xué)習(xí) 希望文章對你們有所幫助 上一節(jié)已經(jīng)實現(xiàn)了機器翻譯的模型訓(xùn)練和預(yù)測,逐個預(yù)測輸出序列, 直

    2024年02月12日
    瀏覽(27)
  • 【動手學(xué)深度學(xué)習(xí)】--機器翻譯與數(shù)據(jù)集

    【動手學(xué)深度學(xué)習(xí)】--機器翻譯與數(shù)據(jù)集

    學(xué)習(xí)視頻:機器翻譯數(shù)據(jù)集【動手學(xué)深度學(xué)習(xí)v2】 官方筆記:機器翻譯與數(shù)據(jù)集 機器翻譯 (machine translation)指的是 將序列從一種語言自動翻譯成另一種語言。 事實上,這個研究領(lǐng)域可以追溯到數(shù)字計算機發(fā)明后不久的20世紀(jì)40年代, 特別是在第二次世界大戰(zhàn)中使用計算機破

    2024年02月09日
    瀏覽(25)
  • 什么是自然語言處理的機器翻譯?

    什么是自然語言處理的機器翻譯?

    機器翻譯(Machine Translation,MT)是一種自然語言處理技術(shù),旨在將一種語言的文本自動翻譯成另一種語言。機器翻譯是自然語言處理領(lǐng)域的重要應(yīng)用之一,它可以幫助人們在跨語言交流、文檔翻譯和信息檢索等方面更加便捷和高效。本文將詳細介紹自然語言處理的機器翻譯。

    2024年02月05日
    瀏覽(24)
  • 為什么需要多語言并行機器翻譯?

    為什么需要多語言并行機器翻譯?

    隨著全球化的加速和不同語言之間的交流需求不斷增長,多語言機器翻譯(Multilingual Parallel Machine Translation)成為一個備受關(guān)注的領(lǐng)域。傳統(tǒng)上,機器翻譯系統(tǒng)主要集中于一對特定語言之間的翻譯,但這種單一語言對的模式在面對多語言環(huán)境時存在一些挑戰(zhàn)。因此,多語言并

    2024年02月12日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包