2

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

遗传学习变量:

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

每个生物的神经网络层:

  • 输入层:5个传感器,1如果传感器方向有墙,或者0没有。在此处输入图像描述
  • 输出层:映射到生物角度的线性输出。

这是我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();
}

如果它可能有帮助,我已经推送到这个 repo https://github.com/kareem3d/GeneticNeuralNetwork

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

我是一名机器学习的学生,所以如果您发现任何明显的错误,请告诉我,谢谢 :)

4

2 回答 2

3

我不知道您是否可以在层配置中设置权重(我在 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()and 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--

于 2017-05-10T08:37:05.417 回答
1

It worked for me:

    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
    }
于 2019-02-02T10:35:23.863 回答