I am using encog to do some of my university assignments and I would like to export a list of all the connections in the networks and their associated weights.
I saw the dumpWeights()
function that is part of the BasicMLNetwork
class (im using Java), but this only provides me with the weights with no information about the connections.
Does anyone know of a good way to achieve this?
Thanks In Advance Bidski
Yes, use the BasicNetwork.getWeight. You can loop over all of your layers and neurons. Just specify the two neurons that you want the weight between. Here is how it is called:
/**
* 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) {
I just added the following function to Encog's BasicNetwork class to dump the weights and structure. It will be in the next Encog release (3.4), its already on GitHub. For now, here is the code, it is a decent tutorial on how to extract the weights from 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();
}