基于Java實(shí)現(xiàn)的一層簡(jiǎn)單人工神經(jīng)網(wǎng)絡(luò)算法示例
本文實(shí)例講述了基于Java實(shí)現(xiàn)的一層簡(jiǎn)單人工神經(jīng)網(wǎng)絡(luò)算法。分享給大家供大家參考,具體如下:
先來看看筆者繪制的算法圖:


2、數(shù)據(jù)類
import java.util.Arrays;
public class Data {
double[] vector;
int dimention;
int type;
public double[] getVector() {
return vector;
}
public void setVector(double[] vector) {
this.vector = vector;
}
public int getDimention() {
return dimention;
}
public void setDimention(int dimention) {
this.dimention = dimention;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Data(double[] vector, int dimention, int type) {
super();
this.vector = vector;
this.dimention = dimention;
this.type = type;
}
public Data() {
}
@Override
public String toString() {
return "Data [vector=" + Arrays.toString(vector) + ", dimention=" + dimention + ", type=" + type + "]";
}
}
3、簡(jiǎn)單人工神經(jīng)網(wǎng)絡(luò)
package cn.edu.hbut.chenjie;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.ui.RefineryUtilities;
public class ANN2 {
private double eta;//學(xué)習(xí)率
private int n_iter;//權(quán)重向量w[]訓(xùn)練次數(shù)
private List<Data> exercise;//訓(xùn)練數(shù)據(jù)集
private double w0 = 0;//閾值
private double x0 = 1;//固定值
private double[] weights;//權(quán)重向量,其長(zhǎng)度為訓(xùn)練數(shù)據(jù)維度+1,在本例中數(shù)據(jù)為2維,故長(zhǎng)度為3
private int testSum = 0;//測(cè)試數(shù)據(jù)總數(shù)
private int error = 0;//錯(cuò)誤次數(shù)
DefaultXYDataset xydataset = new DefaultXYDataset();
/**
* 向圖表中增加同類型的數(shù)據(jù)
* @param type 類型
* @param a 所有數(shù)據(jù)的第一個(gè)分量
* @param b 所有數(shù)據(jù)的第二個(gè)分量
*/
public void add(String type,double[] a,double[] b)
{
double[][] data = new double[2][a.length];
for(int i=0;i<a.length;i++)
{
data[0][i] = a[i];
data[1][i] = b[i];
}
xydataset.addSeries(type, data);
}
/**
* 畫圖
*/
public void draw()
{
JFreeChart jfreechart = ChartFactory.createScatterPlot("exercise", "x1", "x2", xydataset);
ChartFrame frame = new ChartFrame("訓(xùn)練數(shù)據(jù)", jfreechart);
frame.pack();
RefineryUtilities.centerFrameOnScreen(frame);
frame.setVisible(true);
}
public static void main(String[] args)
{
ANN2 ann2 = new ANN2(0.001,100);//構(gòu)造人工神經(jīng)網(wǎng)絡(luò)
List<Data> exercise = new ArrayList<Data>();//構(gòu)造訓(xùn)練集
//人工模擬1000條訓(xùn)練數(shù)據(jù) ,分界線為x2=x1+0.5
for(int i=0;i<1000000;i++)
{
Random rd = new Random();
double x1 = rd.nextDouble();//隨機(jī)產(chǎn)生一個(gè)分量
double x2 = rd.nextDouble();//隨機(jī)產(chǎn)生另一個(gè)分量
double[] da = {x1,x2};//產(chǎn)生數(shù)據(jù)向量
Data d = new Data(da, 2, x2 > x1+0.5 ? 1 : -1);//構(gòu)造數(shù)據(jù)
exercise.add(d);//將訓(xùn)練數(shù)據(jù)加入訓(xùn)練集
}
int sum1 = 0;//記錄類型1的訓(xùn)練記錄數(shù)
int sum2 = 0;//記錄類型-1的訓(xùn)練記錄數(shù)
for(int i = 0; i < exercise.size(); i++)
{
if(exercise.get(i).getType()==1)
sum1++;
else if(exercise.get(i).getType()==-1)
sum2++;
}
double[] x1 = new double[sum1];
double[] y1 = new double[sum1];
double[] x2 = new double[sum2];
double[] y2 = new double[sum2];
int index1 = 0;
int index2 = 0;
for(int i = 0; i < exercise.size(); i++)
{
if(exercise.get(i).getType()==1)
{
x1[index1] = exercise.get(i).vector[0];
y1[index1++] = exercise.get(i).vector[1];
}
else if(exercise.get(i).getType()==-1)
{
x2[index2] = exercise.get(i).vector[0];
y2[index2++] = exercise.get(i).vector[1];
}
}
ann2.add("1", x1, y1);
ann2.add("-1", x2, y2);
ann2.draw();
ann2.input(exercise);//將訓(xùn)練集輸入人工神經(jīng)網(wǎng)絡(luò)
ann2.fit();//訓(xùn)練
ann2.showWeigths();//顯示權(quán)重向量
//人工生成一千條測(cè)試數(shù)據(jù)
for(int i=0;i<10000;i++)
{
Random rd = new Random();
double x1_ = rd.nextDouble();
double x2_ = rd.nextDouble();
double[] da = {x1_,x2_};
Data test = new Data(da, 2, x2_ > x1_+0.5 ? 1 : -1);
ann2.predict(test);//測(cè)試
}
System.out.println("總共測(cè)試" + ann2.testSum + "條數(shù)據(jù),有" + ann2.error + "條錯(cuò)誤,錯(cuò)誤率:" + ann2.error * 1.0 /ann2.testSum * 100 + "%");
}
/**
*
* @param eta 學(xué)習(xí)率
* @param n_iter 權(quán)重分量學(xué)習(xí)次數(shù)
*/
public ANN2(double eta, int n_iter) {
this.eta = eta;
this.n_iter = n_iter;
}
/**
* 輸入訓(xùn)練集到人工神經(jīng)網(wǎng)絡(luò)
* @param exercise
*/
private void input(List<Data> exercise) {
this.exercise = exercise;//保存訓(xùn)練集
weights = new double[exercise.get(0).dimention + 1];//初始化權(quán)重向量,其長(zhǎng)度為訓(xùn)練數(shù)據(jù)維度+1
weights[0] = w0;//權(quán)重向量第一個(gè)分量為w0
for(int i = 1; i < weights.length; i++)
weights[i] = 0;//其余分量初始化為0
}
private void fit() {
for(int i = 0; i < n_iter; i++)//權(quán)重分量調(diào)整n_iter次
{
for(int j = 0; j < exercise.size(); j++)//對(duì)于訓(xùn)練集中的每條數(shù)據(jù)進(jìn)行訓(xùn)練
{
int real_result = exercise.get(j).type;//y
int calculate_result = CalculateResult(exercise.get(j));//y'
double delta0 = eta * (real_result - calculate_result);//計(jì)算閾值更新
w0 += delta0;//閾值更新
weights[0] = w0;//更新w[0]
for(int k = 0; k < exercise.get(j).getDimention(); k++)//更新權(quán)重向量其它分量
{
double delta = eta * (real_result - calculate_result) * exercise.get(j).vector[k];
//Δw=η*(y-y')*X
weights[k+1] += delta;
//w=w+Δw
}
}
}
}
private int CalculateResult(Data data) {
double z = w0 * x0;
for(int i = 0; i < data.dimention; i++)
z += data.vector[i] * weights[i+1];
//z=w0x0+w1x1+...+WmXm
//激活函數(shù)
if(z>=0)
return 1;
else
return -1;
}
private void showWeigths()
{
for(double w : weights)
System.out.println(w);
}
private void predict(Data data) {
int type = CalculateResult(data);
if(type == data.getType())
{
//System.out.println("預(yù)測(cè)正確");
}
else
{
//System.out.println("預(yù)測(cè)錯(cuò)誤");
error ++;
}
testSum ++;
}
}
運(yùn)行結(jié)果:
-0.22000000000000017 -0.4416843982815453 0.442444202054685 總共測(cè)試10000條數(shù)據(jù),有17條錯(cuò)誤,錯(cuò)誤率:0.16999999999999998%

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java手動(dòng)創(chuàng)建線程池代碼實(shí)例
這篇文章主要介紹了Java手動(dòng)創(chuàng)建線程池代碼實(shí)例,FixedThreadPool或者SingleThreadPool,允許的請(qǐng)求隊(duì)列長(zhǎng)度為Integer.MAX_VALUE,可能會(huì)堆積大量的請(qǐng)求,從而導(dǎo)致OOM,需要的朋友可以參考下2023-12-12
springBoot解決static和@Component遇到的bug
這篇文章主要介紹了springBoot解決static和@Component遇到的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場(chǎng)景分析
這篇文章主要介紹了SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場(chǎng)景分析,本篇文章簡(jiǎn)單介紹?OpenFeign?調(diào)用傳遞?header?,以及多線程環(huán)境下可能會(huì)出現(xiàn)的問題,其中涉及到?ThreadLocal?的相關(guān)知識(shí),需要的朋友可以參考下2022-07-07
Java中字符串與日期轉(zhuǎn)換常見方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Java中字符串與日期轉(zhuǎn)換常見方法的相關(guān)資料,在Java編程中經(jīng)常需要將字符串表示的日期轉(zhuǎn)換為日期對(duì)象進(jìn)行處理,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
Java設(shè)計(jì)模式之橋接模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之橋接模式,結(jié)合實(shí)例形式詳細(xì)分析了橋接模式的概念、功能、Java實(shí)現(xiàn)方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09
SpringBoot使用Maven實(shí)現(xiàn)多環(huán)境配置管理
軟件開發(fā)中經(jīng)常有開發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境,而且一般這些環(huán)境配置會(huì)各不相同,本文主要介紹了SpringBoot使用Maven實(shí)現(xiàn)多環(huán)境配置管理,感興趣的可以了解一下2024-01-01
Java單線程程序?qū)崿F(xiàn)實(shí)現(xiàn)簡(jiǎn)單聊天功能
這篇文章主要介紹了Java單線程程序?qū)崿F(xiàn)實(shí)現(xiàn)簡(jiǎn)單聊天功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
java.lang.ArrayStoreException異常的解決方案
這篇文章主要介紹了java.lang.ArrayStoreException異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

