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

news2024/10/6 14:29:30

代码:

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 i

        errors = 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 j

            activatedOutput[i] = activator.activate(output[i]);
        } // Of for i

        return 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 i

        for (int j = 0; j < numOutput; j++) {
            deltaWeights[numInput][j] = mobp * deltaWeights[numInput][j] + learningRate * paraErrors[j];
            weights[numInput][j] += deltaWeights[numInput][j];
        } // Of for j

        return 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 i

        return 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

结果:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/846869.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

web前端之JS

文章目录 介绍一、JS引入到文件1.1 嵌入到HTML文件中1.2 引入本地独立JS文件1.3 引入网络来源文件 二、JS的注释三、JS输出方式四、JS数据类型4.1 判断数据类型 typeof4.2 charAt返回指定位置的字符4.3 concat连接两个字符串4.4 substring从原字符串提取字符串并返回4.4 substr…

java动态生成excel并且需要合并单元格

java动态生成excel并且需要合并单元格 先上图看一下预期效果 集成poi <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.0.0</version> </dependency> <dependency><…

实践分享:小程序事件系统设计

微信小程序官方文档中解释说&#xff1a;事件是用于子组件向父组件传递数据&#xff0c;可以传递任意数据。 小程序开发中的事件是指视图层到逻辑层的通讯方式&#xff0c;主要是可以将用户的行为反馈到逻辑层进行处理。事件可以绑定在组件上&#xff0c;当达到触发事件&#…

java面向对象查缺

