Saturday, October 7, 2017

How to use DL4J for Linux

I am using Linux mint for my OS. The official quick start guide is here.
Suppose we are using Linux OS like Linux mint or Ubuntu.

At first, we will install JDK.
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
$ sudo apt-get install openjfx
$ java -version

openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

Then install Maven.
$ sudo apt-get install maven
$ mvn -version

Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_144, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.8.0-53-generic", arch: "amd64", family: "unix"

Maven is a project management tool, which is aimed for Java projects.
Available commands of Maven:
install
clean
compile
package
validate
test-compile
test
integration-test
verify
deploy

Install git.
$ sudo apt-get install git
$ git --version

git version 2.7.4

Create a workspace folder.
$ mkdir ~/dlj
$ mkdir ~/dlj/workspace
$ cd ~/dlj/workspace

Install dl4j from git.
$ git clone https://github.com/deeplearning4j/dl4j-examples.git
$ cd dl4j-examples/

If you want to use Nvidia's GPU for learning, change some of the POM in the root directory.
<name>DeepLearning4j Examples Parent</name>
        <description>Examples of training different data sets</description>
        <properties>
            <nd4j.backend>nd4j-cuda-8.0-platform</nd4j.backend>

like this


Then install.
$ mvn clean install

Install intellij (That is an integrated development environment for Java):
$ mkdir ~/intellij
$ cd ~/intellij
$ wget https://download.jetbrains.com/idea/ideaIC-2017.2.5.tar.gz
$ tar xfz ideaIC-2017.2.5.tar.gz
$ cd idea-IC-172.4343.14/bin
$ ./idea.sh
Or download intellij from the official website: https://www.jetbrains.com/idea/download/#section=linux

Open intellij and choose "import project".


Choose the folder of dl4j-examples which we downloaded just now and click "OK".


Choose "Maven" and click "Next".


Just click "Next" without touching anything.

Tick "open jfx" and click "Next".


Tick the URL of dl4j and click "Next".



Tick the SDK and click "Next".


Click "Finish".


Wait until the loading is finished.


Choose an example which you want to run and right-click on it. Then menu will be shown. Click "Run" from the menu. Then the deep learning will be started.

I will try LenetMnistExample:

The result of LenetMnistExample:
==========================Scores========================================
 # of classes:    10
 Accuracy:        0.9746
 Precision:       0.9748
 Recall:          0.9745
 F1 Score:        0.9744
Precision, recall & F1: macro-averaged (equally weighted avg. of 10 classes)
========================================================================
o.d.e.c.LenetMnistExample - ****************Example finished********************