day-12,多层感知机-矩阵计算 <C版>

<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;
	}
}



                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用python bs4库从下面这段内容“<div class="maingrid" data-v-0f3d927f=""> <!-- --> <!-- --> <!-- --> <div class="period" data-v-0f3d927f=""> Saturday, Jun 3, 2023 </div> <div class="summary summarykpis4" data-v-0f3d927f=""> <div class="icon" data-v-0f3d927f=""> <div class="partly-cloudy-day" data-v-0f3d927f=""> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Max </div> <div> 28℃ </div> <div class="normals"> <div> 22 </div> <div> 27 </div> <div> 36 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Min </div> <div> 13℃ </div> <div class="normals"> <div> 9.9 </div> <div> 15 </div> <div> 19 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Rain </div> <div> 0mm </div> <div class="normals"> <div> 0 </div> <div> 4.3 </div> <div> 23 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Precip % </div> <div> 0% </div> <!-- --> </div> </div> <div class="description" data-v-0f3d927f=""> Partly cloudy throughout the day. </div> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> </div>”提取出6月3至6月5日天气信息
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RtZero

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值