<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;
}
}
day-12,多层感知机-矩阵计算 <C版>
最新推荐文章于 2022-03-03 16:49:47 发布