当前位置: 首页 > 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;且…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望

文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例&#xff1a;使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例&#xff1a;使用OpenAI GPT-3进…...

React19源码系列之 事件插件系统

事件类别 事件类型 定义 文档 Event Event 接口表示在 EventTarget 上出现的事件。 Event - Web API | MDN UIEvent UIEvent 接口表示简单的用户界面事件。 UIEvent - Web API | MDN KeyboardEvent KeyboardEvent 对象描述了用户与键盘的交互。 KeyboardEvent - Web…...

ios苹果系统,js 滑动屏幕、锚定无效

现象&#xff1a;window.addEventListener监听touch无效&#xff0c;划不动屏幕&#xff0c;但是代码逻辑都有执行到。 scrollIntoView也无效。 原因&#xff1a;这是因为 iOS 的触摸事件处理机制和 touch-action: none 的设置有关。ios有太多得交互动作&#xff0c;从而会影响…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

Yolov8 目标检测蒸馏学习记录

yolov8系列模型蒸馏基本流程&#xff0c;代码下载&#xff1a;这里本人提交了一个demo:djdll/Yolov8_Distillation: Yolov8轻量化_蒸馏代码实现 在轻量化模型设计中&#xff0c;**知识蒸馏&#xff08;Knowledge Distillation&#xff09;**被广泛应用&#xff0c;作为提升模型…...

Java毕业设计:WML信息查询与后端信息发布系统开发

JAVAWML信息查询与后端信息发布系统实现 一、系统概述 本系统基于Java和WML(无线标记语言)技术开发&#xff0c;实现了移动设备上的信息查询与后端信息发布功能。系统采用B/S架构&#xff0c;服务器端使用Java Servlet处理请求&#xff0c;数据库采用MySQL存储信息&#xff0…...

【JVM面试篇】高频八股汇总——类加载和类加载器

目录 1. 讲一下类加载过程&#xff1f; 2. Java创建对象的过程&#xff1f; 3. 对象的生命周期&#xff1f; 4. 类加载器有哪些&#xff1f; 5. 双亲委派模型的作用&#xff08;好处&#xff09;&#xff1f; 6. 讲一下类的加载和双亲委派原则&#xff1f; 7. 双亲委派模…...

逻辑回归暴力训练预测金融欺诈

简述 「使用逻辑回归暴力预测金融欺诈&#xff0c;并不断增加特征维度持续测试」的做法&#xff0c;体现了一种逐步建模与迭代验证的实验思路&#xff0c;在金融欺诈检测中非常有价值&#xff0c;本文作为一篇回顾性记录了早年间公司给某行做反欺诈预测用到的技术和思路。百度…...

stm32wle5 lpuart DMA数据不接收

配置波特率9600时&#xff0c;需要使用外部低速晶振...