<span style="font-size:18px;">#include "stdio.h"
#include<iostream>
using namespace std;
#include "stdio.h"
int main(void)
{
float matrix[4][2] = { { 1, 4 }, { 2, 5 }, { 5, 1 }, { 4, 2 } };
float result[4] = { 19, 26, 19, 20 };
float theta[2] = { 2, 5 };
float learning_rate = 0.01;
float loss = 1000.0;
for (int i = 0; i<100 && loss>0.0001; ++i)
{
float error_sum[2] = { 0.0, 0.0 };
for (int j = 0; j<4; ++j)
{
float h = 0.0;
for (int k = 0; k<2; ++k)
{
h += matrix[j][k] * theta[k];
}
for (int k = 0; k < 2; ++k)
{
error_sum[k] += (result[j] - h)*matrix[j][k];
}
}
for (int k = 0; k<2; ++k)
{
theta[k] += learning_rate*error_sum[k];
}
printf("*************************************\n");
printf("theta now: %f,%f\n", theta[0], theta[1]);
loss = 0.0;
for (int j = 0; j<4; ++j)
{
float sum = 0.0;
for (int k = 0; k<2; ++k)
{
sum += matrix[j][k] * theta[k];
}
loss += (sum - result[j])*(sum - result[j]);
}
printf("loss now: %f\n", loss);
}
system("pause");
// return 0;
}</span>