Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- #include <windows.h>
- using namespace std;
- float* NN(float *a, float W, float b) {
- float * nowa = new float[100];
- for (int i = 0; i<100; i++) {
- nowa[i] = W*a[i] + b;
- }
- return nowa;
- }
- float MSE(float *a, float *b) {
- float wynik = 0.0;
- for (int i = 0; i<100; i++) {
- wynik += (a[i] - b[i])*(a[i] - b[i]);
- }
- return wynik / 100.0;
- }
- void update(float *x, float *y, float &w, float &b)
- {
- float E = 0.01;
- //dW = (suma(Wx[i]+b-y[i])*x[i])/d
- float dw=0, db=0;
- for (int i = 0; i < 100; i++)
- {
- dw = dw + (x[i] * (x[i] * w + b - y[i]));
- db += x[i] * w + b - y[i];
- }
- w = w - ((E*dw)/100.0);
- b = b - ((E*db)/100.0);
- }
- int main()
- {
- srand(time(NULL));
- //y=2x+0,5
- float x[100];
- float y[100];
- float y2[100];
- float y3[100];
- float N;
- for (int i = 0; i<100; i++) {
- x[i] = (float)rand() / RAND_MAX * 2;
- y[i] = 2 * x[i] + 0.5;
- N = (rand() % 21 - 10) / 100.0;
- cout << N << endl;
- //Sleep(3000);
- y2[i] = (-3) * x[i] + 4 + N;
- y3[i] = x[i] * x[i];
- }
- float w, b;
- w = (float)rand() / RAND_MAX * 6 - 3;
- b = (float)rand() / RAND_MAX * 6 - 3;
- /*float * Z = NN(x, w, b);
- float mse = MSE(y, Z);
- cout << mse << endl;
- Z = NN(x, 2, 0.5);
- mse = MSE(y, Z);
- cout << mse;*/
- for (int i = 0; i < 10000; i++)
- {
- float * Z = NN(x, w, b);
- float mse = MSE(y3, Z);
- cout << "MSE: " << mse << endl;
- cout << "W: " << w << endl;
- cout << "b: " << b << endl;
- float memory = b;
- update(x, y3, w, b);
- if (b == memory)
- break;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement