; Steven Wojciechowski ; Neural Networks Assessment 1 ; learn_rate.f = 0.1 ; learning rate is constant bias.f = 1 ; as is the bias num_axons.w = 3 ; 3 axons required (inc 1 for bias) num_inputs.w = 4 ; 4 input patterns num_neurons.w = 4 ; 4 neurons NEWTYPE .weight value.f ; value of the weight node1.w ; source node number node2.w ; dest node (both of WORD type) End NEWTYPE NEWTYPE .node strength.f ; strength of node logic_out.w ; logical output (ie -1 or 1) when necessary End NEWTYPE NEWTYPE .dataset input1.f ; input for node 1 input2.f ; node 2 output.w ; logical output value End NEWTYPE Dim inputs.dataset(num_inputs-1) ; array for logic table type DATASET Dim neuron.node(num_neurons-1) ; array of neurons type NODE Dim axon.weight(num_axons-1) ; the axons type WEIGHT ; connect neuron with axons axon(0)\node1 = 0 axon(0)\node2 = 3 ; neuron 0 (bias) connected to neuron 3 (output) axon(1)\node1 = 1 axon(1)\node2 = 3 ; neuron 1 (node 1) connected to neuron 3 axon(2)\node1 = 2 axon(2)\node2 = 3 ; neuron 2 (node 2) connected to neuron 3 ;set the bias neuron neuron(0)\strength = bias Statement set_weights{} ; statement is the same as procedure SHARED axon() ; SHARED make axons global to this statement SHARED num_axons.w ; also make required variable global RRandomize Ticks ; set seed for random function (in built) ; "Ticks" returns the time since the computer was turned on (50ths of a second) For x = 0 To (num_axons - 1) axon(x)\value = RRnd(0,10)/10 ; set axon weight with a value from 0 to 1 Next ; For x = 0 To (num_axons - 1) ; Print "Enter Weight for neuron ",x,": " ; axon(x)\value = Edit(4) ; take input of float (max 4 digits) ; Next End Statement Statement set_and {} ; set the AND logic table SHARED inputs() inputs(0)\input1 = -1.0, -1.0, -1 ; the 2nd 2 values will cascade into inputs(1)\input1 = -1.0, 1.0, -1 ; the input2 and output values inputs(2)\input1 = 1.0, -1.0, -1 inputs(3)\input1 = 1.0, 1.0, 1 End Statement Statement set_or {} ; set the OR logic table SHARED inputs() inputs(0)\input1 = -1.0, -1.0, -1 inputs(1)\input1 = -1.0, 1.0, 1 inputs(2)\input1 = 1.0, -1.0, 1 inputs(3)\input1 = 1.0, 1.0, 1 End Statement Statement set_xor {} ; set the XOR logic table SHARED inputs() inputs(0)\input1 = -1.0, -1.0, -1 inputs(1)\input1 = -1.0, 1.0, 1 inputs(2)\input1 = 1.0, -1.0, 1 inputs(3)\input1 = 1.0, 1.0, -1 End Statement Function feed_forward {} ; Calculate the output of the network SHARED axon(), neuron() ; the feed forward formula temp.f = (axon(0)\value * neuron(axon(0)\node1)\strength) + (axon(1)\value * neuron(axon(1)\node1)\strength) neuron(3)\strength = temp + (axon(2)\value * neuron(axon(2)\node1)\strength) If neuron(3)\strength > 0 neuron(3)\logic_out = 1 ; output >0 so logically 1 Else neuron(3)\logic_out = -1 ; <= 0 so logically -1 End If Function Return neuron(3)\logic_out End Function Statement change_weights{difference.w, rate.f} ; difference is the difference between target and actual outputs SHARED axon(), neuron() axon(0)\value = axon(0)\value + rate * (difference) * neuron(axon(0)\node1)\strength axon(1)\value = axon(1)\value + rate * (difference) * neuron(axon(1)\node1)\strength axon(2)\value = axon(2)\value + rate * (difference) * neuron(axon(2)\node1)\strength End Statement ; Main section ;set local variables choice$ = "?" ; set the variable CHOICE as a string, defaulting to "?" log_table$ = "?" ; set log_table as string with default "?" next_pattern.w = 0 ; next_pattern variable temp_output.w = 0 ; temp handling variable for output NPrint "" : NPrint "" NPrint "Select an option: (1) AND (2) OR (3) XOR (4) Quit" choice$ = Edit$(1) ; read in a single character While choice$ <> "4" ; check for quit option Select choice$ Case "1" set_and{} ; call set_and statement without parameters log_table$ = "AND" Case "2" set_or{} log_table$ = "OR" Case "3" set_xor{} log_table$ = "XOR" Default NPrint "Invalid option, quitting" Delay_ (2) ; wait before quitting out End End Select NPrint "Setting random weights" set_weights{} ; call the set_weights statement NPrint "" NPrint "Weight settings are:" NPrint "Bias weight: ", axon(0)\value, " Neuron 1 weight: ", axon(1)\value, " Neuron 2 weight: ", axon(2)\value Repeat ; start a loop NPrint "" Print "select a pattern (0-3, 9 to change table):" next_pattern = Edit(1) ; read a single digit number If next_pattern < 4 neuron(1)\strength = inputs(next_pattern)\input1 neuron(2)\strength = inputs(next_pattern)\input2 temp_output = feed_forward {} ; call the fee_forward NPrint "Neuron 1: ", neuron(1)\strength, " Neuron 2: ", neuron(2)\strength NPrint "Target output: ",inputs(next_pattern)\output, " Actual output:", temp_output If temp_output = inputs(next_pattern)\output NPrint "OUTPUTS MATCH. Weights remain the same." NPrint "Bias weight: ", axon(0)\value, " Neuron 1 weight: ", axon(1)\value, " Neuron 2 weight: ", axon(2)\value Else NPrint "OUTPUTS DON'T MATCH. Changing weights." change_weights{inputs(next_pattern)\output - temp_output, learn_rate} NPrint "Bias weight: ", axon(0)\value, " Neuron 1 weight: ", axon(1)\value, " Neuron 2 weight: ", axon(2)\value End If End If Until next_pattern = 9 NPrint "" NPrint "Select an option: (1) AND (2) OR (3) XOR (4) Quit" choice$ = Edit$(1) ; read in a single character Wend ; While END End