当前位置: 首页 > news >正文

Day 75:通用BP神经网络 (2. 单层实现)

代码:

package dl;import java.util.Arrays;
import java.util.Random;/*** Ann layer.*/
public class AnnLayer {/*** The number of input.*/int numInput;/*** The number of output.*/int numOutput;/*** The learning rate.*/double learningRate;/*** The mobp.*/double mobp;/*** The weight matrix.*/double[][] weights;/*** The delta weight matrix.*/double[][] deltaWeights;/*** Error on nodes.*/double[] errors;/*** The inputs.*/double[] input;/*** The outputs.*/double[] output;/*** The output after activate.*/double[] activatedOutput;/*** The inputs.*/Activator activator;/*** The inputs.*/Random random = new Random();/************************ The first constructor.** @param paraActivator*            The activator.**********************/public AnnLayer(int paraNumInput, int paraNumOutput, char paraActivator,double paraLearningRate, double paraMobp) {numInput = paraNumInput;numOutput = paraNumOutput;learningRate = paraLearningRate;mobp = paraMobp;weights = new double[numInput + 1][numOutput];deltaWeights = new double[numInput + 1][numOutput];for (int i = 0; i < numInput + 1; i++) {for (int j = 0; j < numOutput; j++) {weights[i][j] = random.nextDouble();} // Of for j} // Of for ierrors = new double[numInput];input = new double[numInput];output = new double[numOutput];activatedOutput = new double[numOutput];activator = new Activator(paraActivator);}// Of the first constructor/*********************** Set parameters for the activator.** @param paraAlpha*            Alpha. Only valid for certain types.* @param paraBeta*            Beta.* @param paraAlpha*            Alpha.*********************/public void setParameters(double paraAlpha, double paraBeta, double paraGamma) {activator.setAlpha(paraAlpha);activator.setBeta(paraBeta);activator.setGamma(paraGamma);}// Of setParameters/*********************** Forward prediction.** @param paraInput*            The input data of one instance.* @return The data at the output end.*********************/public double[] forward(double[] paraInput) {//System.out.println("Ann layer forward " + Arrays.toString(paraInput));// Copy data.for (int i = 0; i < numInput; i++) {input[i] = paraInput[i];} // Of for i// Calculate the weighted sum for each output.for (int i = 0; i < numOutput; i++) {output[i] = weights[numInput][i];for (int j = 0; j < numInput; j++) {output[i] += input[j] * weights[j][i];} // Of for jactivatedOutput[i] = activator.activate(output[i]);} // Of for ireturn activatedOutput;}// Of forward/*********************** Back propagation and change the edge weights.** @param paraTarget*            For 3-class data, it is [0, 0, 1], [0, 1, 0] or [1, 0, 0].*********************/public double[] backPropagation(double[] paraErrors) {//Step 1. Adjust the errors.for (int i = 0; i < paraErrors.length; i++) {paraErrors[i] = activator.derive(output[i], activatedOutput[i]) * paraErrors[i];}//Of for i//Step 2. Compute current errors.for (int i = 0; i < numInput; i++) {errors[i] = 0;for (int j = 0; j < numOutput; j++) {errors[i] += paraErrors[j] * weights[i][j];deltaWeights[i][j] = mobp * deltaWeights[i][j]+ learningRate * paraErrors[j] * input[i];weights[i][j] += deltaWeights[i][j];} // Of for j} // Of for ifor (int j = 0; j < numOutput; j++) {deltaWeights[numInput][j] = mobp * deltaWeights[numInput][j] + learningRate * paraErrors[j];weights[numInput][j] += deltaWeights[numInput][j];} // Of for jreturn errors;}// Of backPropagation/*********************** I am the last layer, set the errors.** @param paraTarget*            For 3-class data, it is [0, 0, 1], [0, 1, 0] or [1, 0, 0].*********************/public double[] getLastLayerErrors(double[] paraTarget) {double[] resultErrors = new double[numOutput];for (int i = 0; i < numOutput; i++) {resultErrors[i] = (paraTarget[i] - activatedOutput[i]);} // Of for ireturn resultErrors;}// Of getLastLayerErrors/*********************** Show me.*********************/public String toString() {String resultString = "";resultString += "Activator: " + activator;resultString += "\r\n weights = " + Arrays.deepToString(weights);return resultString;}// Of toString/*********************** Unit test.*********************/public static void unitTest() {AnnLayer tempLayer = new AnnLayer(2, 3, 's', 0.01, 0.1);double[] tempInput = { 1, 4 };System.out.println(tempLayer);double[] tempOutput = tempLayer.forward(tempInput);System.out.println("Forward, the output is: " + Arrays.toString(tempOutput));double[] tempError = tempLayer.backPropagation(tempOutput);System.out.println("Back propagation, the error is: " + Arrays.toString(tempError));}// Of unitTest/*********************** Test the algorithm.*********************/public static void main(String[] args) {unitTest();}// Of main
}// Of class AnnLayer

结果:

 

相关文章:

Day 75:通用BP神经网络 (2. 单层实现)

代码&#xff1a; package dl;import java.util.Arrays; import java.util.Random;/*** Ann layer.*/ public class AnnLayer {/*** The number of input.*/int numInput;/*** The number of output.*/int numOutput;/*** The learning rate.*/double learningRate;/*** The m…...

PHP序列化,反序列化

一.什么是序列化和反序列化 php类与对象 类是定义一系列属性和操作的模板&#xff0c;而对象&#xff0c;就是把属性进行实例化&#xff0c;完事交给类里面的方法&#xff0c;进行处理。 <?php class people{//定义类属性&#xff08;类似变量&#xff09;,public 代表可…...

Android google admob Timeout for show call succeed 问题解决

项目场景&#xff1a; 项目中需要接入 google admob sdk 实现广告商业化 问题描述 在接入Institial ad 时&#xff0c;onAdLoaded 成功回调&#xff0c;但是onAdFailedToShowFullScreenContent 也回调了错误信息 “Timeout for show call succeed.” InterstitialAd.load(act…...

EFLFK——ELK日志分析系统+kafka+filebeat架构

环境准备 node1节点192.168.40.16elasticsearch2c/4Gnode2节点192.168.40.17elasticsearch2c/4GApache节点192.168.40.170logstash/Apache/kibana2c/4Gfilebeat节点192.168.40.20filebeat2c/4G https://blog.csdn.net/m0_57554344/article/details/132059066?spm1001.2014.30…...

C# MVC controller 上传附件及下载附件(笔记)

描述&#xff1a;Microsoft.AspNetCore.Http.IFormFileCollection 实现附件快速上传功能代码。 上传附件代码 [Route("myUploadFile")][HttpPost]public ActionResult MyUploadFile([FromForm] upLoadFile rfile){string newFileName Guid.NewGuid().ToString(&quo…...

安装element-plus报错:Conflicting peer dependency: eslint-plugin-vue@7.20.0

VSCode安装element-plus报错&#xff1a; D:\My Programs\app_demo>npm i element-plus npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: vue/eslint-config-standard6.1.0 npm ERR! Found: eslint-plugin-vue8.7.1 npm E…...

【操作系统】进程和线程对照解释

进程&#xff08;Process&#xff09;和线程&#xff08;Thread&#xff09;都是操作系统中用于执行任务的基本单位&#xff0c;但它们有着不同的特点和使用方式。 进程&#xff08;Process&#xff09;&#xff1a; 进程是正在运行的程序的实例。一个程序在运行时会被操作系统…...

4用opencv玩转图像2

opencv绘制文字和几何图形 黑色底图 显示是一张黑色图片 使用opencv画圆形 #画一个圆 cv2.circle(imgblack_img,center(400,400),radius100,color(0,0,255),thickness10) 画实心圆 只需要把thickness-1。 cv2.circle(imgblack_img,center(500,600),radius50,color(0,0,255),t…...

Swagger的使用

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言1 .介绍2. 使用步骤完整的WebMvcConfiguration.java 3. 常用注解 前言 1 .介绍 Swagger 是一个规范和完整的框架&#xff0c;用于生成、描述、调用和可视化 RE…...

python高阶技巧

目录 设计模式 单例模式 具体用法 工厂模式 优点 闭包 案例 修改闭包外部变量 闭包优缺点 装饰器 装饰器原理 装饰器写法 递归 递归的调用过程 递归的优缺点 用递归计算阶乘 设计模式 含义&#xff1a;设计模式是一种编程套路&#xff0c;通过这种编程套路可…...

Linux和Windows安装MySQL服务

Linux和Windows安装MySQL服务 1 Linux安装MySQL服务1.1 安装1.2 服务端启动1.3 客户端连接 2 Windows安装MySQL服务2.1官网下载安装包(windows)并解压2.2 配置系统环境变量2.3 服务端启动(管理员DOS)2.4 客户端连接(管理员DOS) 3 修改密码4 Windows问题 1 Linux安装MySQL服务 …...

Vue3 第四节 自定义hook函数以及组合式API

1.自定义hook函数 2.toRef和toRefs 3.shallowRef和shallowReactive 4.readonly和shallowReadonly 5.toRaw和markRaw 6.customref 一.自定义hook函数 ① 本质是一个函数&#xff0c;把setup函数中使用的Composition API 进行了封装,类似于vue2.x中的mixin 自定义hook函数…...

门面模式(C++)

定义 为子系统中的一组接口提供一个一致(稳定) 的界面&#xff0c;Facade模式定义了一个高层接口&#xff0c;这个接口使得这一子系统更加容易使用(复用)。 应用场景 上述A方案的问题在于组件的客户和组件中各种复杂的子系统有了过多的耦合&#xff0c;随着外部客户程序和各子…...

ASP.NET Core SignalR

ASP.NET Core SignalR是一个开发实时网络应用程序的框架&#xff0c;它使用WebSocket作为传输协议&#xff0c;并提供了一种简单和高效的方式来实现实时双向通信。 SignalR使用了一种称为"Hub"的概念来管理连接和消息的传递。开发者可以编写自己的Hub类&#xff0c;…...

auto-changelog的简单使用

auto-changelog的简单使用 自动化生成Git提交记录&#xff0c;CHANGELOG.md文件 github&#xff1a;https://github.com/cookpete/auto-changelog 安装 npm install -g auto-changelog配置脚本 package.json文件下 "scripts": {"changelog": "aut…...

map 比较(两个map的key,value 是否一样)

1. 用equals 比较 public static void main(String[] args) {List<Map<String,Object>> list new ArrayList<>();Map<String,Object> map1 new HashMap<>();map1.put("name","郭");map1.put("objId","1&quo…...

LayUI之入门

目录 1.什么是layui 2.layui、easyui与bootstrap的对比 有趣的对比方式&#xff0c;嘿嘿嘿.... easyuijqueryhtml4&#xff08;用来做后台的管理界面&#xff09; 半老徐娘 bootstrapjqueryhtml5 美女 拜金 layui 清纯少女 2.1 layui和bootstrap对比&#xff08;这两个都属…...

【Linux】Linux下git的使用

文章目录 一、什么是git二、git发展史三、Gitee仓库的创建1.新建仓库2.复制仓库链接3.在命令行克隆仓库3.1仓库里的.gitignore是什么3.2仓库里的git是什么 三、git的基本使用1.将克隆仓库的新增文件添加到暂存区(本地仓库)2.将暂存区的文件添加到.git仓库中3.将.git仓库中的变化…...

QT读写配置文件

文章目录 一、概述二、使用步骤1.引入头文件2.头文件的public中定义配置文件对象3.初始化 一、概述 Qt中常见的配置文件为&#xff08;.ini&#xff09;文件&#xff0c;其中ini是Initialization File的缩写&#xff0c;即初始化文件。 配置文件的格式如下所示&#xff1a; 模…...

【计算机网络】12、frp 内网穿透

文章目录 一、服务端设置二、客户端设置 frp &#xff1a;A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet。是一个专注于内网穿透的高性能的反向代理应用&#xff0c;支持 TCP、UDP、HTTP、HTTPS 等多种协议&#xff0c;且…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

Spring Boot 实现流式响应(兼容 2.7.x)

在实际开发中&#xff0c;我们可能会遇到一些流式数据处理的场景&#xff0c;比如接收来自上游接口的 Server-Sent Events&#xff08;SSE&#xff09; 或 流式 JSON 内容&#xff0c;并将其原样中转给前端页面或客户端。这种情况下&#xff0c;传统的 RestTemplate 缓存机制会…...

java 实现excel文件转pdf | 无水印 | 无限制

文章目录 目录 文章目录 前言 1.项目远程仓库配置 2.pom文件引入相关依赖 3.代码破解 二、Excel转PDF 1.代码实现 2.Aspose.License.xml 授权文件 总结 前言 java处理excel转pdf一直没找到什么好用的免费jar包工具,自己手写的难度,恐怕高级程序员花费一年的事件,也…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践

6月5日&#xff0c;2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席&#xff0c;并作《智能体在安全领域的应用实践》主题演讲&#xff0c;分享了在智能体在安全领域的突破性实践。他指出&#xff0c;百度通过将安全能力…...

C# SqlSugar:依赖注入与仓储模式实践

C# SqlSugar&#xff1a;依赖注入与仓储模式实践 在 C# 的应用开发中&#xff0c;数据库操作是必不可少的环节。为了让数据访问层更加简洁、高效且易于维护&#xff0c;许多开发者会选择成熟的 ORM&#xff08;对象关系映射&#xff09;框架&#xff0c;SqlSugar 就是其中备受…...

让AI看见世界:MCP协议与服务器的工作原理

让AI看见世界&#xff1a;MCP协议与服务器的工作原理 MCP&#xff08;Model Context Protocol&#xff09;是一种创新的通信协议&#xff0c;旨在让大型语言模型能够安全、高效地与外部资源进行交互。在AI技术快速发展的今天&#xff0c;MCP正成为连接AI与现实世界的重要桥梁。…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

Java编程之桥接模式

定义 桥接模式&#xff08;Bridge Pattern&#xff09;属于结构型设计模式&#xff0c;它的核心意图是将抽象部分与实现部分分离&#xff0c;使它们可以独立地变化。这种模式通过组合关系来替代继承关系&#xff0c;从而降低了抽象和实现这两个可变维度之间的耦合度。 用例子…...