Home » Archimedes archive » Archimedes World » archimedes_world_volume_14_issue_12_scp.adf » !AcornAns_AcornAns » Box3

Box3

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Archimedes World » archimedes_world_volume_14_issue_12_scp.adf » !AcornAns_AcornAns
Filename: Box3
Read OK:
File size: 0878 bytes
Load address: 0000
Exec address: 0000
File contents
Backpropagation training flow
=============================
BOOL backpropagation_training(network_rec *net,
        weights_rec *wts, int no_of_training_items,
        get_data_proc get_network_input_output,
        BOOL verbose, ff_setup_rec *setup)
does:
  ensure_network_exists(), creating it if necessary
  setup_screen()
  while network weights not adequately trained
    get_network_input_output(), that is, get a sample input
    and corresponding desired output
    feedforward(), hence computing actual output
    ff_error(), calculating error between desired and actual
    if error too big then apply backpropagation() to modify weights
    display_results() of progress so far
  endwhile



Flows specific to brolly application
====================================
Our get_data_proc function,
brolly_dynamics_input(vector x_in, vector desired_out, ff_setup_rec *setup)
does:
  get_random_brolly_state(), that is, get a random 'initial state'
  pick a random 'force'
  get_network_input(), that is, normalise the 'initial state' & 'force', putting result in x_in
  force_effect(), that is, see how system evolves from initial state by simulation
  set up desired_out[] by normalising simulated actual output

During an actual game our trained net is used to control the brolly via,
unit neural_control(state_rec *s)
which does:
  assume zero force
  get_network_input(), that is, normalise system state *s & zero force
  feedforward(), that is, anticipate normalised theta if no force applied, via the network
  set out_angle = unnormalised g_network.y_out[1], that is, unnormalise theta
  if (out_angle>g_brm_setup.zero_theta)  return  g_brm_setup.system_force_incr
  if (out_angle<-g_brm_setup.zero_theta) return -g_brm_setup.system_force_incr
  else return zero, that is, return a suitable non-zero force if and only if anticipated theta
  is big enough



Typical overall flow for a net program
======================================
network_initialise_parameters()
create_max_min_data()
initialise_weights() randomly
backpropagation_training()
while something or other
  do stuff
  feedforward()
  then more stuff, using net output as desired
