Python 如何在Google App Engine灵活环境中运行TensorFlow?
在我问GAE为什么在这里找不到TensorFlow lib之前 Dmytro Sadovnychi告诉我GAE不能运行TensorFlow,但GAE flexible可以 因此,我在美国专区创建了我的项目,并尝试部署我的简单项目:Python 如何在Google App Engine灵活环境中运行TensorFlow?,python,google-app-engine,tensorflow,Python,Google App Engine,Tensorflow,在我问GAE为什么在这里找不到TensorFlow lib之前 Dmytro Sadovnychi告诉我GAE不能运行TensorFlow,但GAE flexible可以 因此,我在美国专区创建了我的项目,并尝试部署我的简单项目: import webapp2 import tensorflow as tf class MainHandler(webapp2.RequestHandler): def get(self): hello = tf.constant('Hel
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)
witnvm: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:没有名为tensorflow的模块
我需要做什么才能使基于TensorFlow的应用程序在灵活的环境中运行?这听起来好像依赖项没有被推送到实例中
创建一个requirements.txt
文件,并在其中包含Tensor Flow。为了帮助其他人,我将使用Python 3发布我的hello world Tensor Flow代码,用于google app engine flexible environment(我知道最初的问题是针对Python 2.7提出的)。还要注意,webapp2还不能与python3兼容,所以我使用的是Flask
完整的代码是
requirements.txt
Flask==0.12.2
gunicorn==19.7.1
tensorflow==1.3.0
应用程序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]
#版权所有2015 Google Inc.保留所有权利。
#
#根据Apache许可证2.0版(以下简称“许可证”)获得许可;
#除非遵守许可证,否则不得使用此文件。
#您可以通过以下方式获得许可证副本:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#除非适用法律要求或书面同意,软件
#根据许可证进行的分发是按“原样”进行分发的,
#无任何明示或暗示的保证或条件。
#请参阅许可证以了解管理权限和权限的特定语言
#许可证下的限制。
#[启动应用程序]
导入日志记录
导入平台
导入tensorflow作为tf
从烧瓶进口烧瓶
app=烧瓶(名称)
@应用程序路径(“/”)
def hello():
“”“返回友好的HTTP问候语。”“”
#使用TensorFlow的简单hello world
#创建一个常量op
#op将作为节点添加到默认图形中。
#
#构造函数返回的值表示输出
#常数op。
hello=tf.constant('hello,TensorFlow!')
#启动tf会话
sess=tf.Session()
返回sess.run(hello.decode()+'Python'+platform.Python_version())
@app.errorhandler(500)
def服务器_错误(e):
logging.exception('请求期间发生错误')
返回“”
发生内部错误:{}
有关完整堆栈跟踪,请参阅日志。
“”。格式(e),500
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#这在本地运行时使用。Gunicorn用于运行
#谷歌应用程序引擎上的应用程序。请参见app.yaml中的entrypoint。
app.run(host='127.0.0.1',port=8080,debug=True)
#[结束应用程序]
同样的代码也发布在github上,因此,我创建了requirements.txt文件,并将其与我的yaml
和py
文件放在一起。它的内容只是一个链接:https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl
。在部署这个项目并重新测试之后,我仍然得到了关于TensorFlow模块not found的相同错误。要让VM安装TensorFlow,我还需要做其他特殊的事情吗?我通过PyCharm部署项目。存储链接是404。应该公开吗?我不知道。。。但这个命令在控制台中运行良好:Metafalica@DESKTOP-MC1D431:~$pip安装--升级https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl 下载/解包https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl 正在下载tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl(39.8MB):97%39.0MB