728x90
반응형
#include <stdio.h>

void forward_Y(double* X, double* W, double* B, double* Y, int M, int N);
double calculate_MSE(double* Y, double* YT, int N);
void backward_YE(double* YE, double* Y, double* YT, int N);
void backward_WE(double* WE, double* X, double* YE, int M, int N);
void backward_BE(double* BE, double* YE, int N);
void learning_W(double* W, double lr, double* WE, int M, int N);
void learning_B(double* B, double lr, double* BE, int N);
void print(const char* s, double* Y, int N);
void print(const char* s, double* W, int M, int N);

int main(void)
{
	dnn_test();

	return 0;
}

void dnn_test()
{
	void forward_Y(double* X, double* W, double* B, double* Y, int M, int N);
	double calculate_MSE(double* Y, double* YT, int N);
	void backward_YE(double* YE, double* Y, double* YT, int N);
	void backward_WE(double* WE, double* X, double* YE, int M, int N);
	void backward_BE(double* BE, double* YE, int N);
	void learning_W(double* W, double lr, double* WE, int M, int N);
	void learning_B(double* B, double lr, double* BE, int N);
	void print(const char* s, double* Y, int N);
	void print(const char* s, double* W, int M, int N);
}

void forward_Y(double* X, double* W, double* B, double* Y, int M, int N)
{
	for (int n = 0; n < N; n++)
	{
		Y[n] = 0;
	}
	for (int m = 0; m < M; m++)
	{
		for (int n = 0; n < N; n++)
		{
			Y[n] += X[m] * W[m * N + n];
		}
	}
	for (int n = 0; n < N; n++)
	{
		Y[n] += B[n];
	}
}

double calculate_MSE(double* Y, double* YT, int N)
{
	double E = 0;
	for (int n = 0; n < N; n++)
	{
		E += (Y[n] - YT[n]) * (Y[n] - YT[n]) / 2;
		return E;
	}
}

void backward_YE(double* YE, double* Y, double* YT, int N)
{
	for (int n = 0; n < N; n++)
	{
		YE[n] = Y[n] - YT[n];
	}
}

void backward_WE(double* WE, double* X, double* YE, int M, int N)
{
	for (int m = 0; m < M; m++)
	{
		for (int n = 0; n < N; n++)
		{
			WE[m * n + n] = X[m] * YE[n];
		}
	}
}

void backward_BE(double* BE, double* YE, int N)
{
	for (int n = 0; n < N; n++)
	{
		BE[n] = YE[n];
	}
}

void learning_W(double* W, double lr, double* WE, int M, int N)
{
	for (int m = 0; m < M; m++)
	{
		for (int n = 0; n < N; n++)
		{
			W[m * N + n] -= lr * WE[m * N + n];
		}
	}
}

void learning_B(double* B, double lr, double* BE, int N)
{
	for (int n = 0; n < N; n++)
	{
		B[n] -= lr * BE[n];
	}
}

void print(const char* s, double* Y, int N)
{
	printf("%s [", s);
	for (int n = 0; n < N - 1; n++)
	{
		printf("%.3f", Y[n]);
	}
	printf("%.3f]\n", Y[N - 1]);
}

void print(const char* s, double* W, int M, int N)
{
	printf("%s [\n", s);
	for (int m = 0; m < M; m++)
	{
		printf("[%s", s);
		for (int n = 0; n < N - 1; n++)
		{
			printf("%.3f ", W[m * N + n]);
		}
		printf("%.3f]\n", W[m * N + N - 1]);
	}
	printf("]\n");
}

 

728x90
반응형

+ Recent posts