之前我问为什么GAE在这里找不到TensorFlow libhttps://stackoverflow.com/questions/40241846/why-googleappengine-gives-me-importerror-no-module-named-tensorflow https://stackoverflow.com/questions/40241846/why-googleappengine-gives-me-importerror-no-module-named-tensorflow
And Dmytro Sadovnychyi
告诉我GAE不能运行TensorFlow,但GAE灵活可以。
所以我在美国区域创建了我的项目并尝试部署我的简单项目:
import webapp2
import tensorflow as tf
class MainHandler(webapp2.RequestHandler):
def get(self):
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
self.response.write(sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
self.response.write(sess.run(a + b))
#self.response.write('asd');
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
witn vm: true
在 yaml 中。
这是yaml:
application: tstmchnlrn
version: 1
runtime: python27
vm: true
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
部署成功,但在访问我的应用程序时出现服务器内部错误appspot
控制台仍然向我显示ImportError: No module named tensorflow
.
我需要做什么才能使基于 TensorFlow 的应用程序运行flexible enviroment
?
为了帮助其他人,我发布了用于谷歌应用程序引擎灵活环境的 hello world 张量流代码,使用Python 3(我知道原来的问题是针对 python 2.7 提出的)。另请注意,webapp2 尚未与 python 3 兼容,因此我使用 Flask。
完整代码是
要求.txt
Flask==0.12.2
gunicorn==19.7.1
tensorflow==1.3.0
app.yaml
runtime: python
threadsafe: yes
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
main.py
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START app]
import logging
import platform
import tensorflow as tf
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
# Simple hello world using TensorFlow
# Create a Constant op
# The op is added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
hello = tf.constant('Hello, TensorFlow!')
# Start tf session
sess = tf.Session()
return sess.run(hello).decode()+' Python '+ platform.python_version()
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]
同样的代码也发布在github上here https://github.com/armujahid/appengine-tensorflow-python3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)