匿名对象 匿名对象只能使用一次 public class Test1 {public static void main(String[] args) {PhoneFactory p1 new PhoneFactory();p1.show(new Phone(1000,"black"));} } class PhoneFactory{public void show(Phone p){p.send();} } class Phone{private int…

Nginx代理接口访问返回404

Nginx代理接口访问返回404 一、背景 因为不同业务系统间有接口调用&#xff0c;存在跨域问题&#xff0c;为了解决同源策略&#xff0c;需要将接口通过nginx去转发&#xff0c;但是配置完后通过postman请求一直存在访问404的问题。 访问地址&#xff1a;https://a.test.com/n…

无代码集成明道云与更多应用连接

明道云是一个APaaS平台&#xff0c;可以帮助用户快速搭建个性化企业应用&#xff0c;用户不需要代码开发就能够搭建出用户体验上佳的销售、运营、人事、采购等核心业务应用&#xff0c;打通企业内部数据&#xff0c;也能够通过API和Webhook和其他系统对接。 场景描述&#xff…

Vue实战技巧:从零开始封装全局防抖和节流函数

前言 你是否曾经遇到过用户频繁点击按钮或滚动页面导致反应迟钝的问题&#xff1f;这是因为事件被连续触发&#xff0c;导致性能下降。在本文中&#xff0c;我将为大家介绍 vue 中的防抖和节流策略&#xff0c;并展示如何封装全局的防抖节流函数&#xff0c;以避免频繁触发事件…

酷开系统丨加入酷开会员,体验有趣的云逛荷兰海牙市立博物馆

夏日炎炎&#xff0c;出门是不可能的&#xff0c;还是宅在家里享受空调的吹拂吧&#xff01;如果想足不出户看展览&#xff0c;感受文化的熏陶&#xff0c;那就打开酷开系统&#xff0c;加入酷开会员&#xff0c;在云端逛逛荷兰海牙市立博物馆吧&#xff01; 荷兰海牙市立博物…

格式化后数据恢复,教你3个实用方法!

“格式化后数据还能恢复吗&#xff1f;前几天因为我的电脑中了病毒&#xff0c;我不得不将它进行格式化操作。但是我电脑里有很多比较重要的文件&#xff0c;有什么方法可以帮我恢复电脑中的文件吗&#xff1f;求解答&#xff01;” 格式化是一种比较常见的数据清除方法&#x…

低代码平台:初创公司的理想选择

对于初创公司而言&#xff0c;时间和资源是宝贵的。他们需要快速构建和部署应用程序&#xff0c;以满足业务需求&#xff0c;提高效率&#xff0c;并保持竞争优势。在这个背景下&#xff0c;低代码平台成为了初创公司的一个理想选择。而Zoho Creator作为一款出色的低代码平台&a…

node 报错:tagOffsetsMap[tag] ??= [];...SyntaxError: Unexpected token ,‘??=‘,亲测解决

安装的 node 版本不支持空值赋值运算符(??) 更换合适的 node 版本就行&#xff0c;如图 看看版本对应支持的 更多支持请在 node.green 上查看各种语法支持的版本

计算机视觉与图形学-神经渲染专题-pi-GAN and CIPS-3D

《pi-GAN: Periodic Implicit Generative Adversarial Networks for 3D-Aware Image Synthesis》 摘要 我们见证了3D感知图像合成的快速进展&#xff0c;利用了生成视觉模型和神经渲染的最新进展。然而&#xff0c;现有的方法在两方面存在不足&#xff1a;首先&#xff0c;它们…

ELK 将数据流转换回常规索引

ELK 将数据流转换回常规索引 现象&#xff1a;创建索引模板是打开了数据流&#xff0c;导致不能创建常规索引&#xff0c;并且手动修改、删除索引模板失败 "reason" : "composable template [logs_template] with index patterns [new-pattern*], priority [2…

SAP中采购文档出现定价转换因子字段溢出是何原因?

近期处理了一笔用户反馈的主题问题。这个问题有意思的地方地于&#xff0c;多重错误的叠加&#xff0c;导致了问题在开始就暴露出来&#xff0c;可以将隐患消除在萌芽状态。 在公司的应用中&#xff0c;会由采购创建价格合同&#xff0c;物流参照价格合同创建计划协议。但采购…

聊聊JDK动态代理原理

1. 示例 首先&#xff0c;定义一个接口&#xff1a; public interface Staff {void work(); }然后&#xff0c;新增一个类并实现上面的接口&#xff1a; public class Coder implements Staff {Overridepublic void work() {System.out.println("认真写bug……");…

为什么说用C端产品的思维做B端产品就是死路一条?

经常听行业大佬说起&#xff1a;如果用C端产品的思维做B端产品就是死路一条&#xff0c;那原因究竟是什么呢&#xff1f; 首先&#xff0c;需要明确的是C端产品和B端产品的用户群体和需求存在很大差异。C端产品的用户主要是消费者&#xff0c;更多的是被情感驱动。而B端产品的…

Visual Studio 2022安装教程(英文版)

文章目录 1.下载安装 1.下载 官网地址&#xff1a;https://visualstudio.microsoft.com/zh-hans/vs/ 选择第一个社区版本&#xff1a;Community 2022 安装 1.将下载好的文件保存到桌面&#xff0c;双击点开 2.等待visual studio installer配置好 3.点击安装后会来到配件选…

消息队列(3) -封装数据库的操作

前言 上一篇博客我们写了, 关于交换机, 队列,绑定, 写入数据库的一些建库建表的操作 这一篇博客中,我们将建库建表操作,封装一下实现层一个类来供上层服务的调用 , 并在写完该类之后, 测试代码是否完整 实现封装 在写完上述的接口类 与 xml 后, 我们想要 创建一个类 ,来调用…

使用OpenCV进行目标提取详细教程(附python代码演练)

今天的文章将讨论并指导你识别图像中的对象&#xff0c;使用 OpenCV 对这些对象进行遮罩处理。让我们开始吧&#xff01; HSV 色标 请花一点时间观察下面的图片。每个图块似乎是不同的颜色&#xff0c;对吧&#xff1f;但是有一个有趣的地方&#xff1a;如果我们仔细思考&#…

B2B2C跨境独立站后台管理--支付系统开源搭建

要搭建一个B2B2C跨境独立站后台管理的支付系统&#xff0c;您可以按照以下步骤进行开发&#xff1a; 1. 确定需求和功能&#xff1a;首先&#xff0c;您需要明确支付系统的功能和需求&#xff0c;包括接入第三方支付平台、实现支付功能、订单管理、退款管理、对账功能等。 2.…