Keras+Tensorflow部署在Jupyter+Spark+Docker教程

#安裝Keras on Docker的兩種方法 Keras是一個機器學習的框架,支持Theano和Tensorflow做爲神經網絡計算引擎。Keras是採用Python的,能夠配置經過Jupyter進行使用,就很是方便了。爲了安裝和部署方便,咱們將其放到Docker中運行。html

A. 爲了在Docker中安裝Keras,咱們能夠選擇一個Docker基礎鏡像。可是沒有找到安裝Jupyter的版本,咱們本身能夠手動進行安裝( http://www.javashuo.com/article/p-pfqfojqe-he.html ),只是步驟稍微多一些。node

B. 咱們也可使用Jupyter官方的基礎鏡像(https://github.com/jupyter/docker-stacks ),而後在裏面安裝Keras和Tensorflow。這裏主要介紹這一種方法,優點是Python和Juyter的支持很是完整。python

1. 安裝支持環境

1.1 安裝Docker容器支持環境

我用的Mac OS X,如今的Docker已經原生支持了,不用像之前要安裝VirtualBox,再使用Vagrant了。
到這裏http://www.docker.io下載相應的版本,而後運行安裝程序,就能夠了。git

若是使用Ubuntu,建議使用 http://get.docker.io下載的腳本進行安裝,官方的安裝方法貌似與Ubuntu內置的lxd有衝突,常常出問題。方法以下:github

wget http://get.docker.io -O docker-setup.sh
sudo chmod +x docker-setup.sh
./docker-setup.sh

1.2 安裝Jupyter Notebook容器

採用Jupyter的官方Docker技術棧docker

docker run -it --name GISpark 
    -p 9000:8888 
    --user root -e GRANT_SUDO=yes 
    -v /本地目錄/GISpark:/home/jovyan/work/GISpark 
    jupyter/all-spark-notebook

1.3 安裝和更新容器內軟件

打開瀏覽器,輸入:http://localhost:9000。vim

2. 安裝機器學習支持庫

2.1 安裝python基礎開發包

sudo apt update
sudo apt upgrade
source activate root

sudo apt install -y python3-dev python3-pip python3-nose gcc g++ git gfortran vim
sudo apt install -y libopenblas-dev liblapack-dev libatlas-base-dev
sudo pip3 install -U --pre pip setuptools wheel
sudo pip3 install -U --pre numpy scipy matplotlib scikit-learn scikit-image
sudo pip3 install -U --pre keras

** 注意,由於該鏡像中安裝了最新的conda版本管理工具,使用的是Python3.5,而操做系統安裝的Python3.4,兩者的目錄結構不同。所以,應先使用source activate root切換到conda的環境,再進行安裝。**瀏覽器

2.2 安裝Theano支持庫

sudo pip3 install -U --pre theano

2.3 安裝Tensorflow支持庫

conda install -c conda-forge tensorflow

參考:網絡

3. 運行測試

點取「New」按鈕,選取「Python3」,建立一個新的Notebook。 建立新的Notebooksession

3.1 驗證Theano

在新建立的notebook中,輸入如下內容,按shift+enter鍵運行。

import theano
import keras
help(theano)

若是出現下面信息,就安裝成功了。

Help on package keras:

NAME
    keras

PACKAGE CONTENTS
    activations
    applications (package)
    backend (package)
    callbacks
    constraints
    datasets (package)
    engine (package)
    initializations
    layers (package)
    legacy (package)
    metrics
    models
    objectives
    optimizers
    preprocessing (package)
    regularizers
    utils (package)
    wrappers (package)

DATA
    absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...

VERSION
    1.1.0

FILE
    /opt/conda/lib/python3.5/site-packages/keras/__init__.py

3.2 驗證Tensorflow

再新建立notebook,輸入如下內容,按shift+enter鍵運行。

import tensorflow as tf

# Create a Constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)

# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()

若是成功運行,代表Tensorflow也安裝成功了。

相關文章
相關標籤/搜索