Kako izraditi mehanizam za preporuke pomoću Apacheova poslužitelja za predviđanje IO strojnog učenja

Ovaj će vas post voditi kroz instaliranje Apache Prediction IO poslužitelja za strojno učenje. Upotrijebit ćemo jedan od njegovih predložaka pod nazivom Preporuka za izradu funkcionalnog mehanizma za preporuke. Gotovi proizvod moći će preporučiti prilagođene proizvode ovisno o kupčevom ponašanju određenog korisnika.

Problem

Imate gomilu podataka i trebate nešto točno predvidjeti kako biste svom poslovanju pomogli povećati prodaju, povećati kupce, povećati profit, povećati konverziju ili što god već trebate za posao.

Sustavi preporuka vjerojatno su prvi korak prema primjeni znanosti o podacima i strojnom učenju. Strojevi za preporuke koriste podatke kao ulaz i nad njima prelaze svoje algoritme. Zatim izlaze modeli iz kojih možemo prognozirati što će korisnik doista kupiti ili što se korisniku može svidjeti ili ne.

Unesite Predviđanje IO

"Apache PredictionIO (inkubacija) je otvoreni izvor poslužitelja za strojno učenje izgrađen na vrhu modernog otvorenog koda za programere i znanstvenike podataka koji stvaraju mehanizme za predviđanje za bilo koji zadatak strojnog učenja." - Apache Prediction IO dokumentacija

Već pri prvom pogledu na dokumentaciju osjećam se dobro jer mi daje pristup moćnom tehnološkom paketu za rješavanje problema strojnog učenja. Još je zanimljivije to što Prediction IO daje pristup mnogim predlošcima koji su korisni za rješavanje stvarnih problema.

Galerija predložaka sadrži brojne predloške za preporuke, klasifikaciju, regresiju, obradu prirodnog jezika i još mnogo toga. Koristi tehnologiju poput Apache Hadoop, Apache spark, ElasticSearch i Apache Hbase kako bi poslužitelj za strojno učenje bio skalabilan i učinkovit. Neću puno govoriti o samom predviđanju IO-a, jer to ovdje možete sami.

Vratimo se na problem: imam gomilu podataka iz povijesti kupovine korisnika, koji se sastoji od user_id, product_id i kupljeni_datum. Pomoću njih trebam dati prilagođeno predviđanje / preporuku korisniku. Uzimajući u obzir ovaj problem, poslužit ćemo se predloškom preporuke s poslužiteljem za predviđanje IO strojnog učenja. Upotrijebit ćemo poslužitelj događaja I Prediction IO kao i skupni uvoz podataka.

Pa krenimo naprijed. (Napomena: Ovaj vodič pretpostavlja da za instalaciju koristite sustav Ubuntu)

1. korak: preuzmite Apache Prediction IO

Idite na početni direktorij trenutnog korisnika i preuzmite najnoviji 0.10.0 Predviđanje IO apache inkubator. Pretpostavljam da ste u sljedećem direktoriju(/home/you/)

git clone [email protected]:apache/incubator-predictionio.git

Sada idite u direktorij ` incubator-predictionio` gdje smo klonirali repo predviđanja IO. Ako ste ga klonirali u drugi direktorij, pobrinite se da budete unutar tog direktorija na vašem terminalu.

Sada provjerimo trenutnu stabilnu verziju Prediction IO koja je 0.10.0

cd incubator-predictionio # or any dir where you have cloned pio.git checkout release/0.10.0

Korak # 2: Napravimo distribuciju predviđanja IO

./make-distribution.sh

Ako je sve prošlo u redu, na vašu konzolu dobit ćete poruku poput ove:

Međutim, ako ste naišli na ovako nešto:

onda će morati ukloniti .ivy2dir u svom domu imenik, po defaultu ova mapa je skrivena. Morate ga potpuno ukloniti, a zatim ./make-distribution.shponovo pokrenuti za izgradnju kako bi uspješno generirao datoteku za distribuciju.

Osobno sam se suočio s tim problemom mnogo puta, ali nisam siguran da je to valjani način za rješavanje ovog problema. Ali uklanjanje .ivy2mape i ponovno pokretanje naredbe make-distribution djeluje.

Korak # 3: Izdvojite distribucijsku datoteku

Nakon uspješne izrade, imat ćemo naziv datoteke pod nazivom PredictionIO-0.10.0-incubating.tar.gz unutar direktorija u kojem smo izgradili svoj Prediction IO. Sad ga izvucimo u direktorij zvan pio.

mkdir ~/piotar zxvf PredictionIO-0.10.0-incubating.tar.gz -C ~/pio

Provjerite podudara li se naziv datoteke tar.gz s distribucijskom datotekom koju imate u izvornom direktoriju predviđanja. Ako ste zaboravili provjeriti verziju 0.10.0 Prediction IO, sigurno ćete dobiti drugo ime datoteke, jer bi inačica po zadanim postavkama bila najnovija.

Korak # 4: Pripremite se za preuzimanje ovisnosti

cd ~/pio
#Let’s make a vendors folder inside ~/pio/PredictionIO-0.10.0-incubating where we will save hadoop, elasticsearch and hbase.
mkdir ~/pio/PredictionIO-0.10.0-incubating/vendors

Korak # 5: Preuzimanje i postavljanje Spark

wget //d3kbcqa49mib13.cloudfront.net/spark-1.5.1-bin-hadoop2.6.tgz

Ako je vaš trenutni direktorij ~/pionaredba, preuzet će iskru u pio dir. Sad ga izvadimo. Ovisno o tome gdje ste ga preuzeli, možda biste željeli promijeniti naredbu u nastavku.

tar zxvfC spark-1.5.1-bin-hadoop2.6.tgz PredictionIO-0.10.0-incubating/vendors
# This will extract the spark setup that we downloaded and put it inside the vendors folder of our fresh pio installation. 

Provjerite jeste li to učinili mkdir PredictionIO-0.10.0-incubating/vendorsranije.

Korak # 6: Preuzimanje i postavljanje ElasticSearch

wget //download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.tar.gz
#Let’s extract elastic search inside vendors folder.
tar zxvfC elasticsearch-1.4.4.tar.gz PredictionIO-0.10.0-incubating/vendors

Korak # 7: Preuzmite i postavite Hbase

wget //archive.apache.org/dist/hbase/hbase-1.0.0/hbase-1.0.0-bin.tar.gz
#Let’s extract it.
tar zxvfC hbase-1.0.0-bin.tar.gz PredictionIO-0.10.0-incubating/vendors

Ajmo sada urediti hbase-site.xmlda usmjeri hbase konfiguraciju na desni direkt. S obzirom da se nalazite u ~/piodirektoriju, mogli biste pritisnuti ovu naredbu i urediti hbase conf.

nano PredictionIO-0.10.0-incubating/vendors/hbase-1.0.0/conf/hbase-site.xml

Zamijenite konfiguracijski blok sljedećom konfiguracijom.

  hbase.rootdir file:///home/you/pio/PredictionIO-0.10.0-incubating/vendors/hbase-1.0.0/data   hbase.zookeeper.property.dataDir /home/you/pio/PredictionIO-0.10.0-incubating/vendors/hbase-1.0.0/zookeeper 

Ovdje " vi" označava vaš korisnički direktorij, na primjer, ako sve to radite kao korisnik "tom", to bi bilo nešto poput file :: /// home / tom / ...

Provjerite nalaze li se prave datoteke.

Sada postavimo JAVA_HOME u hbase-env.sh.

nano PredictionIO-0.10.0-incubating/vendors/hbase-1.0.0/conf/hbase-env.sh

If you’re unsure about which version of JDK you’re currently using, follow these step and make necessary changes if required.

We need Java SE Development Kit 7 or greater for Prediction IO to work. Now let’s make sure we’re using the right version by running:

sudo update-alternatives — config java

By default I’m using:

java -version
openjdk version “1.8.0_121”
OpenJDK Runtime Environment (build 1.8.0_121–8u121-b13–0ubuntu1.16.04.2-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)

If you’re using below 1.7, then you should change the java config to use a version of java that is equal to 1.7 or greater. You can change that with the update-alternatives command as given above. In my case the command sudo update-alternatives -config java outputs something like this:

If you have any trouble setting this up, you can follow this link.

Now let’s export the JAVA_HOME path in the .bashrc file inside /home/you/pio.

Considering you’re on ~/pio dir, you could do this: nano .bashrc

Don’t forget to do source .bashrc after you set up the java home in the .bashrc.

Step #8: Configure the Prediction IO Environment

Now let’s configure pio.env.sh to give a final touch to our Prediction IO Machine learning server installation.

