day-12,多層感知機-矩陣計算

<pre name="code" class="csharp">#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
void f(float *dst, float src[], int h, int w);
void df(float *dst, float src[], int h, int w);
void MatX(float *dst, float src1[], float src2[], int h1, int w1, int h2, int w2);
void MatAdd(float *dst, float src1[], float src2[], int h1, int w1);
void MatSub(float *dst, float src1[], float src2[], int h1, int w1);
void MatXp(float *dst, float src1[], float src2[], int h1, int w1);
void MatCf(float *dst, float src1[], float Mu, int h1, int w1);

int main()
{
	//設置參數
	const int n1 = 2, n2 = 2, n3 = 1;
	float x[n1];//自變量
	float y[1];//因變量
	float Py[1];//網絡輸出
	//初始化網絡
	float P1[n1], P2[n2]; 				//輸出矩陣
	float W1[n1*n2], W2[n2 * n3];		//權值矩陣
	float S1[n2], S2[n3];					//輸入矩陣
	float Sigma1[n2], Sigma2[n3];				//權值調整矩陣
	float Mu = -1;
	//初始化數據
	for (int i = 0; i < n1*n2; i++)
	{
		W1[i] = rand() / (RAND_MAX + 1.0);
	}
	for (int i = 0; i < n1; i++)
	{
		x[i] = 1;
		P1[i] = 0;
	}
	for (int i = 0; i < n2; i++)
	{
		P2[i] = 0;
	}
	for (int i = 0; i < n2*n3; i++)
	{
		W2[i] = rand() / (RAND_MAX + 1.0);
	}
	y[0] = 1 / (x[0] + x[1]);
	//顯示
	printf("輸入:x0=%f,x1=%f\n輸出:y=%f\n", x[0], x[1], y[0]);
	printf("Mu;%f", Mu);
	//------------
	int num = 1;
	float Sigma2Tmp1[1 * n3], Sigma2Tmp2[1 * n3];
	float Sigma1Tmp1[1 * n3], Sigma1Tmp2[1 * n3];
	float W1Tmp1[n1*n2], W2Tmp1[n2*n3];
	float W1Tmp2[n1*n2], W2Tmp2[n2*n3];
	float End=0.010000;
	while (1)
	{
		//輸入神經
		f(P1, x, 1, n1);
		//前向神經
		MatX(S1, W1, P1, n2, n1, n2, 1);
		f(P2, S1, 1, n2);
		MatX(S2, W2, P2, n3, n2, n3, 1);
		f(Py, S2, 1, n3);
		//顯示
		printf("\n第%d輪,Py=%f,y=%f,差值=%f,終止差值=%f", num, Py[0], y[0], abs(Py[0] - y[0]),End);
		//結束條件
		if ((End > abs(Py[0] - y[0]))||(num>100)){ break; }
		//輸出神經
		MatSub(Sigma2Tmp1, Py, y, 1, n3);
		df(Sigma2Tmp2, S2, 1, n3);
		MatXp(Sigma2, Sigma2Tmp1, Sigma2Tmp2, 1, n3);
		MatX(W2Tmp1, P2, Sigma2, n2, 1, 1, n3);
		MatCf(W2Tmp2, W2Tmp1, Mu, n2, n3);
		//反向傳遞
		MatX(Sigma1Tmp1, W2, Sigma2, n2, n3, 1, n3);
		df(Sigma1Tmp2, S1, 1, n2);
		MatXp(Sigma1, Sigma1Tmp1, Sigma1Tmp2, 1, n2);
		MatX(W1Tmp1, P1, Sigma1, n1, 1, 1, n2);
		MatCf(W1Tmp2, W1Tmp1, Mu, n1, n2);

		MatAdd(W2, W2, W2Tmp2, n2, n3);
		MatAdd(W1, W1, W1Tmp2, n1, n2);

		num = num + 1;
	}
	cout << endl;
	cin.get();
	return 0;
}

void f(float *dst, float src[], int h, int w)
{
	for (int i = 0; i < h*w; i++)
	{
		dst[i] = 1 / (1 + exp(-src[i]));
	}
}

void df(float *dst, float src[], int h, int w)
{
	for (int i = 0; i < h*w; i++)
	{
		dst[i] = 1 / (1 + exp(-src[i]));
	}
}

void MatX(float *dst, float src1[], float src2[], int h1, int w1, int h2, int w2)
{
	for (int h = 0; h < h1; h++)
	{
		for (int w = 0; w < w2; w++)
		{
			dst[h*w2 + w] = 0;
			for (int k = 0; k < w1; k++)
			{
				dst[h*w2 + w] =
					dst[h*w2 + w] +
					src1[h*w1 + k] * src2[k*w2 + w];
			}
		}
	}
}

void MatAdd(float *dst, float src1[], float src2[], int h1, int w1)
{
	for (int i = 0; i< h1*w1; i++)
	{
		dst[i] = src1[i] + src2[i];
	}
}

void MatSub(float *dst, float src1[], float src2[], int h1, int w1)
{
	for (int i = 0; i< h1*w1; i++)
	{
		dst[i] = src1[i] - src2[i];
	}
}

void MatXp(float *dst, float src1[], float src2[], int h1, int w1)
{
	for (int i = 0; i< h1*w1; i++)
	{
		dst[i] = src1[i] * src2[i];
	}
}

void MatCf(float *dst, float src1[], float Mu, int h1, int w1)
{
	for (int i = 0; i< h1*w1; i++)
	{
		dst[i] = src1[i] * Mu;
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章