|
| 1 | +#include "fnn.h" |
| 2 | +#include <Arduino.h> |
| 3 | + |
| 4 | +// Konstruktor |
| 5 | +FNN::FNN(int inputSize, float bias, std::function<float(float)> activation) |
| 6 | + : weights(2, std::vector<float>(inputSize, 0.0)), biases(2, bias), activationFunction(activation) { |
| 7 | + if (!activationFunction) { |
| 8 | + activationFunction = sigmoid; // Default fungsi aktivasi adalah sigmoid |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +// Set bobot |
| 13 | +void FNN::setWeights(const std::vector<float>& newWeights) { |
| 14 | + if (newWeights.size() == weights[0].size()) { |
| 15 | + weights[0] = newWeights; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// Set bias |
| 20 | +void FNN::setBiases(const std::vector<float>& newBiases) { |
| 21 | + if (newBiases.size() == biases.size()) { |
| 22 | + biases = newBiases; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Set fungsi aktivasi |
| 27 | +void FNN::setActivationFunction(std::function<float(float)> activation) { |
| 28 | + activationFunction = activation; |
| 29 | +} |
| 30 | + |
| 31 | +// Set fuzzy rules |
| 32 | +void FNN::setFuzzyRules(const std::map<std::string, float>& rules) { |
| 33 | + fuzzyRules = rules; |
| 34 | +} |
| 35 | + |
| 36 | +// Fungsi aktivasi: Sigmoid |
| 37 | +float FNN::sigmoid(float x) { |
| 38 | + return 1.0 / (1.0 + exp(-x)); |
| 39 | +} |
| 40 | + |
| 41 | +// Fungsi aktivasi: Tanh |
| 42 | +float FNN::tanh(float x) { |
| 43 | + return std::tanh(x); |
| 44 | +} |
| 45 | + |
| 46 | +// Fungsi aktivasi: Leaky ReLU |
| 47 | +std::function<float(float)> FNN::leakyRelu(float alpha) { |
| 48 | + return [alpha](float x) { return (x > 0) ? x : alpha * x; }; |
| 49 | +} |
| 50 | + |
| 51 | +// Fungsi aktivasi: ELU |
| 52 | +std::function<float(float)> FNN::elu(float alpha) { |
| 53 | + return [alpha](float x) { return (x > 0) ? x : alpha * (exp(x) - 1); }; |
| 54 | +} |
| 55 | + |
| 56 | +// Fungsi aktivasi: Softplus |
| 57 | +float FNN::softplus(float x) { |
| 58 | + return log(1 + exp(x)); |
| 59 | +} |
| 60 | + |
| 61 | +// Defuzzifikasi |
| 62 | +std::string FNN::defuzzify(float fuzzyOutput) { |
| 63 | + for (const auto& rule : fuzzyRules) { |
| 64 | + if (fuzzyOutput <= rule.second) { |
| 65 | + return rule.first; |
| 66 | + } |
| 67 | + } |
| 68 | + return "Undefined"; |
| 69 | +} |
| 70 | + |
| 71 | +// Compute Loss |
| 72 | +float FNN::computeLoss(const std::vector<float>& predicted, const std::vector<float>& expected) { |
| 73 | + float loss = 0.0f; |
| 74 | + for (size_t i = 0; i < predicted.size(); ++i) { |
| 75 | + loss += pow(predicted[i] - expected[i], 2); |
| 76 | + } |
| 77 | + return loss / predicted.size(); |
| 78 | +} |
| 79 | + |
| 80 | +// Train |
| 81 | +void FNN::train(const std::vector<std::vector<float>>& inputs, const std::vector<std::string>& targets, int epochs, float learningRate) { |
| 82 | + for (int epoch = 0; epoch < epochs; ++epoch) { |
| 83 | + for (size_t i = 0; i < inputs.size(); ++i) { |
| 84 | + float hiddenSum = biases[0]; |
| 85 | + for (size_t j = 0; j < weights[0].size(); ++j) { |
| 86 | + hiddenSum += inputs[i][j] * weights[0][j]; |
| 87 | + } |
| 88 | + float hiddenOutput = activationFunction(hiddenSum); |
| 89 | + |
| 90 | + float outputSum = hiddenOutput * weights[1][0] + biases[1]; |
| 91 | + float output = activationFunction(outputSum); |
| 92 | + |
| 93 | + float outputError = fuzzyRules[targets[i]] - output; |
| 94 | + weights[1][0] += learningRate * outputError * hiddenOutput; |
| 95 | + biases[1] += learningRate * outputError; |
| 96 | + |
| 97 | + float hiddenError = outputError * weights[1][0]; |
| 98 | + for (size_t j = 0; j < weights[0].size(); ++j) { |
| 99 | + weights[0][j] += learningRate * hiddenError * inputs[i][j]; |
| 100 | + } |
| 101 | + biases[0] += learningRate * hiddenError; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Predict |
| 107 | +std::string FNN::predictFNN(const std::vector<float>& inputs) { |
| 108 | + float hiddenSum = biases[0]; |
| 109 | + for (size_t j = 0; j < weights[0].size(); ++j) { |
| 110 | + hiddenSum += inputs[j] * weights[0][j]; |
| 111 | + } |
| 112 | + float hiddenOutput = activationFunction(hiddenSum); |
| 113 | + |
| 114 | + float outputSum = hiddenOutput * weights[1][0] + biases[1]; |
| 115 | + float output = activationFunction(outputSum); |
| 116 | + |
| 117 | + return defuzzify(output); |
| 118 | +} |
| 119 | +// Evaluasi Akurasi |
| 120 | +float FNN::evaluateAccuracy(const std::vector<std::vector<float>>& testInputs, const std::vector<std::string>& expectedOutputs) { |
| 121 | + int correctPredictions = 0; |
| 122 | + |
| 123 | + for (size_t i = 0; i < testInputs.size(); ++i) { |
| 124 | + std::string predictedOutput = predictFNN(testInputs[i]); |
| 125 | + if (predictedOutput == expectedOutputs[i]) { |
| 126 | + correctPredictions++; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + float accuracy = (float)correctPredictions / testInputs.size(); |
| 131 | + return accuracy * 100.0f; // Hasil dalam persen |
| 132 | +} |
| 133 | + |
| 134 | +// Evaluasi Presisi |
| 135 | +float FNN::evaluatePrecision(const std::vector<std::vector<float>>& testInputs, const std::vector<std::string>& expectedOutputs) { |
| 136 | + int truePositives = 0; |
| 137 | + int falsePositives = 0; |
| 138 | + |
| 139 | + for (size_t i = 0; i < testInputs.size(); ++i) { |
| 140 | + std::string predictedOutput = predictFNN(testInputs[i]); |
| 141 | + |
| 142 | + if (predictedOutput == expectedOutputs[i]) { |
| 143 | + truePositives++; |
| 144 | + } else if (fuzzyRules.find(predictedOutput) != fuzzyRules.end()) { |
| 145 | + falsePositives++; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (truePositives + falsePositives == 0) { |
| 150 | + return 0.0f; // Hindari pembagian dengan nol |
| 151 | + } |
| 152 | + |
| 153 | + float precision = (float)truePositives / (truePositives + falsePositives); |
| 154 | + return precision * 100.0f; // Hasil dalam persen |
| 155 | +} |
0 commit comments