LoginSignup
1
4

More than 5 years have passed since last update.

Vagrant x DockerでPredictionIOを試す

Last updated at Posted at 2017-01-23

はじめに

PredictionIOの勉強会に参加したので、とりあえず動かしてみようと思いました。
環境は何となく楽そうなDockerを選択。(楽じゃなかった。。)

前提:私の環境

Windows 10 Pro (64bit)
Git for Windows (Git Bash)

VMの準備

Hyper-Vの無効化

VirtualBoxを利用していきますが、Hyper-Vと共存できないので
こちらの記事を参考にbatファイルを作成して無効化しておきます。
Hyper-VとVMware/VirtualBoxは共存不可!?

VirtualBoxのインストール

まずはVirtualBoxをインストールします。
今回インストールしたのは VirtualBox 5.1.14 です。
Download VirtualBox

Vagrantのインストール

次にvagrantをインストールします。
今回インストールしたのは Vagrant 1.9.1 です。
DOWNLOAD VAGRANT

ディレクトリの作成

VM用のディレクトリを用意します。
適宜好きな場所に。

$ cd /c/opt
$ mkdir -p vagrant/machines/predictionio
$ cd vagrant/machiens/predictionio

Vagrantfileの生成

構成ファイルを生成します。
boxはubuntu/trusty64をベースとしてDockerを導入をするより
16.04 LTSでDockerも入っているboxがVagrantbox.esに公開されているのでこちらを利用します。
vagrant box addに結構時間が掛かります。

$ vagrant box add ubuntu/docker https://github.com/jose-lpa/packer-ubuntu_lts/releases/download/v3.1/ubuntu-16.04.box
$ vagrant init --minimal ubuntu/docker

ポートフォワーディングの設定

Vagrantfileに、あとでpredictionioで利用するポートをforwarded_portで追記しておきます。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/docker"
+ config.vm.network "forwarded_port", guest: 8000, host: 8000, auto_correct: true
end

メモリの設定

後述のDockerイメージのビルド時にメモリ不足にならないように
VagrantfileにVMのメモリ設定をします。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/docker"
  config.vm.network "forwarded_port", guest: 8000, host: 8000, auto_correct: true
+ config.vm.provider "virtualbox" do |vb|
+  vb.memory = "2048"
+ end
end

起動してログイン

VMを起動、ログインします。

$ vagrant up
$ vagrant ssh

DNSの設定

Dockerイメージのビルドの為にDNSを設定しておきます。
(参考:Ubuntuでのresolv.confの扱い)

$ sudo vim /etc/network/interfaces
/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp
pre-up sleep 2

+dns-nameservers 8.8.8.8

設定反映

$ sudo service networking restart

Dockerの準備

Dockerファイルの取得

Docker Installation for PredictionIOにある通り、v0.10.0を利用しているsteveny2k/docker-predictionioのリポジトリをcloneしてきます。
*なお、Docker Hubにも色々とイメージが転がっていますが、PredictionIOのバージョンが古いと後で詰まるので要注意(このイメージならいけるかも?)

$ git clone https://github.com/steveny2k/docker-predictionio.git
$ cd docker-predictionio

Dockerイメージのビルドと起動

REDMEに従ってビルド・起動してみます。
Dockerイメージのビルドはかなり時間が掛かります。

$ docker build -t predictionio .
$ docker run -p 8000:8000 --name predictionio_instance -it predictionio /bin/bash

PredictionIOを動かす

関連ライブラリのインストール

Dockerのコンテナ環境にログインしたら、以下の関連ライブラリをインストールします。
また、設定ファイルの編集用にvimもインストールしておきます。

# apt-get update
# apt-get install sudo
# apt-get install vim

# pip install --upgrade pip
# pip install setuptools
# pip install predictionio

PredictionIOサーバの起動

起動に成功すると「Your system is all ready to go.」と表示されます。

# pio-start-all
# pio status

Engineの取得・起動

Engineを取得

# mkdir ~/MySimilarProduct
# pio template get apache/incubator-predictionio-template-similar-product ~/MySimilarProduct
# cd ~/MySimilarProduct/

AppNameを登録してAccessKeyを取得

# pio app new MyApp1

AppNameをengine.jsonに設定

engine.json
---  
  "datasource": {
    "params" : {
-     "appName": "INVALID_APP_NAME"
+     "appName": "MyApp1"
    }
---

学習データの投入

# python data/import_eventserver.py --access_key [取得したキー]

Engine Templateのビルドとトレーニング

# pio build --verbose
# pio train

trainがStackoverflowでこける場合には、とりあえずengine.jsonの修正で逃げる

engine.json
---  
  "algorithms": [
    {
      "name": "als",
      "params": {
        "rank": 10,
-       "numIterations": 20,
+       "numIterations": 10,
        "lambda": 0.01,
        "seed": 3
      }
    }
  ]
---

起動確認

以下にアクセスして起動を確認します。
http://localhost:8000

Todo

とりあえずここまで。
あとは、実際に色々なEngineを試してみたい。

参考

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4