参考下面代码:
创新互联建站专注于赤壁企业网站建设,响应式网站开发,成都做商城网站。赤壁网站建设公司,为赤壁等地区提供建站服务。全流程按需求定制制作,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数
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();//随机初始化权重
}
}
}
//逐层向前计算输出
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];
}
//逐层反向计算误差并修改权重
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];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
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];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
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) {
/*
* 定义窗体,标题
*/
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);
}
});
/*
* 标题栏的叉的退出事件
*/
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
/*
* 布局,采用简单的Box+Flow的布局,可以采用Grid或者更复杂的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行为准,超过千行就要考虑类拆分了。类的代码行数没有特定的行数限制规范。根据实际情况决定。对于经常使用的java类,代码行数应该尽可能的少,这样能减少java类的加载时间,减少内存频繁占用和回收。如果类过大,java类加载会耗时并且占用内存大。容易引起内存回收。
Java是一种简单的,面向对象的,分布式的,解释型的,健壮安全的,结构中立的,可移植的,性能优异、多线程的动态语言。
Java语言其实最早诞生于1991年,起初被称为OAK语言,是SUN公司为一些消费性电子产品而设计的一个通用环境。他们最初的目的只是为了开发一种独立于平台的软件技术,而且在网络出现之前,OAK可以说是默默无闻,甚至差点夭折。但是,网络的出现改变了OAK的命运。