python - 如何在 Google App Engine 柔性环境中运行 TensorFlow?

标签 python google-app-engine tensorflow

在我问为什么 GAE 在这里找不到 TensorFlow 库之前 https://stackoverflow.com/questions/40241846/why-googleappengine-gives-me-importerror-no-module-named-tensorflow

Dmytro Sadovnychyi 告诉我,GAE 不能运行 TensorFlow,但 GAE flexible 可以。

所以我在美国区创建了我的项目并尝试部署我的简单项目:

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)

在 yaml 中使用 vm: true

这是 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 的应用在灵活的环境中运行,我需要做什么?

最佳答案

为了帮助其他人,我发布了使用 Python 3 的适用于 google app engine 灵活环境的 hello world tensorflow 代码(我知道原始问题是针对 python 2.7 提出的)。另请注意,webapp2 尚不兼容 python 3,因此我使用的是 Flask。

完整代码为

requirements.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

主.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

关于python - 如何在 Google App Engine 柔性环境中运行 TensorFlow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310602/

相关文章:

amazon-web-services - AWS 批量限制单个主机上的容器数量

tensorflow - 使用张量板绘制图表

python - 如何使用 argparse 让用户更改现有程序的输出?

python - 在 Python 中处理提示 - popen

javascript - 如何在 Python 中创建一个由其他 JSON 对象的部分组成的新 JSON 对象?

google-app-engine - 部署后 AppEngine 500 服务器错误 - 代码错误?

google-app-engine - 使用 Google Cloud 和图像优化创建 CDN

eclipse - GWT 重置清除 *.cache.html 文件

python - 'IndexError: list index out of range'是什么?

python - Tensorflow pad序列特征列