⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backpropxor.java

📁 CroftSoft Code Library是一个开源的可移植的纯Java游戏库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     package com.croftsoft.apps.backpropxor;

     import java.awt.Choice;
     import java.awt.Color;
     import java.awt.Font;
     import java.awt.Graphics;
     import java.awt.Rectangle;
     import java.awt.event.*;
     import java.text.DecimalFormat;
     import javax.swing.*;

     import com.croftsoft.core.gui.ShutdownWindowListener;
     import com.croftsoft.core.gui.FrameLib;
     import com.croftsoft.core.gui.plot.PlotLib;
     import com.croftsoft.core.lang.lifecycle.Lifecycle;
     import com.croftsoft.core.math.*;

     /*********************************************************************
     * A Backpropagation neural network learning algorithm demonstration.
     *
     * @version
     *   2002-02-28
     * @since
     *   1996
     * @author
     *   <a href="http://www.alumni.caltech.edu/~croft/">David W. Croft</a>
     *********************************************************************/

     public class  BackpropXor
       extends JPanel
       implements ActionListener, ItemListener, Lifecycle, Runnable,
         BackpropXorConstants
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     private Matrix  l0_activations      = new Matrix ( 3, 1 );
     private Matrix  l1_activations      = new Matrix ( 2, 1 );
     private Matrix  l2_inputs           = new Matrix ( 3, 1 );
     private Matrix  l2_activations      = new Matrix ( 1, 1 );
     private Matrix  l1_weights          = new Matrix ( 3, 2 );
     private Matrix  l2_weights          = new Matrix ( 3, 1 );
     private Matrix  l1_weighted_sums    = new Matrix ( 2, 1 );
     private Matrix  l2_weighted_sums    = new Matrix ( 1, 1 );
     private double  output_desired      = 0.0;
     private double  output_error        = 0.0;
     private Matrix  l2_local_gradient   = new Matrix ( 1, 1 );
     private double  learning_rate       = 0.1;
     private double  momentum_constant   = 0.9;
     private Matrix  l2_weights_delta    = new Matrix ( 3, 1 );
     private Matrix  l2_weights_momentum = new Matrix ( 3, 1 );
     private Matrix  sum_weighted_deltas = new Matrix ( 2, 1 );
     private Matrix  l1_local_gradients  = new Matrix ( 2, 1 );
     private Matrix  l1_weights_delta    = new Matrix ( 3, 2 );
     private Matrix  l1_weights_momentum = new Matrix ( 3, 2 );

     //

     private long        total_samples = 0;

     private int         iteration = 0;
     private double [ ]  squared_errors = new double [ ITERATIONS_PER_EPOCH ];
     private int         epoch = 0;
     private int         epochs_max = 1000;
     private double [ ]  epoch_rms_error = new double [ epochs_max ];

     private double [ ] [ ]  samples = new double [ ITERATIONS_PER_EPOCH ] [ 3 ];

     //

     private Rectangle  r;
     private Rectangle  rs;

     //

     private Thread   runner;

     private boolean  pleaseStop = false;

     private boolean  isPaused;

     //

     private JComboBox   functionComboBox;

     private JTextField  learning_rate_TextField;

     private JLabel      learning_rate_Label;

     private JTextField  momentum_TextField;

     private JLabel      momentum_Label;

     private JButton     randomize_Button;

     private JButton     reset_Button;

     private JButton     pause_Button;

     //

     private int     function_selected;

     private String  function_String;

     private int  y2;

     private int  y4;

     private DecimalFormat  decimalFormat
       = new DecimalFormat ( "0.000000" );

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public static void  main ( String [ ]  args )
     //////////////////////////////////////////////////////////////////////
     {
       JFrame  jFrame = new JFrame ( TITLE );

/*
       try
       {
         jFrame.setIconImage ( ClassLib.getResourceAsImage (
           BackpropXor.class, FRAME_ICON_FILENAME ) );
       }
       catch ( Exception  ex )
       {
         ex.printStackTrace ( );
       }
*/

       BackpropXor  backpropXor = new BackpropXor ( );

       jFrame.setContentPane ( backpropXor );

       FrameLib.launchJFrameAsDesktopApp (
         jFrame,
         new Lifecycle [ ] { backpropXor },
         FRAME_SIZE,
         SHUTDOWN_CONFIRMATION_PROMPT );
     }

     //////////////////////////////////////////////////////////////////////
     // interface Lifecycle methods
     //////////////////////////////////////////////////////////////////////

     public void  init ( )
     //////////////////////////////////////////////////////////////////////
     {
       setFont ( new Font ( "Times New Roman", Font.PLAIN, FONT_SIZE ) );

       function_selected = INITIAL_FUNCTION;

       function_String = FUNCTION_NAMES [ function_selected ];

       functionComboBox = new JComboBox ( FUNCTION_NAMES );

       functionComboBox.setSelectedIndex ( function_selected );

       learning_rate_TextField = new JTextField ( "" + learning_rate, 4 );

       learning_rate_Label = new JLabel ( "Learning Rate" );

       momentum_TextField = new JTextField ( "" + momentum_constant, 4 );

       momentum_Label = new JLabel ( "Momentum Factor" );

       add ( functionComboBox );
       add ( momentum_Label );
       add ( momentum_TextField );
       add ( learning_rate_Label );
       add ( learning_rate_TextField );

       JPanel  buttonPanel = new JPanel ( );

       randomize_Button = new JButton ( "Randomize Weights" );

       reset_Button = new JButton ( "Reset Strip" );

       pause_Button = new JButton ( "  Pause  " );

       buttonPanel.add ( randomize_Button );
       
       buttonPanel.add ( pause_Button );
       
       buttonPanel.add ( reset_Button );

       add ( buttonPanel );

       functionComboBox       .addItemListener   ( this );

       momentum_TextField     .addActionListener ( this );

       learning_rate_TextField.addActionListener ( this );

       randomize_Button       .addActionListener ( this );

       pause_Button           .addActionListener ( this );

       reset_Button           .addActionListener ( this );

       r = new Rectangle (
         10, 10 + Y, SIZE.width - 100, SIZE.height - 350 );

       y2 = 30 + Y + r.height;

       y4 = y2 + YTAB * 8;

       rs = new Rectangle (
         10 + r.width + 10, 10 + Y,
         SIZE.height - 350, SIZE.height - 350 );

       randomize_weights ( );
     }

     public void  start ( )
     //////////////////////////////////////////////////////////////////////
     {
       if ( ( runner == null ) && !isPaused )
       {
         pleaseStop = false;

         runner = new Thread ( this );

         runner.setPriority ( runner.getPriority ( ) - 1 );

         runner.setDaemon ( true );

         runner.start ( );
       }
     }

     public void  stop ( )
     //////////////////////////////////////////////////////////////////////
     {
       pleaseStop = true;
     }

     public void  destroy ( )
     //////////////////////////////////////////////////////////////////////
     {
       stop ( );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public void  itemStateChanged ( ItemEvent  itemEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Object  source = itemEvent.getSource ( );

       if ( source == functionComboBox )
       {
         function_selected = functionComboBox.getSelectedIndex ( );

         function_String = ( String ) functionComboBox.getSelectedItem ( );
       }
     }

     public void  actionPerformed ( ActionEvent  actionEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Object  source = actionEvent.getSource ( );

       if ( source == momentum_TextField )
       {
         momentum_constant = Double.valueOf (
           momentum_TextField.getText ( ) ).doubleValue ( );
       }
       else if ( source == learning_rate_TextField )
       {
         learning_rate = Double.valueOf (
           learning_rate_TextField.getText ( ) ).doubleValue ( );
       }
       else if ( source == randomize_Button )
       {
         randomize_weights ( );
       }
       else if ( source == reset_Button )
       {
         total_samples = 0;

         iteration = 0;

         epoch = 0;
       }
       else if ( source == pause_Button )
       {
         if ( isPaused )
         {
           isPaused = false;

           pause_Button.setText ( " Pause " );

           start ( );           
         }
         else
         {
           isPaused = true;

           pause_Button.setText ( "Continue" );

           stop ( );
         }
       }
     }

     public void  paintComponent ( Graphics g )
     //////////////////////////////////////////////////////////////////////
     {
       super.paintComponent ( g );

       plot_epochs ( r, g, epoch_rms_error );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -