在deeplearning4j中初始化自定义权重

时间:2017-03-15 10:16:32

标签: machine-learning deeplearning4j dl4j

我正在尝试使用DL4J库实现像https://www.youtube.com/watch?v=Fp9kzoAxsA4这样的GANN(遗传算法神经网络)。

遗传学习变量:

  • 基因:生物神经网络权重
  • 健身:移动的总距离。

每个生物的神经网络图层:

  • 输入图层:5个传感器1如果传感器方向有墙,或0,如果没有。enter image description here
  • 输出图层:映射到该生物角度的线性输出。

这是我对生物对象的createBrain方法:

private void createBrain() {
    Layer inputLayer = new DenseLayer.Builder()
            // 5 eye sensors
            .nIn(5)
            .nOut(5)
            // How do I initialize custom weights using creature genes (this.genes)?
            // .weightInit(WeightInit.ZERO)
            .activation(Activation.RELU)
            .build();

    Layer outputLayer = new OutputLayer.Builder()
            .nIn(5)
            .nOut(1)
            .activation(Activation.IDENTITY)
            .lossFunction(LossFunctions.LossFunction.MSE)
            .build();

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(6)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .iterations(1)
            .learningRate(0.006)
            .updater(Updater.NESTEROVS).momentum(0.9)
            .list()
            .layer(0,inputLayer)
            .layer(1, outputLayer)
            .pretrain(false).backprop(true)
            .build();

    this.brain = new MultiLayerNetwork(conf);
    this.brain.init();
}

如果它可能有帮助我已推到这个回购 https://github.com/kareem3d/GeneticNeuralNetwork

这是Creature类 https://github.com/kareem3d/GeneticNeuralNetwork/blob/master/src/main/java/com/mycompany/gaan/Creature.java

我是机器学习的学生,所以如果你看到任何明显的错误请告诉我,谢谢:)

2 个答案:

答案 0 :(得分:2)

我不知道您是否可以在图层配置中设置权重(我无法在API文档中看到),但您可以在初始化模型后获取并设置为网络参数。

要为图层单独设置它们,您可以按照此示例进行操作;

    Iterator paramap_iterator = convolutionalEncoder.paramTable().entrySet().iterator();

    while(paramap_iterator.hasNext()) {
        Map.Entry<String, INDArray> me = (Map.Entry<String, INDArray>) paramap_iterator.next();
        System.out.println(me.getKey());//print key
        System.out.println(Arrays.toString(me.getValue().shape()));//print shape of INDArray
        convolutionalEncoder.setParam(me.getKey(), Nd4j.rand(me.getValue().shape()));//set some random values
    }

如果您想一次性设置网络的所有参数,可以使用setParams()params(),例如;

INDArray all_params = convolutionalEncoder.params();
convolutionalEncoder.setParams(Nd4j.rand(all_params.shape()));//set random values with the same shape

您可以查看API以获取更多信息; https://deeplearning4j.org/doc/org/deeplearning4j/nn/api/Model.html#params--

答案 1 :(得分:0)

对我有用:

    int inputNum = 4;
    int outputNum = 3;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(123)
            .layer(new EmbeddingLayer.Builder()
                    .nIn(inputNum) // Number of input datapoints.
                    .nOut(8) // Number of output datapoints.
                    .activation(Activation.RELU) // Activation function.
                    .weightInit(WeightInit.XAVIER) // Weight initialization.
                    .build())
            .list()
            .layer(new DenseLayer.Builder()
                    .nIn(inputNum) // Number of input datapoints.
                    .nOut(8) // Number of output datapoints.
                    .activation(Activation.RELU) // Activation function.
                    .weightInit(WeightInit.XAVIER) // Weight initialization.
                    .build())
            .layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                    .nIn(8)
                    .nOut(outputNum)
                    .activation(Activation.SOFTMAX)
                    .weightInit(WeightInit.XAVIER)
                    .build())
            .pretrain(false).backprop(false)
            .build();

    MultiLayerNetwork multiLayerNetwork = new MultiLayerNetwork(conf);
    multiLayerNetwork.init();

    Map<String, INDArray> paramTable = multiLayerNetwork.paramTable();
    Set<String> keys = paramTable.keySet();
    Iterator<String> it = keys.iterator();

    while (it.hasNext()) {
        String key = it.next();
        INDArray values = paramTable.get(key);
        System.out.print(key+" ");//print keys
        System.out.println(Arrays.toString(values.shape()));//print shape of INDArray
        System.out.println(values);
        multiLayerNetwork.setParam(key, Nd4j.rand(values.shape()));//set some random values
    }