endwhile
00000000  42 61 63 6b 70 72 6f 70  61 67 61 74 69 6f 6e 20  |Backpropagation |
00000010  74 72 61 69 6e 69 6e 67  20 66 6c 6f 77 0a 3d 3d  |training flow.==|
00000020  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000030  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 0a 42 4f 4f 4c  |===========.BOOL|
00000040  20 62 61 63 6b 70 72 6f  70 61 67 61 74 69 6f 6e  | backpropagation|
00000050  5f 74 72 61 69 6e 69 6e  67 28 6e 65 74 77 6f 72  |_training(networ|
00000060  6b 5f 72 65 63 20 2a 6e  65 74 2c 0a 20 20 20 20  |k_rec *net,.    |
00000070  20 20 20 20 77 65 69 67  68 74 73 5f 72 65 63 20  |    weights_rec |
00000080  2a 77 74 73 2c 20 69 6e  74 20 6e 6f 5f 6f 66 5f  |*wts, int no_of_|
00000090  74 72 61 69 6e 69 6e 67  5f 69 74 65 6d 73 2c 0a  |training_items,.|
000000a0  20 20 20 20 20 20 20 20  67 65 74 5f 64 61 74 61  |        get_data|
000000b0  5f 70 72 6f 63 20 67 65  74 5f 6e 65 74 77 6f 72  |_proc get_networ|
000000c0  6b 5f 69 6e 70 75 74 5f  6f 75 74 70 75 74 2c 0a  |k_input_output,.|
000000d0  20 20 20 20 20 20 20 20  42 4f 4f 4c 20 76 65 72  |        BOOL ver|
000000e0  62 6f 73 65 2c 20 66 66  5f 73 65 74 75 70 5f 72  |bose, ff_setup_r|
000000f0  65 63 20 2a 73 65 74 75  70 29 0a 64 6f 65 73 3a  |ec *setup).does:|
00000100  0a 20 20 65 6e 73 75 72  65 5f 6e 65 74 77 6f 72  |.  ensure_networ|
00000110  6b 5f 65 78 69 73 74 73  28 29 2c 20 63 72 65 61  |k_exists(), crea|
00000120  74 69 6e 67 20 69 74 20  69 66 20 6e 65 63 65 73  |ting it if neces|
00000130  73 61 72 79 0a 20 20 73  65 74 75 70 5f 73 63 72  |sary.  setup_scr|
00000140  65 65 6e 28 29 0a 20 20  77 68 69 6c 65 20 6e 65  |een().  while ne|
00000150  74 77 6f 72 6b 20 77 65  69 67 68 74 73 20 6e 6f  |twork weights no|
00000160  74 20 61 64 65 71 75 61  74 65 6c 79 20 74 72 61  |t adequately tra|
00000170  69 6e 65 64 0a 20 20 20  20 67 65 74 5f 6e 65 74  |ined.    get_net|
00000180  77 6f 72 6b 5f 69 6e 70  75 74 5f 6f 75 74 70 75  |work_input_outpu|
00000190  74 28 29 2c 20 74 68 61  74 20 69 73 2c 20 67 65  |t(), that is, ge|
000001a0  74 20 61 20 73 61 6d 70  6c 65 20 69 6e 70 75 74  |t a sample input|
000001b0  0a 20 20 20 20 61 6e 64  20 63 6f 72 72 65 73 70  |.    and corresp|
000001c0  6f 6e 64 69 6e 67 20 64  65 73 69 72 65 64 20 6f  |onding desired o|
000001d0  75 74 70 75 74 0a 20 20  20 20 66 65 65 64 66 6f  |utput.    feedfo|
000001e0  72 77 61 72 64 28 29 2c  20 68 65 6e 63 65 20 63  |rward(), hence c|
000001f0  6f 6d 70 75 74 69 6e 67  20 61 63 74 75 61 6c 20  |omputing actual |
00000200  6f 75 74 70 75 74 0a 20  20 20 20 66 66 5f 65 72  |output.    ff_er|
00000210  72 6f 72 28 29 2c 20 63  61 6c 63 75 6c 61 74 69  |ror(), calculati|
00000220  6e 67 20 65 72 72 6f 72  20 62 65 74 77 65 65 6e  |ng error between|
00000230  20 64 65 73 69 72 65 64  20 61 6e 64 20 61 63 74  | desired and act|
00000240  75 61 6c 0a 20 20 20 20  69 66 20 65 72 72 6f 72  |ual.    if error|
00000250  20 74 6f 6f 20 62 69 67  20 74 68 65 6e 20 61 70  | too big then ap|
00000260  70 6c 79 20 62 61 63 6b  70 72 6f 70 61 67 61 74  |ply backpropagat|
00000270  69 6f 6e 28 29 20 74 6f  20 6d 6f 64 69 66 79 20  |ion() to modify |
00000280  77 65 69 67 68 74 73 0a  20 20 20 20 64 69 73 70  |weights.    disp|
00000290  6c 61 79 5f 72 65 73 75  6c 74 73 28 29 20 6f 66  |lay_results() of|
000002a0  20 70 72 6f 67 72 65 73  73 20 73 6f 20 66 61 72  | progress so far|
000002b0  0a 20 20 65 6e 64 77 68  69 6c 65 0a 0a 0a 0a 46  |.  endwhile....F|
000002c0  6c 6f 77 73 20 73 70 65  63 69 66 69 63 20 74 6f  |lows specific to|
000002d0  20 62 72 6f 6c 6c 79 20  61 70 70 6c 69 63 61 74  | brolly applicat|
000002e0  69 6f 6e 0a 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |ion.============|
000002f0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000300  3d 3d 3d 3d 3d 3d 3d 3d  0a 4f 75 72 20 67 65 74  |========.Our get|
00000310  5f 64 61 74 61 5f 70 72  6f 63 20 66 75 6e 63 74  |_data_proc funct|
00000320  69 6f 6e 2c 0a 62 72 6f  6c 6c 79 5f 64 79 6e 61  |ion,.brolly_dyna|
00000330  6d 69 63 73 5f 69 6e 70  75 74 28 76 65 63 74 6f  |mics_input(vecto|
00000340  72 20 78 5f 69 6e 2c 20  76 65 63 74 6f 72 20 64  |r x_in, vector d|
00000350  65 73 69 72 65 64 5f 6f  75 74 2c 20 66 66 5f 73  |esired_out, ff_s|
00000360  65 74 75 70 5f 72 65 63  20 2a 73 65 74 75 70 29  |etup_rec *setup)|
00000370  0a 64 6f 65 73 3a 0a 20  20 67 65 74 5f 72 61 6e  |.does:.  get_ran|
00000380  64 6f 6d 5f 62 72 6f 6c  6c 79 5f 73 74 61 74 65  |dom_brolly_state|
00000390  28 29 2c 20 74 68 61 74  20 69 73 2c 20 67 65 74  |(), that is, get|
000003a0  20 61 20 72 61 6e 64 6f  6d 20 27 69 6e 69 74 69  | a random 'initi|
000003b0  61 6c 20 73 74 61 74 65  27 0a 20 20 70 69 63 6b  |al state'.  pick|
000003c0  20 61 20 72 61 6e 64 6f  6d 20 27 66 6f 72 63 65  | a random 'force|
000003d0  27 0a 20 20 67 65 74 5f  6e 65 74 77 6f 72 6b 5f  |'.  get_network_|
000003e0  69 6e 70 75 74 28 29 2c  20 74 68 61 74 20 69 73  |input(), that is|
000003f0  2c 20 6e 6f 72 6d 61 6c  69 73 65 20 74 68 65 20  |, normalise the |
00000400  27 69 6e 69 74 69 61 6c  20 73 74 61 74 65 27 20  |'initial state' |
00000410  26 20 27 66 6f 72 63 65  27 2c 20 70 75 74 74 69  |& 'force', putti|
00000420  6e 67 20 72 65 73 75 6c  74 20 69 6e 20 78 5f 69  |ng result in x_i|
00000430  6e 0a 20 20 66 6f 72 63  65 5f 65 66 66 65 63 74  |n.  force_effect|
00000440  28 29 2c 20 74 68 61 74  20 69 73 2c 20 73 65 65  |(), that is, see|
00000450  20 68 6f 77 20 73 79 73  74 65 6d 20 65 76 6f 6c  | how system evol|
00000460  76 65 73 20 66 72 6f 6d  20 69 6e 69 74 69 61 6c  |ves from initial|
00000470  20 73 74 61 74 65 20 62  79 20 73 69 6d 75 6c 61  | state by simula|
00000480  74 69 6f 6e 0a 20 20 73  65 74 20 75 70 20 64 65  |tion.  set up de|
00000490  73 69 72 65 64 5f 6f 75  74 5b 5d 20 62 79 20 6e  |sired_out[] by n|
000004a0  6f 72 6d 61 6c 69 73 69  6e 67 20 73 69 6d 75 6c  |ormalising simul|
000004b0  61 74 65 64 20 61 63 74  75 61 6c 20 6f 75 74 70  |ated actual outp|
000004c0  75 74 0a 0a 44 75 72 69  6e 67 20 61 6e 20 61 63  |ut..During an ac|
000004d0  74 75 61 6c 20 67 61 6d  65 20 6f 75 72 20 74 72  |tual game our tr|
000004e0  61 69 6e 65 64 20 6e 65  74 20 69 73 20 75 73 65  |ained net is use|
000004f0  64 20 74 6f 20 63 6f 6e  74 72 6f 6c 20 74 68 65  |d to control the|
00000500  20 62 72 6f 6c 6c 79 20  76 69 61 2c 0a 75 6e 69  | brolly via,.uni|
00000510  74 20 6e 65 75 72 61 6c  5f 63 6f 6e 74 72 6f 6c  |t neural_control|
00000520  28 73 74 61 74 65 5f 72  65 63 20 2a 73 29 0a 77  |(state_rec *s).w|
00000530  68 69 63 68 20 64 6f 65  73 3a 0a 20 20 61 73 73  |hich does:.  ass|
00000540  75 6d 65 20 7a 65 72 6f  20 66 6f 72 63 65 0a 20  |ume zero force. |
00000550  20 67 65 74 5f 6e 65 74  77 6f 72 6b 5f 69 6e 70  | get_network_inp|
00000560  75 74 28 29 2c 20 74 68  61 74 20 69 73 2c 20 6e  |ut(), that is, n|
00000570  6f 72 6d 61 6c 69 73 65  20 73 79 73 74 65 6d 20  |ormalise system |
00000580  73 74 61 74 65 20 2a 73  20 26 20 7a 65 72 6f 20  |state *s & zero |
00000590  66 6f 72 63 65 0a 20 20  66 65 65 64 66 6f 72 77  |force.  feedforw|
000005a0  61 72 64 28 29 2c 20 74  68 61 74 20 69 73 2c 20  |ard(), that is, |
000005b0  61 6e 74 69 63 69 70 61  74 65 20 6e 6f 72 6d 61  |anticipate norma|
000005c0  6c 69 73 65 64 20 74 68  65 74 61 20 69 66 20 6e  |lised theta if n|
000005d0  6f 20 66 6f 72 63 65 20  61 70 70 6c 69 65 64 2c  |o force applied,|
000005e0  20 76 69 61 20 74 68 65  20 6e 65 74 77 6f 72 6b  | via the network|
000005f0  0a 20 20 73 65 74 20 6f  75 74 5f 61 6e 67 6c 65  |.  set out_angle|
00000600  20 3d 20 75 6e 6e 6f 72  6d 61 6c 69 73 65 64 20  | = unnormalised |
00000610  67 5f 6e 65 74 77 6f 72  6b 2e 79 5f 6f 75 74 5b  |g_network.y_out[|
00000620  31 5d 2c 20 74 68 61 74  20 69 73 2c 20 75 6e 6e  |1], that is, unn|
00000630  6f 72 6d 61 6c 69 73 65  20 74 68 65 74 61 0a 20  |ormalise theta. |
00000640  20 69 66 20 28 6f 75 74  5f 61 6e 67 6c 65 3e 67  | if (out_angle>g|
00000650  5f 62 72 6d 5f 73 65 74  75 70 2e 7a 65 72 6f 5f  |_brm_setup.zero_|
00000660  74 68 65 74 61 29 20 20  72 65 74 75 72 6e 20 20  |theta)  return  |
00000670  67 5f 62 72 6d 5f 73 65  74 75 70 2e 73 79 73 74  |g_brm_setup.syst|
00000680  65 6d 5f 66 6f 72 63 65  5f 69 6e 63 72 0a 20 20  |em_force_incr.  |
00000690  69 66 20 28 6f 75 74 5f  61 6e 67 6c 65 3c 2d 67  |if (out_angle<-g|
000006a0  5f 62 72 6d 5f 73 65 74  75 70 2e 7a 65 72 6f 5f  |_brm_setup.zero_|
000006b0  74 68 65 74 61 29 20 72  65 74 75 72 6e 20 2d 67  |theta) return -g|
000006c0  5f 62 72 6d 5f 73 65 74  75 70 2e 73 79 73 74 65  |_brm_setup.syste|
000006d0  6d 5f 66 6f 72 63 65 5f  69 6e 63 72 0a 20 20 65  |m_force_incr.  e|
000006e0  6c 73 65 20 72 65 74 75  72 6e 20 7a 65 72 6f 2c  |lse return zero,|
000006f0  20 74 68 61 74 20 69 73  2c 20 72 65 74 75 72 6e  | that is, return|
00000700  20 61 20 73 75 69 74 61  62 6c 65 20 6e 6f 6e 2d  | a suitable non-|
00000710  7a 65 72 6f 20 66 6f 72  63 65 20 69 66 20 61 6e  |zero force if an|
00000720  64 20 6f 6e 6c 79 20 69  66 20 61 6e 74 69 63 69  |d only if antici|
00000730  70 61 74 65 64 20 74 68  65 74 61 0a 20 20 69 73  |pated theta.  is|
00000740  20 62 69 67 20 65 6e 6f  75 67 68 0a 0a 0a 0a 54  | big enough....T|
00000750  79 70 69 63 61 6c 20 6f  76 65 72 61 6c 6c 20 66  |ypical overall f|
00000760  6c 6f 77 20 66 6f 72 20  61 20 6e 65 74 20 70 72  |low for a net pr|
00000770  6f 67 72 61 6d 0a 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |ogram.==========|
00000780  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000790  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 0a 6e 65 74  |============.net|
000007a0  77 6f 72 6b 5f 69 6e 69  74 69 61 6c 69 73 65 5f  |work_initialise_|
000007b0  70 61 72 61 6d 65 74 65  72 73 28 29 0a 63 72 65  |parameters().cre|
000007c0  61 74 65 5f 6d 61 78 5f  6d 69 6e 5f 64 61 74 61  |ate_max_min_data|
000007d0  28 29 0a 69 6e 69 74 69  61 6c 69 73 65 5f 77 65  |().initialise_we|
000007e0  69 67 68 74 73 28 29 20  72 61 6e 64 6f 6d 6c 79  |ights() randomly|
000007f0  0a 62 61 63 6b 70 72 6f  70 61 67 61 74 69 6f 6e  |.backpropagation|
00000800  5f 74 72 61 69 6e 69 6e  67 28 29 0a 77 68 69 6c  |_training().whil|
00000810  65 20 73 6f 6d 65 74 68  69 6e 67 20 6f 72 20 6f  |e something or o|
00000820  74 68 65 72 0a 20 20 64  6f 20 73 74 75 66 66 0a  |ther.  do stuff.|
00000830  20 20 66 65 65 64 66 6f  72 77 61 72 64 28 29 0a  |  feedforward().|
00000840  20 20 74 68 65 6e 20 6d  6f 72 65 20 73 74 75 66  |  then more stuf|
00000850  66 2c 20 75 73 69 6e 67  20 6e 65 74 20 6f 75 74  |f, using net out|
00000860  70 75 74 20 61 73 20 64  65 73 69 72 65 64 0a 65  |put as desired.e|
00000870  6e 64 77 68 69 6c 65 0a                           |ndwhile.|
00000878