nano PredictionIO-0.10.0-incubating/conf/pio-env.sh

We’re not using ProsgesSQl or MySql for our event server, So let’s comment out that section and have a pio-env.sh something like this:

#!/usr/bin/env bash## Copy this file as pio-env.sh and edit it for your site's configuration.## Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements. See the NOTICE file distributed with# this work for additional information regarding copyright ownership.# The ASF licenses this file to You 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## //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.#
# PredictionIO Main Configuration## This section controls core behavior of PredictionIO. It is very likely that# you need to change these to fit your site.
# SPARK_HOME: Apache Spark is a hard dependency and must be configured.SPARK_HOME=$PIO_HOME/vendors/spark-1.5.1-bin-hadoop2.6
POSTGRES_JDBC_DRIVER=$PIO_HOME/lib/postgresql-9.4-1204.jdbc41.jarMYSQL_JDBC_DRIVER=$PIO_HOME/lib/mysql-connector-java-5.1.37.jar
# ES_CONF_DIR: You must configure this if you have advanced configuration for# your Elasticsearch setup. ES_CONF_DIR=$PIO_HOME/vendors/elasticsearch-1.4.4/conf
# HADOOP_CONF_DIR: You must configure this if you intend to run PredictionIO# with Hadoop 2. HADOOP_CONF_DIR=$PIO_HOME/vendors/spark-1.5.1-bin-hadoop2.6/conf
# HBASE_CONF_DIR: You must configure this if you intend to run PredictionIO# with HBase on a remote cluster. HBASE_CONF_DIR=$PIO_HOME/vendors/hbase-1.0.0/conf
# Filesystem paths where PredictionIO uses as block storage.PIO_FS_BASEDIR=$HOME/.pio_storePIO_FS_ENGINESDIR=$PIO_FS_BASEDIR/enginesPIO_FS_TMPDIR=$PIO_FS_BASEDIR/tmp
# PredictionIO Storage Configuration## This section controls programs that make use of PredictionIO's built-in# storage facilities. Default values are shown below.## For more information on storage configuration please refer to# //predictionio.incubator.apache.org/system/anotherdatastore/
# Storage Repositories
# Default is to use PostgreSQLPIO_STORAGE_REPOSITORIES_METADATA_NAME=pio_metaPIO_STORAGE_REPOSITORIES_METADATA_SOURCE=ELASTICSEARCH
PIO_STORAGE_REPOSITORIES_EVENTDATA_NAME=pio_eventPIO_STORAGE_REPOSITORIES_EVENTDATA_SOURCE=HBASE
PIO_STORAGE_REPOSITORIES_MODELDATA_NAME=pio_modelPIO_STORAGE_REPOSITORIES_MODELDATA_SOURCE=LOCALFS
# Storage Data Sources
# PostgreSQL Default Settings# Please change "pio" to your database name in PIO_STORAGE_SOURCES_PGSQL_URL# Please change PIO_STORAGE_SOURCES_PGSQL_USERNAME and# PIO_STORAGE_SOURCES_PGSQL_PASSWORD accordingly# PIO_STORAGE_SOURCES_PGSQL_TYPE=jdbc# PIO_STORAGE_SOURCES_PGSQL_URL=jdbc:postgresql://localhost/pio# PIO_STORAGE_SOURCES_PGSQL_USERNAME=pio# PIO_STORAGE_SOURCES_PGSQL_PASSWORD=root
# MySQL Example# PIO_STORAGE_SOURCES_MYSQL_TYPE=jdbc# PIO_STORAGE_SOURCES_MYSQL_URL=jdbc:mysql://localhost/pio# PIO_STORAGE_SOURCES_MYSQL_USERNAME=root# PIO_STORAGE_SOURCES_MYSQL_PASSWORD=root
# Elasticsearch Example PIO_STORAGE_SOURCES_ELASTICSEARCH_TYPE=elasticsearch PIO_STORAGE_SOURCES_ELASTICSEARCH_CLUSTERNAME=firstcluster PIO_STORAGE_SOURCES_ELASTICSEARCH_HOSTS=localhost PIO_STORAGE_SOURCES_ELASTICSEARCH_PORTS=9300 PIO_STORAGE_SOURCES_ELASTICSEARCH_HOME=$PIO_HOME/vendors/elasticsearch-1.4.4
# ocal File System ExamplePIO_STORAGE_SOURCES_LOCALFS_TYPE=localfsPIO_STORAGE_SOURCES_LOCALFS_PATH=$PIO_FS_BASEDIR/models
# HBase ExamplePIO_STORAGE_SOURCES_HBASE_TYPE=hbasePIO_STORAGE_SOURCES_HBASE_HOME=$PIO_HOME/vendors/hbase-1.0.0

