Deep Learning 4J 学习(一) 异或例子

1.maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jiantsing</groupId>
  <artifactId>dltest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <nd4j.version>0.7.2</nd4j.version>
    <dl4j.version>0.7.2</dl4j.version>
    <datavec.version>0.7.2</datavec.version>
    <scala.binary.version>2.10</scala.binary.version>
  </properties>
  <dependencies>
     <dependency>
         <groupId>org.nd4j</groupId>
         <artifactId>nd4j-native</artifactId>
         <version>${nd4j.version}</version>
     </dependency>
     <dependency>
         <groupId>org.deeplearning4j</groupId>
         <artifactId>dl4j-spark_2.11</artifactId>
         <version>${dl4j.version}</version>
     </dependency>
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-spark_${scala.binary.version}</artifactId>
            <version>${datavec.version}</version>
         </dependency>
        <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-core</artifactId>
        <version>${dl4j.version}</version>
     </dependency>
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.jiantsing.test.XorTest</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.测试类

package com.jiantsing.test;

import org.deeplearning4j.eval.Evaluation;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration.ListBuilder;
import org.deeplearning4j.nn.conf.distribution.UniformDistribution;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer.Builder;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.LossFunctions;

public class XorTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // list off input values, 4 training samples with data for 2
        // input-neurons each
        //创建一个4*2的输入矩阵,初始值为0
        INDArray input = Nd4j.zeros(4, 2);

        // correspondending list with expected output values, 4 training samples
        // with data for 2 output-neurons each
        //创建一个4*2的目标矩阵,初始值为0
        INDArray labels = Nd4j.zeros(4, 2);

        // create first dataset
        // when first input=0 and second input=0
        /*输入矩阵下标
             (0,0)  (0,1)      0    0 
            (1,0)  (1,1)      1    0
            (2,0)  (2,1)      0    1
            (3,0)  (3,1)      1    1
           
          期望值
            (0,0)  (0,1)      1    0
            (1,0)  (1,1)      0    1
            (2,0)  (2,1)      0    1
            (3,0)  (3,1)      1    0
           
             the labels (these should be binarized label matrices such that the specified label has a value of 1 in the desired column with the label)
             列值为1的为期望的列的下标,因此列数就是分类数,列中为1的为期望列
         */
        input.putScalar(new int[]{0, 0}, 0);
        input.putScalar(new int[]{0, 1}, 0);
        // then the first output fires for false, and the second is 0 (see class
        // comment)
        labels.putScalar(new int[]{0, 0}, 1);
        labels.putScalar(new int[]{0, 1}, 0);

        // when first input=1 and second input=0
        input.putScalar(new int[]{1, 0}, 1);
        input.putScalar(new int[]{1, 1}, 0);
        // then xor is true, therefore the second output neuron fires
        labels.putScalar(new int[]{1, 0}, 0);
        labels.putScalar(new int[]{1, 1}, 1);

        // same as above
        input.putScalar(new int[]{2, 0}, 0);
        input.putScalar(new int[]{2, 1}, 1);
        labels.putScalar(new int[]{2, 0}, 0);
        labels.putScalar(new int[]{2, 1}, 1);

        // when both inputs fire, xor is false again - the first output should
        // fire
        input.putScalar(new int[]{3, 0}, 1);
        input.putScalar(new int[]{3, 1}, 1);
        labels.putScalar(new int[]{3, 0}, 1);
        labels.putScalar(new int[]{3, 1}, 0);

        // create dataset object
        DataSet ds = new DataSet(input, labels);

        // Set up network configuration
        NeuralNetConfiguration.Builder builder = new NeuralNetConfiguration.Builder();
        // how often should the training set be run, we need something above
        // 1000, or a higher learning-rate - found this values just by trial and
        // error
        builder.iterations(10000);//迭代次数,建议1000以上
        // learning rate
        builder.learningRate(0.1);//学习率
        // fixed seed for the random generator, so any run of this program
        // brings the same results - may not work if you do something like
        // ds.shuffle()
        builder.seed(123);//随机种子
        // not applicable, this network is to small - but for bigger networks it
        // can help that the network will not only recite the training data
        builder.useDropConnect(false);
        // a standard algorithm for moving on the error-plane, this one works
        // best for me, LINE_GRADIENT_DESCENT or CONJUGATE_GRADIENT can do the
        // job, too - it's an empirical value which one matches best to
        // your problem
        builder.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);
        // init the bias with 0 - empirical value, too
        builder.biasInit(0);//偏置值???
        // from "http://deeplearning4j.org/architecture": The networks can
        // process the input more quickly and more accurately by ingesting
        // minibatches 5-10 elements at a time in parallel.
        // this example runs better without, because the dataset is smaller than
        // the mini batch size
        builder.miniBatch(false);//不适用并行计算,因为数据集规模小

        // create a multilayer network with 2 layers (including the output
        // layer, excluding the input payer)
        ListBuilder listBuilder = builder.list();

        DenseLayer.Builder hiddenLayerBuilder = new DenseLayer.Builder();//全连接层,隐藏层
        // two input connections - simultaneously defines the number of input
        // neurons, because it's the first non-input-layer
        hiddenLayerBuilder.nIn(2);//两个输入值
        // number of outgooing connections, nOut simultaneously defines the
        // number of neurons in this layer
        hiddenLayerBuilder.nOut(8);//输出为什么为4???,可以变
        // put the output through the sigmoid function, to cap the output
        // valuebetween 0 and 1
        hiddenLayerBuilder.activation(Activation.SIGMOID);//阈值函数,s
        // random initialize weights with values between 0 and 1
        hiddenLayerBuilder.weightInit(WeightInit.DISTRIBUTION);
        hiddenLayerBuilder.dist(new UniformDistribution(0, 1));

        // build and set as layer 0
        listBuilder.layer(0, hiddenLayerBuilder.build());

        // MCXENT or NEGATIVELOGLIKELIHOOD (both are mathematically equivalent) work ok for this example - this
        // function calculates the error-value (aka 'cost' or 'loss function value'), and quantifies the goodness
        // or badness of a prediction, in a differentiable way
        // For classification (with mutually exclusive classes, like here), use multiclass cross entropy, in conjunction
        // with softmax activation function
        Builder outputLayerBuilder = new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD);
        // must be the same amout as neurons in the layer before
        outputLayerBuilder.nIn(8);//与隐藏层的out要相同???
        // two neurons in this layer
        outputLayerBuilder.nOut(2);//目标输出个数,与label矩阵一致
        outputLayerBuilder.activation(Activation.SOFTMAX);
        outputLayerBuilder.weightInit(WeightInit.DISTRIBUTION);
        outputLayerBuilder.dist(new UniformDistribution(0, 1));//输出的上下限值
        listBuilder.layer(1, outputLayerBuilder.build());

        // no pretrain phase for this network
        listBuilder.pretrain(false);

        // seems to be mandatory
        // according to agibsonccc: You typically only use that with
        // pretrain(true) when you want to do pretrain/finetune without changing
        // the previous layers finetuned weights that's for autoencoders and
        // rbms
        listBuilder.backprop(true);

        // build and init the network, will check if everything is configured
        // correct
        MultiLayerConfiguration conf = listBuilder.build();
        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        // add an listener which outputs the error every 100 parameter updates
        net.setListeners(new ScoreIterationListener(100));

        // C&P from GravesLSTMCharModellingExample
        // Print the number of parameters in the network (and for each layer)
        Layer[] layers = net.getLayers();
        int totalNumParams = 0;
        for (int i = 0; i < layers.length; i++) {
            int nParams = layers[i].numParams();//10,12=22数值是怎么来的???
            System.out.println("Number of parameters in layer " + i + ": " + nParams);
            totalNumParams += nParams;
        }
        System.out.println("Total number of network parameters: " + totalNumParams);

        // here the actual learning takes place
        net.fit(ds);

        // create output for every training sample
        INDArray output = net.output(ds.getFeatureMatrix());
        System.out.println(output);//训练结果

        // let Evaluation prints stats how often the right output had the
        // highest value
        Evaluation eval = new Evaluation(2);
       
       
       
        eval.eval(ds.getLabels(), output);
        System.out.println(eval.stats());//打印归类结果
       
        System.out.println("测试结果:");
        INDArray output2 = Nd4j.zeros(2, 2);
        output2.putScalar(new int[]{0, 0}, 0);
        output2.putScalar(new int[]{0, 1}, 0);
        output2.putScalar(new int[]{1, 0}, 1);
        output2.putScalar(new int[]{1, 1}, 1);
       
        System.out.println(net.output(output2));//输出测试结果
       

    }

}

 

3.输出结果

Number of parameters in layer 0: 24
Number of parameters in layer 1: 18
Total number of network parameters: 42
[[1.00, 0.00],
[0.00, 1.00],
[0.00, 1.00],
[1.00, 0.00]]

Examples labeled as 0 classified by model as 0: 2 times
Examples labeled as 1 classified by model as 1: 2 times


==========================Scores========================================
Accuracy:        1
Precision:       1
Recall:          1
F1 Score:        1
========================================================================
测试结果:
[[1.00, 0.00],
[1.00, 0.00]]

 

4.源代码

https://github.com/gjq246/deeplearning4jtest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
deeplearning4j是基于java的深度学习库,当然,它有许多特点,但暂时还没学那么深入,所以就不做介绍了 需要学习dl4j,无从下手,就想着先看看官网的examples,于是,下载了examples程序,结果无法运行,总是出错,如下: 查看一周的错误,也没有成功,马上就要放弃了,结果今天在论坛一大牛指导下,终于成功跑起,下面,将心酸的环境配置过程记录如下,以备自己以后查阅,同时,也希望各种高手可以指点,毕竟,本人还是菜鸟一枚 1.安装JAVA运行环境 该部分,网上有许多教程,这里不再赘述,首先,就是安装一个JDK,然后,再安装一个自己喜欢的IED,这里,以eclispe为例 好了,java的运行环境配置好了,接下来,开始配置dl4j的运行环境,它的官网上给了好复杂的设置步骤,照着做看一些后,发现根本无法进行,结果发现,不需要全部设置完成,就可以运行它的例子了,所以,本人并没有按照官网的教程全部设置,只是设置到了可以运行官网的examples为止,可能存在隐患吧,但本人能力有限,实在无从下手,还期待高手指定 2.按照Maven 按照教程安装Maven,该教程讲述非常详细 (1)下载Maven3,3,3,以win7 64位为例 下载地址:https://maven.apache.org/download.cgi (2)将Maven解压到某个文件夹中,这里以“C:\Program Files\apache-maven-3.3.3”为例 (3)配置环境变量:将maven中的bin的路径添加到system variables的PATH中 (4)测试maven是否安装成功 在命令行中输入mvn -version 如果如下下图所示结果,证明配置正确 3. 下载dl4j的examples,网址为: https://github.com/deeplearning4j/dl4j-0.4-examples 4.打开eclipse,导入刚刚下载的dl4j的examples,具体地: 打开eclipse后->File->import->Maven Existing Maven Projects,在Root Directory中选择examples的文件夹 然后,Finish 这样,examples被成功导入 当然,由于Maven会自动导入程序所需的jar文件(在配置文件pom.xml中所提及),所以,会花费一些时间自动下载这些文件 点击运行,出现如下错误: 这个问题困扰了本人一周,终于解决,是因为系统缺少dll文件所致 5. 下载dll文件,地址为https://www.dropbox.com/s/6p8yn3fcf230rxy/ND4J_Win64_OpenBLAS-v0.2.14.zip?dl=1 下载后,将该文件随意放入一个文件夹中,这里以“C:/BLAS”为例 将所有下载得到的dll文件放入该文件夹,并且,将该路径添加至环境变量Path中 6.此时,再运行刚刚的examples,发现程序终于可以正常运行了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值