训练模型后从 Keras/Tensorflow 获取预测

时间:2021-01-27 16:50:43

标签: python tensorflow machine-learning keras nlp

我正在从事一个涉及神经机器翻译(将英语翻译成法语)的项目。

我已经完成了一些在线示例,现在已经完成了模型。使用 Keras 训练模型后,我如何在不再次训练整个模型的情况下获得翻译预测,因为对于我使用的大型数据集,每个时期都需要一些时间,当然,我无法训练模型每次我想要翻译。

那么在不再次训练整个模型的情况下对新输入生成预测的正确方法是什么?

谢谢

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

当拟合结束时,您需要使用以下命令保存模型、模型及其权重:

<cfsavecontent variable="soapBody">
    <cfoutput>
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ass="http://XXXXX">
           <soapenv:Header/>
           <soapenv:Body>
              <ass:LogAgPreserveFiling>
                   <ass:AG.PRES.NR>#ag_pres_nr#</ass:AG.PRES.NR>
                   <ass:TC>04</ass:TC>
                   <ass:EMP.NR>#emp_nr#</ass:EMP.NR>
                   <ass:LCAQ.DT.RCVD>#dt_rcvd#</ass:LCAQ.DT.RCVD>
              </ass:LogAgPreserveFiling>
           </soapenv:Body>
        </soapenv:Envelope>
    </cfoutput>
</cfsavecontent>

<cfhttp url="XXXXXXXXXXXXX" method="post" result="httpResponse">
    <cfhttpparam type="header" name="SOAPAction" value="XXXXXXXXXXXXXXX"/>
    <cfhttpparam type="header" name="content-type" value="text/xml">
    <cfhttpparam type="header" name="Authorization" value="Basic XXXXXXXXXXXXXXXXX" />
    <cfhttpparam type="header" name="accept-encoding" value="no-compression" />
    <cfhttpparam type="xml" value="#trim( soapBody )#" />
</cfhttp>


<cfif find( "200", httpResponse.statusCode )>
    <cfset soapResponse = xmlParse( httpResponse.fileContent ) />
    <cfset responseNodes = xmlSearch( soapResponse, "//*[ local-name() = 'VCfaultdetails'] " ) />
    <cfif arrayLen(responseNodes) NEQ 0>
    <cfoutput>
        Code: #responseNodes[ 1 ].errorcode#
        <br />
        Message: #responseNodes[ 1 ].message#
    </cfoutput>
    <cfelse>
        Success!!!!!!!!
    </cfif>
</cfif>

您可以随时使用

加载训练好的模型
keras.model.save(model_name)

然后执行预测

model = keras.load(model_name)

希望能帮到你

答案 1 :(得分:0)

您可以使用 .predict() 函数,您可以将新输入传递给它,它会为您提供预测。此函数的文档在这里:keras