Step #9: Configure cluster name in ElasticSearch config

Since this line PIO_STORAGE_SOURCES_ELASTICSEARCH_CLUSTERNAME=firstcluster points to our cluster name in the ElasticSearch configuration, let’s replace a default cluster name in ElasticSearch configuration.

nano PredictionIO-0.10.0-incubating/vendors/elasticsearch-1.4.4/config/elasticsearch.yml

Step #10: Export The Prediction IO Path

Let’s now export the Prediction IO path so we could freely use the pio command without pointing to it’s bin every time. Run the following command in your terminal:

PATH=$PATH:/home/you/pio/PredictionIO-0.10.0-incubating/bin; export PATH

Step #11: Give Permission To Prediction IO Installation

sudo chmod -R 775 ~/pio

To je od vitalne važnosti, jer ako nismo dali dozvolu za mapu pio, postupak Iiction Predviđanja neće moći pisati datoteke dnevnika.

Korak # 12: Pokrenite IO poslužitelj predviđanja

Sada smo spremni za početak, pokrenimo naš Prediction IO poslužitelj. Prije pokretanja ove naredbe provjerite jeste li izvezli gore opisanu stazu pio.

pio-start-all
#if you forgot to export the pio path, it won't work and you manually have to point the pio bin path. 

Ako je sve u redu do ove točke, vidjeli biste izlaz ovako nešto.

Napomena: Ako tada zaboravite dati dopuštenje, pojavit će se problemi s pisanjem dnevnika i ako je vaša putanja JAVA_HOME netočna, HBASE se neće pravilno pokrenuti i to će vam dati pogrešku.

Korak # 13: Potvrdite postupak

Sada provjerimo našu instalaciju pomoću pio status, ako je sve u redu, dobit ćete izlaz poput ovog:

Ako naiđete na pogrešku u Hbaseu ili bilo kojoj drugoj pozadinskoj pohrani, provjerite je li sve pravilno pokrenuto.

Our Prediction IO Server is ready to implement the template now.

Implementing the Recommendation Engine

A recommendation engine template is a Prediction IO engine template that uses collaborative filtering to make personalized recommendation to the user. It uses can be in E-commerce site, news site, or any application that collects user histories of event to give a personalized experiences to the user.

We’ll implement this template in Prediction IO with few eCommerce user data, just to do an sample experiment with Prediction IO machine learning server.

Now let’s back to our home dir cd ~

Step #14: Download the Recommendation Template

pio template get apache/incubator-predictionio-template-recommender MyRecommendation

It will ask for company name and author name, input subsequently, now we have a MyRecommendation Template inside our home dir. Just a reminder: you can put the template anywhere you want.

#15. Create Our First Prediction IO App

Now let’s go inside the MyRecommendation dir cd MyRecommendation

After you’re inside the template dir, let’s create our first Prediction IO app called ourrecommendation.

You will get output like this. Please remember that you can give any name to your app, but for this example I’ll be using the app name ourrecommendation.

pio app new ourrecommendation

This command will output something like this:

Let’s verify that our new app is there with this command:

pio app list

Now our app should be listed in the list.

Step #16: Import Some Sample Data

Let’s download the sample-data from gist, and put that inside importdata folder inside MyRecommendation folder.

mkdir importdata

Copy the sample-data.json file that you just created inside the importdata folder.

Finally let’s import the data inside our ourrecommendation app. Considering you’re inside the MyRecommendation dir you can do this to batch import the events.

pio import — appid 1 — input importdata/data-sample.json

(Note: make sure the appid of ourrecommendation is same as of your appid that you just provided)

Step #17: Build The App

Before building the app, let’s edit engine.json file inside the MyRecommendation directory to replicate our app name inside it. It should look something like this:

Note: Don’t copy this, just change the “appName” in your engine.json.

{ "id": "default", "description": "Default settings", "engineFactory": "orgname.RecommendationEngine", "datasource": { "params" : { "appName": "ourrecommendation" } }, "algorithms": [ { "name": "als", "params": { "rank": 10, "numIterations": 5, "lambda": 0.01, "seed": 3 } } ]}

