No.6 soft computing 2

 #include <iostream>

#include <vector>


class ADALINE {

public:

    ADALINE(int n) : w(n + 1, 0.0) {}


    void train(const std::vector<std::vector<double>>& data, const std::vector<int>& labels, double eta, int epochs) {

        for (int e = 0; e < epochs; ++e) {

            for (size_t i = 0; i < data.size(); ++i) {

                double o = predict(data[i]), err = labels[i] - o;

                for (size_t j = 0; j < data[i].size(); ++j) w[j] += eta * err * data[i][j];

                w.back() += eta * err; // Bias update

            }

        }

    }


    double predict(const std::vector<double>& x) {

        double sum = w.back();

        for (size_t i = 0; i < x.size(); ++i) sum += w[i] * x[i];

        return sum;

    }


private:

    std::vector<double> w; // Weights and bias

};


int main() {

    ADALINE adaline(2);

    std::vector<std::vector<double>> data = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

    std::vector<int> labels = {0, 0, 0, 1}; // AND function


    adaline.train(data, labels, 0.1, 100);


    for (const auto& x : data) {

        std::cout << adaline.predict(x) << '\n';

    }


    return 0;

}

Post a Comment

0 Comments