encog java 导出网络权重

encog java exporting network weights

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

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

有人知道实现此目的的好方法吗?

提前致谢 比德斯基

是的,使用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 class 中以转储权重和结构。它将出现在下一个 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();
}