在训练和测试了RNN模型之后,我创建了文件,第一个是server.py,第二个是request.py。这两个文件的代码都在下面给出,但出现如下错误:
回溯(最近通话最近):文件 “ /anaconda3/lib/python3.6/site-packages/flask/app.py”,第2309行,在 致电 返回self.wsgi_app(environ,start_response)文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,第2295行,在 wsgi_app 响应= self.handle_exception(e)文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行1741,在 handle_exception reraise(exc_type,exc_value,tb)文件“ /anaconda3/lib/python3.6/site-packages/flask/_compat.py”,第35行,在 加薪 提高价值文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行2292,在 wsgi_app 响应= self.full_dispatch_request()文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行1815,在 full_dispatch_request rv = self.handle_user_exception(e)文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行1718,在 handle_user_exception reraise(exc_type,exc_value,tb)文件“ /anaconda3/lib/python3.6/site-packages/flask/_compat.py”,第35行,在 加薪 提高价值文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行1813,在 full_dispatch_request rv = self.dispatch_request()文件“ /anaconda3/lib/python3.6/site-packages/flask/app.py”,行1799,在 dispatch_request 返回self.view_functionsrule.endpoint文件“ /Users/aniket/RNN/server.py”,第25行,以inde response = model.predict(exp)文件“ /anaconda3/lib/python3.6/site-packages/keras/engine/training.py”, 行1164,在预测中 self._make_predict_function()文件“ /anaconda3/lib/python3.6/site-packages/keras/engine/training.py”, _make_predict_function中的第554行 kwargs)文件“ /anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, 2744行,功能正常 返回函数(输入,输出,更新=更新,** kwargs)文件“ /anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, __init__中的第2546行 使用tf.control_dependencies(self.outputs):文件“ /anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 第5004行,在control_dependencies中 返回get_default_graph()。control_dependencies(control_inputs)文件 “ /anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 行4543,在control_dependencies中 c = self.as_graph_element(c)文件“ /anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 第3490行,位于as_graph_element中 返回self._as_graph_element_locked(obj,allow_tensor,allow_operation)文件 “ /anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 第3569行,在_as_graph_element_locked中 引发ValueError(“ Tensor%s不是此图的元素。”%obj)** ValueError:Tensor Tensor(“ activation_4 / Relu:0”,shape =(?,?,1), dtype = float32)不是该图的元素。
server.py代码:
# Import libraries
import numpy as np
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
# Load the model
model = pickle.load(open('modell.pkl','rb'))
@app.route('/api/predict/',methods=['POST'])
def predict():
# Get the data from the POST request.
data = request.get_json(force=True)
data1 = json.loads(data)
exp=np.reshape(data1,(1,1,12))
# Make prediction using model loaded from disk as per the data.
prediction = model.predict(exp)
# Take the first value of prediction
output = prediction[0]
return jsonify(output)
if __name__ == '__main__':
app.run(port=5000, debug=True)
request.py代码:
import numpy as np
import json
import requests
url = 'http://localhost:5000/api/predict/'
x=[0.545455,0.0333333,0,1,1,0.185,0.489233,0.576471,0.979147,0.262753,0.963205,0.908989])
x=json.dumps(x)
r = requests.post(url,json=x)
print(r.json())