Example #1
0
  public static List getNcMLElements(String path, Document doc) {

    // XPath doesn't support default namespaces, so we add nc as a prefix for the tags within the
    // namespace!!!
    if (!path.startsWith(NS_PREFIX_ON_TAG) && !path.startsWith("/")) path = NS_PREFIX_ON_TAG + path;

    Pattern pattern = Pattern.compile("/\\w");
    Matcher matcher = pattern.matcher(path);

    StringBuilder sb = new StringBuilder();
    int currentChar = 0;
    while (matcher.find()) {

      sb.append(path.substring(currentChar, matcher.start() - currentChar + 1));
      if (!sb.toString().endsWith("/")) sb.append("/");
      sb.append(NS_PREFIX_ON_TAG);
      currentChar = matcher.start() + 1;
    }

    sb.append(path.substring(currentChar, path.length()));

    XPath xpath;
    try {

      xpath = XPath.newInstance(sb.toString());
      xpath.addNamespace(NS_PREFIX, doc.getRootElement().getNamespaceURI());
      return xpath.selectNodes(doc);

    } catch (JDOMException e) {

      e.printStackTrace();
    }

    return null;
  }
  public void trainNNet(Document nnet) throws JDOMException {
    // 1.search net map for all output layer neurodes, add to list, search for all hidden neurodes
    // append to list, search for all input neurodes append to list
    // 2.cycle through list, get EG. Cycle through list, MT adjust weights.
    java.util.concurrent.ExecutorService executor =
        java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
    java.util.List nodeList =
        XPath.newInstance("//LAYER[@LAYER_NAME = 'OUTPUT']/NEURODE")
            .selectNodes(Erudite_gui.NNetMap);
    java.util.List Hnodes =
        XPath.newInstance("//LAYER[@LAYER_NAME = 'HIDDEN']/NEURODE")
            .selectNodes(Erudite_gui.NNetMap);
    nodeList.addAll(Hnodes);

    Iterator nodeEGlist = nodeList.iterator(); // iterate through list and calculate error gradients
    do {
      Element currentNode = (Element) nodeEGlist.next();
      getErrorGradient(currentNode);
    } while (nodeEGlist.hasNext());

    Iterator nodeAWlist = nodeList.iterator(); // iterate through list and adjust weights and biases
    do {
      Element currentNode = (Element) nodeAWlist.next();
      TrainNet train = new TrainNet();
      if (Erudite_gui.jCheckBox2.isSelected()) { // auto-adapting learning rate
        train.learningRate = -1.0;
      }
      train.neurode = currentNode;
      executor.execute(train);
    } while (nodeAWlist.hasNext());

    try {
      executor.shutdown();
      executor.awaitTermination(2, TimeUnit.SECONDS);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public void clearNNet(Document NNetMap) throws JDOMException {
   java.util.concurrent.ExecutorService executor =
       java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
   java.util.List nodes = XPath.newInstance("//NEURODE").selectNodes(NNetMap);
   Iterator itNeurodes = nodes.iterator();
   do {
     Element CurrentNode = (Element) itNeurodes.next();
     ClearNode clear = new ClearNode();
     clear.neurode = CurrentNode;
     executor.execute(clear);
   } while (itNeurodes.hasNext());
   try {
     executor.shutdown();
     executor.awaitTermination(1, TimeUnit.SECONDS);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public void runNNet(Document NNetMap) throws JDOMException {
    java.util.concurrent.ExecutorService executor =
        java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
    java.util.List nodes =
        XPath.newInstance("//NEURODE[SYNAPSE/@ORG_NEURODE != 'INPUT']").selectNodes(NNetMap);
    Iterator itNeurodes = nodes.iterator();
    do {
      EvaluateNeurode evalN = new EvaluateNeurode();
      Element CurrentNode = (Element) itNeurodes.next(); // get current NEURODE				
      evalN.NNetMap = Erudite_gui.NNetMap;
      evalN.neurode = CurrentNode; // get summed input for current NEURODE
      executor.execute(evalN);
    } while (itNeurodes.hasNext());

    try {
      executor.shutdown();
      executor.awaitTermination(2, TimeUnit.SECONDS);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 private void getErrorGradient(Element node) throws JDOMException {
   // if-then-else for output/hidden/input layers, if-then for transfer functions, compute error
   // gradient and update nnet map
   String layerType = node.getParentElement().getAttributeValue("LAYER_NAME").toString();
   int xfer = Integer.parseInt(node.getParentElement().getAttributeValue("TRANSFER_FUNCTION"));
   String nid = node.getAttributeValue("N_ID").toString();
   if (layerType.equals("OUTPUT")) { // if computing output neurode error gradient
     if (xfer == 1) { // if hyperbolic tangent transfer function
       for (int i = 0; i < Erudite_gui.outputNID.length; i++) {
         if (nid.equals(Erudite_gui.outputNID[i])) {
           double error = Erudite_gui.desiredOutput[i] - Erudite_gui.output[i];
           double errorGradient =
               error * ((1 - Erudite_gui.output[i]) * (1 + Erudite_gui.output[i]));
           node.setAttribute("NNET_V4", String.valueOf(errorGradient));
         }
       }
     } else if (xfer == 2) { // if sigmoid transfer function function
       for (int i = 0; i < Erudite_gui.outputNID.length; i++) {
         if (nid.equals(Erudite_gui.outputNID[i])) {
           double error = Erudite_gui.desiredOutput[i] - Erudite_gui.output[i];
           double errorGradient = error * (Erudite_gui.output[i] * (1 - Erudite_gui.output[i]));
           node.setAttribute("NNET_V4", String.valueOf(errorGradient));
         }
       }
     }
   } else if (layerType.equals("HIDDEN")
       || layerType.equals("INPUT")) { // if computing hidden or input neurode error gradient
     if (xfer == 1) {
       double sumWouts = 0.0;
       double nodeActivity = java.lang.Double.parseDouble(node.getAttributeValue("ACTIVITY"));
       java.util.List outputs =
           XPath.newInstance("//SYNAPSE[@ORG_NEURODE = '" + nid + "']")
               .selectNodes(Erudite_gui.NNetMap);
       Iterator synapseO = outputs.iterator();
       do {
         Element currentNode = (Element) synapseO.next();
         sumWouts +=
             java.lang.Double.parseDouble(currentNode.getAttributeValue("WEIGHT"))
                 * java.lang.Double.parseDouble(
                     currentNode.getParentElement().getAttributeValue("NNET_V4"));
       } while (synapseO.hasNext());
       double errorGradient = sumWouts * ((1 - nodeActivity) * (1 + nodeActivity));
       node.setAttribute("NNET_V4", String.valueOf(errorGradient));
     } else if (xfer == 2) {
       double sumWouts = 0.0;
       double nodeActivity = java.lang.Double.parseDouble(node.getAttributeValue("ACTIVITY"));
       java.util.List outputs =
           XPath.newInstance("//SYNAPSE[@ORG_NEURODE = '" + nid + "']")
               .selectNodes(Erudite_gui.NNetMap);
       Iterator synapseO = outputs.iterator();
       do {
         Element currentNode = (Element) synapseO.next();
         sumWouts +=
             java.lang.Double.parseDouble(currentNode.getAttributeValue("WEIGHT"))
                 * java.lang.Double.parseDouble(
                     currentNode.getParentElement().getAttributeValue("NNET_V4"));
       } while (synapseO.hasNext());
       double errorGradient = sumWouts * (nodeActivity * (1 - nodeActivity));
       node.setAttribute("NNET_V4", String.valueOf(errorGradient));
     }
   }
 }