參考下面代碼:
創(chuàng)新互聯(lián)建站專注于赤壁企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都做商城網(wǎng)站。赤壁網(wǎng)站建設(shè)公司,為赤壁等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
import java.util.Random;
public class BpDeep{
public double[][] layer;//神經(jīng)網(wǎng)絡(luò)各層節(jié)點(diǎn)
public double[][] layerErr;//神經(jīng)網(wǎng)絡(luò)各節(jié)點(diǎn)誤差
public double[][][] layer_weight;//各層節(jié)點(diǎn)權(quán)重
public double[][][] layer_weight_delta;//各層節(jié)點(diǎn)權(quán)重動(dòng)量
public double mobp;//動(dòng)量系數(shù)
public double rate;//學(xué)習(xí)系數(shù)
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//隨機(jī)初始化權(quán)重
}
}
}
//逐層向前計(jì)算輸出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐層反向計(jì)算誤差并修改權(quán)重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隱含層動(dòng)量調(diào)整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隱含層權(quán)重調(diào)整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距動(dòng)量調(diào)整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距權(quán)重調(diào)整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//記錄誤差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class TestWindow {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 定義窗體,標(biāo)題
*/
JFrame frame=new JFrame("java");
/*
* 定義菜單
*/
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("文件(F)");
menuFile.setMnemonic(KeyEvent.VK_F);
menuBar.add(menuFile);
JMenuItem exitMenu=new JMenuItem("退出");
menuFile.add(exitMenu);
frame.setJMenuBar(menuBar);
/*
*菜單事件
*/
exitMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
/*
* 標(biāo)題欄的叉的退出事件
*/
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
/*
* 布局,采用簡(jiǎn)單的Box+Flow的布局,可以采用Grid或者更復(fù)雜的GridBagLayout
*/
JPanel panelMain=new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
frame.getContentPane().add(panelMain,BorderLayout.CENTER);
//添加組件
JPanel line1Panel=new JPanel();
JPanel line2Panel=new JPanel();
JPanel line3Panel=new JPanel();
JPanel line4Panel=new JPanel();
line1Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
line2Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
line3Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
line4Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panelMain.add(line1Panel);
panelMain.add(line2Panel);
panelMain.add(line3Panel);
panelMain.add(line4Panel);
line1Panel.add(new JLabel("姓名:"));
JTextField txtName=new JTextField();
txtName.setColumns(10);
line1Panel.add(txtName);
line1Panel.add(new JLabel("密碼:"));
JPasswordField txtPass=new JPasswordField();
txtPass.setColumns(10);
line1Panel.add(txtPass);
line2Panel.add(new JLabel("性別:"));
JRadioButton radioM=new JRadioButton("男");
JRadioButton radioF=new JRadioButton("女");
line2Panel.add(radioM);
line2Panel.add(radioF);
ButtonGroup group = new ButtonGroup();
group.add(radioM);
group.add(radioF);
Object[] years=new Object[50];
java.util.Calendar cal= Calendar.getInstance();
int year=cal.get(Calendar.YEAR);
for(int i=0;i50;i++)
years[i]=new Integer(year-i);
Object[] months=new Object[12];
for(int i=0;i12;i++)
months[i]=new Integer(i+1);
JComboBox yearList=new JComboBox(years);
JComboBox monthList=new JComboBox(months);
line3Panel.add(new JLabel("出生年月:"));
line3Panel.add(yearList);
line3Panel.add(new JLabel("年"));
line3Panel.add(monthList);
line3Panel.add(new JLabel("月"));
JCheckBox chkRead=new JCheckBox("讀書");
JCheckBox chkMovies=new JCheckBox("電影");
line4Panel.add(new JLabel("愛好:"));
line4Panel.add(chkRead);
line4Panel.add(chkMovies);
frame.pack();
frame.setVisible(true);
}
}
以1000行為準(zhǔn),超過千行就要考慮類拆分了。類的代碼行數(shù)沒有特定的行數(shù)限制規(guī)范。根據(jù)實(shí)際情況決定。對(duì)于經(jīng)常使用的java類,代碼行數(shù)應(yīng)該盡可能的少,這樣能減少java類的加載時(shí)間,減少內(nèi)存頻繁占用和回收。如果類過大,java類加載會(huì)耗時(shí)并且占用內(nèi)存大。容易引起內(nèi)存回收。
Java是一種簡(jiǎn)單的,面向?qū)ο蟮?,分布式的,解釋型的,健壯安全的,結(jié)構(gòu)中立的,可移植的,性能優(yōu)異、多線程的動(dòng)態(tài)語言。
Java語言其實(shí)最早誕生于1991年,起初被稱為OAK語言,是SUN公司為一些消費(fèi)性電子產(chǎn)品而設(shè)計(jì)的一個(gè)通用環(huán)境。他們最初的目的只是為了開發(fā)一種獨(dú)立于平臺(tái)的軟件技術(shù),而且在網(wǎng)絡(luò)出現(xiàn)之前,OAK可以說是默默無聞,甚至差點(diǎn)夭折。但是,網(wǎng)絡(luò)的出現(xiàn)改變了OAK的命運(yùn)。