Note: the “engineFactory” will be automatically generated when you pull the template in our step 14, so you don’t have to change that. In my case, it’s my orgname, which I put in the terminal prompt during installation of the template. In you engine.json you just need to modify the appName, please don’t change anything else in there.

In the same dir where our MyRecommendation engine template lies, let’s run this pio command to build our app.

pio build

(Note: if you wanna see all the messages during the building process, you can run this pio build — verbose)

It can take sometimes to build our app, since this is the first time. From next time it takes less time. You should get an output like this:

Our engine is now ready to train our data.

Step #18: Train The dataset

pio train

If you get an error like the one below in the middle of the training, then you may have to change number of iterations inside your engine.json and rebuild the app.

Let’s change the numItirations in engine.json which is by default 20 to 5:

“numIterations”: 5,

Ajmo sad, napravimo aplikaciju pio build, opet pio train. Trening bi trebao biti uspješno završen. Nakon završetka treninga dobit ćete poruku poput ove:

Imajte na umu da ovaj trening radi samo za male podatke, ali ako želite pokušati s velikim nizom podataka, za obavljanje obuke morali bismo postaviti samostalnog radnika s iskrom. (O ovome ću pisati u budućem postu.)

Korak # 19: Postavite i poslužite predviđanje

pio deploy#by default it will take 8000 port.

Sada ćemo imati pokrenut naš poslužitelj za predviđanje.

Napomena: radi jednostavnosti, u ovom postu ne raspravljam o poslužitelju događaja, jer može postati još duži, stoga se usredotočujemo na jednostavan slučaj upotrebe Prediction IO.

Sada ćemo dobiti predviđanje pomoću curl.

Otvorite novi terminal i pritisnite:

curl -H “Content-Type: application/json” \-d ‘{ “user”: “user1”, “num”: 4 }’ //localhost:8000/queries.json

In the above query, the user signifies to the user_id in our event data, and the num means, how many recommendation we want to get.

Now you will get the result like this:

{"itemScores":[{"item":"product5","score":3.9993937903501093},{"item":"product101","score":3.9989989282500904},{"item":"product30","score":3.994934059438341},{"item":"product98","score":3.1035806376677866}]}

That’s it! Great Job. We’re done. But wait, what’s next?

  • Next we will use spark standalone cluster to train large dataset (believe me, its easy, if you wanna do it right now, you could follow the documenation in Prediction IO)
  • We will use Universal Recommender from Action ML to build a recommendation engine.

Important Notes:

  • The template we used uses ALS algorithm with explicit feedback, however you can easily switch to implicit depending upon your need.
  • If you’re curious about Prediction IO and want to learn more you can do that on the Prediction IO official site.
  • If your Java version is not suitable for Prediction IO specification, then you are sure to run into problems. So make sure you configure this first.
  • Don’t run any of the commands described above with sudo except to give permission. Otherwise you will run into problems.
  • Make sure your java path is correct, and make sure to export the Prediction IO path. You might want to add the Prediction IO path to your .bashrc or profile as well depending upon your need.

Update 2017/07/14: Using Spark To Train Real Data Sets

We have the spark installed inside our vendors folders, with our current installation, our spark bin in the following dir.

~/pio/PredictionIO-0.10.0-incubating/vendors/spark-1.5.1-bin-hadoop2.6/sbin

From there we have to setup a spark primary and replica to execute our model training to accomplish it faster. If your training seems to stuck we can use the spark options to accomplish the training tasks.

#Start the Spark primary

~/pio/PredictionIO-0.10.0-incubating/vendors/spark-1.5.1-bin-hadoop2.6/sbin/start-master.sh

This will start the spark primary. Now let’s browse the spark primary’s web UI by going into //localhost:8080/ in the browser.

Now let’s copy the primary-url to start the replica worker. In our case the primary spark URL is something like this:

spark://your-machine:7077 (your machine signifies to your machine name)

~/pio/PredictionIO-0.10.0-incubating/vendors/spark-1.5.1-bin-hadoop2.6/sbin/start-slave.sh spark://your-machine:7077

The worker will start. Refresh the web ui you will see the registered worker this time. Now let’s run the training again.

pio train -- --master spark://localhost:7077 --driver-memory 4G --executor-memory 6G

Great!

Special Thanks: Pat Ferrel From Action ML & Marius Rabenarivo