gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:33:05 26 4
gpt4 key购买 nike

在我问为什么 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com