我在使用encog完成大学作业时,希望能够导出一份网络中所有连接及其相关权重的列表。
我看到了dumpWeights()
函数,它是BasicMLNetwork
类的一部分(我使用的是Java),但它只能提供权重信息,而没有关于连接的信息。
有谁知道有什么好方法可以实现这一点吗?
提前感谢@隐藏人名
回答:
是的,可以使用BasicNetwork.getWeight。你可以遍历所有的层和神经元。只需指定你想要获取权重的两个神经元。调用方法如下:
/** * 获取两层之间的权重。 * @param fromLayer 起始层。 * @param fromNeuron 起始神经元。 * @param toNeuron 目标神经元。 * @return 权重值。 */ 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();}