Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- double w0, w1, w2, w3, w4, w5; // wagi
- double b1, b2, b3; // biasy
- double random() // losuje 0.01 - 1.00
- {
- return ((rand()%100)+1)/100.0;
- }
- double sigmoid(double x) // funkcja aktywuj¹ca
- {
- return 1/(1+exp(-x));
- }
- double derivsigmoid(double x)
- {
- return x*(1-x);
- }
- double mse_loss(double* y, double* ypred, int n) // n - liczba probek, ypred - wartosci obliczone, y - wartosci docelowe
- {
- double result = 0;
- for(int i=0; i<n; i++)
- result+=pow((y[i]-ypred[i]), 2);
- return result/(double)n;
- }
- double feedforward(double* x) // wynik neuronu
- {
- double h0 = sigmoid(w0*x[0] + w1*x[1] + b1);
- double h1 = sigmoid(w2*x[0] + w3*x[1] + b2);
- return sigmoid(w4*h0 + w5*h1 + b3);
- }
- void train(double* x, double* y, double learn_rate, int epochs, int n, int m) // m - size, n - co ile probek
- {
- for(int i=0; i<epochs; i++)
- {
- for(int j=0; j<m; j++)
- {
- double h0 = sigmoid(w0*x[0] + w1*x[1] + b1);
- double h1 = sigmoid(w2*x[0] + w3*x[1] + b2);
- double o0 = sigmoid(w4*h0 + w5*h1 + b3);
- double deriv_mse = -2 * (y[j] - o0);
- double dW5 = h1 * derivsigmoid(o0);
- double dW4 = h0 * derivsigmoid(o0);
- double dB3 = derivsigmoid(o0);
- double dH0 = w4 * derivsigmoid(o0);
- double dH1 = w5 * derivsigmoid(o0);
- double dW0 = x[0] * derivsigmoid(h0);
- double dW1 = x[1] * derivsigmoid(h0);
- double dB1 = derivsigmoid(h0);
- double dW2 = x[0] = derivsigmoid(h1);
- double dW3 = x[1] = derivsigmoid(h1);
- double dB2 = derivsigmoid(h1);
- // aktualizacja wag
- w0 -= (deriv_mse*learn_rate*dH0*dW1);
- w1 -= (deriv_mse*learn_rate*dH0*dW2);
- b1 -= (deriv_mse*learn_rate*dH0*dB1);
- w2 -= (deriv_mse*learn_rate*dH1*dW3);
- w3 -= (deriv_mse*learn_rate*dH1*dW4);
- b2 -= (deriv_mse*learn_rate*dH1*dB2);
- w4 -= (deriv_mse*learn_rate*dW4);
- w5 -= (deriv_mse*learn_rate*dW5);
- b3 -= (deriv_mse*learn_rate*dB3);
- }
- if(i%n==0) // co ile n wypisac dane
- {
- double ypred[4];
- for(int j=0; j<4; j++)
- {
- ypred[j] = feedforward(x[j]);
- }
- double loss = mse_loss(y, ypred, 4);
- cout << "epochs: " << i << endl;
- for(int j=0; j<4; j++)
- cout << ypred[j] << endl;
- cout << loss << endl;
- //print mse, ypred
- }
- }
- }
- int main()
- {
- srand(time(NULL));
- w0 = random();
- w1 = random();
- w2 = random();
- w3 = random();
- w4 = random();
- w5 = random();
- b1 = random();
- b2 = random();
- b3 = random();
- cout << w1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement