void RuleBlock::activate() { FL_DBG("==================="); FL_DBG("ACTIVATING RULEBLOCK " << getName()); if (not getActivation()) { setActivation(new General); } FL_DBG("Activation: " << getActivation()->className() << " " << getActivation()->parameters()); getActivation()->activate(this); }
// Train network on dataset `data` with learning rate `learnRate` for `epochs` epochs void Network::train(Dataset &data, double learnRate, int epochs) { // Loop over epochs for (int iter=0; iter < epochs; iter++) { // Loop over samples for (int i=0; i < data.size(); i++) { std::vector<double> sampleFeatures; std::vector<bool> sampleLabels; std::vector<double> outputDelta(numOutput,0); std::vector<double> hiddenDelta(numHidden,0); // Read sample and calculate activations data.sample(i, sampleFeatures, sampleLabels); getActivation(sampleFeatures); // Output layer delta for (int j=0; j < numOutput; j++) { outputDelta[j] = SIGDERIV(input[2][j]) * (sampleLabels[j] - activation[2][j]); } // Hidden layer delta for (int j=0; j < numHidden; j++) { for (int k=0; k < numOutput; k++) { hiddenDelta[j] += outputWeights[k][j+1] * outputDelta[k]; } hiddenDelta[j] *= SIGDERIV(input[1][j]); } // Update weights from hidden layer to output layer for (int j=0; j < numOutput; j++) { for (int k=0; k < numHidden; k++) { outputWeights[j][k+1] = outputWeights[j][k+1] + learnRate * activation[1][k] * outputDelta[j]; } outputWeights[j][0] = outputWeights[j][0] + learnRate * -1 * outputDelta[j]; // Bias } // Update weights from input layer to hidden layer for (int j=0; j < numHidden; j++) { for (int k=0; k < numInputs; k++) { inputWeights[j][k+1] = inputWeights[j][k+1] + learnRate * activation[0][k] * hiddenDelta[j]; } inputWeights[j][0] = inputWeights[j][0] + learnRate * -1 * hiddenDelta[j]; // Bias } // Update weight vector weights[0] = inputWeights; weights[1] = outputWeights; } } }
// Predict on `data` with current network weights void Network::test(Dataset &data) { // Loop over samples for (int i=0; i < data.size(); i++) { std::vector<double> sampleFeatures; std::vector<bool> sampleLabels; // Load current sample and calculate activation data.sample(i, sampleFeatures, sampleLabels); getActivation(sampleFeatures); // Loop over each class for (int j=0; j < numOutput; j++) { // Threshold activations to predict data.classify(i, j, (activation[2][j] >= .5 ? true : false)); } } }