非学习型单层感知机的java实现(日志三)
要求如下:
所以当神经元输出函数选择在硬极函数的时候,如果想分成上面的四个类型,则必须要2个神经元,其实至于所有的分类问题,n个神经元则可以分成2的n次方类型。
又前一节所证明出来的关系有:
从而算出了所有的权重的值。。
代码实现如下:
第一个类是用来操实际操作的类,真正核心的内容是在PerceptronClassifyNoLearn中。
package com.cgrj.com; import java.util.Arrays; import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.Perceptron; public class MyNeturol { public static void main(String[] args) {
// TODO Auto-generated method stub
DataSet trainingSet=new DataSet(2,2);
trainingSet.addRow(new DataSetRow(new double[]{1,2},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{1,1},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{2,0},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{2,-1},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{-1,2},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{-2,1},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{-1,-1},new double[]{Double.NaN,Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[]{-2,-2},new double[]{Double.NaN,Double.NaN})); PerceptronClassifyNoLearn perceptronClassifyNoLearn=new PerceptronClassifyNoLearn(2); for(DataSetRow row:trainingSet.getRows()){
perceptronClassifyNoLearn.setInput(row.getInput());
perceptronClassifyNoLearn.calculate();
double[] netWorkOutput=perceptronClassifyNoLearn.getOutput();
System.out.println(Arrays.toString(row.getInput())+"="+Arrays.toString(netWorkOutput)); } } }
PerceptronClassifyNoLearn规定了输入层和输出层的属性和规则,由于是无法学的,所以其判定规则是依然设定好了的,在此类中。
package com.cgrj.com; import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.Neuron;
import org.neuroph.nnet.comp.neuron.BiasNeuron;
import org.neuroph.nnet.comp.neuron.InputNeuron;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.LayerFactory;
import org.neuroph.util.NeuralNetworkFactory;
import org.neuroph.util.NeuralNetworkType;
import org.neuroph.util.NeuronProperties;
import org.neuroph.util.TransferFunctionType; public class PerceptronClassifyNoLearn extends NeuralNetwork { public PerceptronClassifyNoLearn(int inputNeuronsCount){
this.createNetWork(inputNeuronsCount); } private void createNetWork(int inputNeuronsCount) {
//设置网络感知机
this.setNetworkType(NeuralNetworkType.PERCEPTRON); //构建输入神经元,表示输入的刺激
NeuronProperties inputNeuronProperties=new NeuronProperties();
inputNeuronProperties.setProperty("neuronType", InputNeuron.class); //由输入神经元构成的输入层
Layer inputLayer=LayerFactory.createLayer(inputNeuronsCount,inputNeuronProperties);
this.addLayer(inputLayer);
//给输入层增加BiasNeron,表示神经元偏置
inputLayer.addNeuron(new BiasNeuron()); //构建输出神经元
NeuronProperties outputNeuronProperties=new NeuronProperties();
outputNeuronProperties.setProperty("transferFunction", TransferFunctionType.STEP);
Layer outputLayer=LayerFactory.createLayer(2, outputNeuronProperties);
this.addLayer(outputLayer); ConnectionFactory.fullConnect(inputLayer, outputLayer);
NeuralNetworkFactory.setDefaultIO(this);
Neuron n=outputLayer.getNeuronAt(0);
n.getInputConnections()[0].getWeight().setValue(-3);
n.getInputConnections()[1].getWeight().setValue(-1);
n.getInputConnections()[2].getWeight().setValue(1); n=outputLayer.getNeuronAt(1);
n.getInputConnections()[0].getWeight().setValue(1);
n.getInputConnections()[1].getWeight().setValue(-2);
n.getInputConnections()[2].getWeight().setValue(0); }
}
可以应用于象限的判定,修改上面的代码如下:
Neuron n=outputLayer.getNeuronAt(0);
n.getInputConnections()[0].getWeight().setValue(0);
n.getInputConnections()[1].getWeight().setValue(1);
n.getInputConnections()[2].getWeight().setValue(0); n=outputLayer.getNeuronAt(1);
n.getInputConnections()[0].getWeight().setValue(1);
n.getInputConnections()[1].getWeight().setValue(0);
n.getInputConnections()[2].getWeight().setValue(0);
则有第一个用来判定位于y的方向,第一个神经元则用来判定位于x轴的方向
switch (Arrays.toString(netWorkOutput)) {
case "[1.0, 1.0]":
str="第一象限";
break;
case "[0.0, 1.0]":
str="第四象限";
break;
case "[1.0, 0.0]":
str="第二象限";
break;
case "[0.0, 0.0]":
str="第三象限";
break; default:
break;
} System.out.println(Arrays.toString(row.getInput())+"="+Arrays.toString(netWorkOutput)+"---属于"+str);
这样就会有打印的结果了。。
运行截图(这里忽略坐标轴的影响,由于输出函数的特殊,所以把0当成负数看):
下一篇,将具体分析每个类和每个方法的含义,及其实现的原理。。。
非学习型单层感知机的java实现(日志三)的更多相关文章
- 基于STM32的学习型通用红外遥控设备的设计实现(三)
CPU: STM32 调试平台: STM32F103ZET和STM32F103VBT 软件平台: Keil uVision4 电路设计: Altium Designer v6.9 http://blo ...
- Java学习笔记之linux配置java环境变量(三种环境变量)
0x00 压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车 ...
- 检查型异常和非检查型异常——Java
文章目录 检查型异常和非检查型异常--Java 检查型异常 非检查型异常 结语 检查型异常和非检查型异常--Java Java语言规范将派生于Error类或RuntimeExceprion类的所有异常 ...
- Java检查型异常和非检查型异常
1.代码 public class ExcepTest { /** * @param args */ public static void main(String[] args) { System.e ...
- 深度学习:多层感知机和异或问题(Pytorch实现)
感知机模型 假设输入空间\(\mathcal{X}\subseteq \textbf{R}^n\),输出空间是\(\mathcal{Y}=\{-1,+1\}\).输入\(\textbf{x}\in \ ...
- Java学习笔记(十七)——java序列化
[前面的话] 做项目总是要用到很多东西,遇到一个新的知识,并不是这个知识出来的时间短,而是对于自己来说是新的,所以就需要自己去学习,希望今后可以提高学习的效率. 这篇文章是关于Java 序列化的,选择 ...
- Java基础学习总结(83)——Java泛型总结
1. 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型 ...
- Java 学习(7):java 日期时间 & 正则表达式
目录 --- 日期时间 --- 正则表达式 日期时间:java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实例化 Date 对象. 构造函数:用于初始化对 ...
- Java 学习(9):java Stream & File & IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
随机推荐
- 兼容主流浏览器的js原生函数封装
1.获取和修改垂直滚动条的滚动距离 //获取滚动条滚动距离function getScrollTop(){ var fromTop=document.documentElement.scrollTop ...
- flask mega-tutorial 1.0 documentation学习记录
本文主要是记录在[用户登录]一节中出现的问题: 报错位置是在 if g.user is not None and g.user.is_authenticated(): return redirect( ...
- java封装的方法
java封装是由Java是面向对象程序设计语言的性质决定的,面向对象程序设计语言的三大特性之一就是封装.封装其实就是包装的意思,从专业的角度来看,就是把对象的所有组成部分组合在一起,保护私有属性. 如 ...
- js,jq获取元素位置属性及兼容性写法
网页被卷起来的高度/宽度 document.documentElement.scrolltop //火狐 和 其他浏览器 document.body.scrolltop //ie,谷歌浏览器和没有 ...
- 使用Python对Access读写操作
学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件. 1.导入模块 import win ...
- 解决虚拟机vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...
- Omi新成员omi-router正式发布
原文链接-https://github.com/AlloyTeam/omi/blob/master/tutorial omi-router omi-router是Omi框架专属的router插件,文件 ...
- vue2.0transition过渡的使用介绍
直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 参加光环国际PRINCE2培训
挑战埃及是全球首套能够同时适配PRINCE2认证人群,PMP认证人群的项目管理沙盘演练游戏.沙盘通过使用乐高积木作为道具,通过一场互动性极强的情景模拟为全球项目经理还原了四千年前古埃及金字塔建造的情景 ...
- Node.js 安装配置介绍
Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v6.10.1 LTS(长期支持版本)版本为例. Node.js安装 ...