encog java导出网络权重

时间:2015-04-16 09:28:12

标签: java machine-learning artificial-intelligence neural-network encog

我正在使用encog来完成我的一些大学任务,我想导出网络中所有连接的列表及其相关权重。

我看到dumpWeights()类的BasicMLNetwork函数(即使用Java),但这只为我提供了权重,没有关于连接的信息。

有谁知道实现这个目标的好方法?

先谢谢 Bidski

1 个答案:

答案 0 :(得分:7)

是的,使用BasicNetwork.getWeight。您可以遍历所有图层和神经元。只需指定您想要权重的两个神经元。以下是它的名称:

/**
     * Get the weight between the two layers.
     * @param fromLayer The from layer.
     * @param fromNeuron The from neuron.
     * @param toNeuron The to neuron.
     * @return The weight value.
     */
    public double getWeight(final int fromLayer, 
            final int fromNeuron,
            final int toNeuron) {

我刚刚将以下函数添加到Encog的BasicNetwork类中以转储权重和结构。它将在下一个Encog版本(3.4)中,它已经在GitHub上。现在,这是代码,它是一个关于如何从Encog中提取权重的体面教程:

    public String dumpWeightsVerbose() {
    final StringBuilder result = new StringBuilder();

    for (int layer = 0; layer < this.getLayerCount() - 1; layer++) {
        int bias = 0;
        if (this.isLayerBiased(layer)) {
            bias = 1;
        }

        for (int fromIdx = 0; fromIdx < this.getLayerNeuronCount(layer)
                + bias; fromIdx++) {
            for (int toIdx = 0; toIdx < this.getLayerNeuronCount(layer + 1); toIdx++) {
                String type1 = "", type2 = "";

                if (layer == 0) {
                    type1 = "I";
                    type2 = "H" + (layer) + ",";
                } else {
                    type1 = "H" + (layer - 1) + ",";
                    if (layer == (this.getLayerCount() - 2)) {
                        type2 = "O";
                    } else {
                        type2 = "H" + (layer) + ",";
                    }
                }

                if( bias ==1 && (fromIdx ==  this.getLayerNeuronCount(layer))) {
                    type1 = "bias";
                } else {
                    type1 = type1 + fromIdx;
                }

                result.append(type1 + "-->" + type2 + toIdx
                        + " : " + this.getWeight(layer, fromIdx, toIdx)
                        + "\n");
            }
        }
    }

    return result.toString();
}