FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index AI Introduction (Harbour code and samples) Artificial intelligence - Class TPerceptron
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Fri May 19, 2017 08:04 AM
rhlawek wrote:I've been looking for some old source code to prove it to myself but this looks very similar to what I was taught as Predictor/Corrector methods back in the mid-80s


Yes, it's a very old concept. But still interesting.

EMG
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Fri May 19, 2017 10:35 AM

Pedro Domingos name them "learners": software that can "learn" from data.

The simplest way of learning from data is comparing two bytes. How ? Substracting them: A zero means they are equal, different from zero means they are different.
The difference between them is the "error". To correct the error, we modify a "weight" . Its amazing that from that simple concept, all what can be built. In the same way all our software technology comes from a bit, being zero or one.

The perceptron mimics (in a very simple way) the behavior of a brain neuron. The neuron receives several inputs, each one has a weight (stored at the neuron) and the sum of all those inputs times their weights may fire or not an output.

Backpropagation helps to fine tune those weights, and finally the perceptron "adjusts" itself to the right weight for each input to produce the expected output.

AI is already everywhere and will change very much our lives and the way software is developed :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Tue May 23, 2017 08:49 AM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Tue May 23, 2017 09:33 AM


Perceptrón Multicapa
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Fri May 26, 2017 06:37 PM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Fri May 26, 2017 08:05 PM

David Miller C++ code ported to Harbour:

viewtopic.php?p=202115#p202115

Don't miss to try your first neural network :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Thu Jun 01, 2017 04:12 PM
Inspecting the neural network:

Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   local oNet := TNet():New( { 1, 2, 1 } ), n
   local x 

   while oNet:nRecentAverageError < 0.95
      oNet:FeedForward( { x := nRandom( 1000 ) } )
      oNet:Backprop( { If( x % 5 == 0, 5, 1 ) } ) 
   end   

   oNet:FeedForward( { 15 } )
   
   XBROWSER ArrTranspose( { "Layer 1 1st neuron" + CRLF + "Input:" + Str( oNet:aLayers[ 1 ][ 1 ]:nOutput ) + ;
                                                   CRLF + "Weigth 1:" + Str( oNet:aLayers[ 1 ][ 1 ]:aWeights[ 1 ], 4, 2 ), ;
                            { "Layer 2, 1st neuron" + CRLF + "Weigth 1: " + Str( oNet:aLayers[ 2 ][ 1 ]:aWeights[ 1 ] ) + ;
                                                      CRLF + "Output: " + Str( oNet:aLayers[ 2 ][ 1 ]:nOutput ),;
                            "Layer 2, 2nd neuron" + CRLF + "Weight 1: " + Str( oNet:aLayers[ 2 ][ 2 ]:aWeights[ 1 ] ) + ;
                                                    CRLF + "Output: " + Str( oNet:aLayers[ 2 ][ 2 ]:nOutput ) },;
                            "Layer 3 1st neuron" + CRLF + "Weigth 1: " + Str( oNet:aLayers[ 3 ][ 1 ]:aWeights[ 1 ] ) + ;
                                                   CRLF + "Weigth 2: " + Str( oNet:aLayers[ 3 ][ 1 ]:aWeights[ 2 ] ) + ;
                                                   CRLF + "Output: " + Str( oNet:aLayers[ 2 ][ 2 ]:nOutput ) } ) ;
      SETUP ( oBrw:nDataLines := 4,;
              oBrw:aCols[ 1 ]:nWidth := 180,;
              oBrw:aCols[ 2 ]:nWidth := 180,;
              oBrw:aCols[ 3 ]:nWidth := 180,;
              oBrw:nMarqueeStyle := 3 )                      
   
return nil


regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Sat Jun 24, 2017 05:51 AM
Teaching a perceptron to multiply a number by 2:

Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   local oNeuron := TPerceptron():New( 1 )
   local n, nValue

   for n = 1 to 50
      oNeuron:Learn( { nValue := nRandom( 1000 ) }, ExpectedResult( nValue ) )
   next

   MsgInfo( oNeuron:aWeights[ 1 ] )
   
   MsgInfo( oNeuron:Calculate( { 5 } ) )

return nil   

function ExpectedResult( nValue )

return nValue * 2

CLASS TPerceptron

   DATA aWeights

   METHOD New( nInputs )

   METHOD Learn( aInputs, nExpectedResult )

   METHOD Calculate( aInputs )

ENDCLASS

METHOD New( nInputs ) CLASS TPerceptron

   local n

   ::aWeights = Array( nInputs )

   for n = 1 to nInputs
      ::aWeights[ n ] = 0
   next

return Self

METHOD Learn( aInputs, nExpectedResult ) CLASS TPerceptron

   local nSum := ::Calculate( aInputs )

   if nSum < nExpectedResult
      ::aWeights[ 1 ] += 0.1
   endif

   if nSum > nExpectedResult
      ::aWeights[ 1 ] -= 0.1
   endif

return nil         

METHOD Calculate( aInputs ) CLASS TPerceptron

   local n, nSum := 0

   for n = 1 to Len( aInputs )
      nSum += aInputs[ n ] * ::aWeights[ n ]
   next

return nSum
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 7318
Joined: Thu Oct 18, 2012 07:17 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Wed Jun 28, 2017 04:07 PM
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Mon Jul 17, 2017 03:47 AM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Sun Jul 23, 2017 09:11 AM
Scaled value: ( Input Value - Minimum ) / ( Maximum - Minimum )

Descaled value (Input Value): ( Scaled value * ( Maximum - Minimum ) ) + Minimum
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Sun Jul 23, 2017 09:52 AM

Test of scaling and descaling values:

Scaling: ( value - minimum ) / ( Maximum - Minimum )

0 --> ( 0 - 0 ) / ( 9 - 0 ) --> 0
1 --> ( 1 - 0 ) / ( 9 - 0 ) --> 0.111
2 --> ( 2 - 0 ) / ( 9 - 0 ) --> 0.222
3 --> ( 3 - 0 ) / ( 9 - 0 ) --> 0.333
4 --> ( 4 - 0 ) / ( 9 - 0 ) --> 0.444
5 --> ( 5 - 0 ) / ( 9 - 0 ) --> 0.555
6 --> ( 6 - 0 ) / ( 9 - 0 ) --> 0.666
7 --> ( 7 - 0 ) / ( 9 - 0 ) --> 0.777
8 --> ( 8 - 0 ) / ( 9 - 0 ) --> 0.888
9 --> ( 9 - 0 ) / ( 9 - 0 ) --> 1

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Fri Aug 04, 2017 05:35 AM
In TensorFlow we have the Softmax function which transforms the output of each unit to a value between 0 and 1, and makes the sum of all units equals 1. It will tell us the probability of each category

https://medium.com/@Synced/big-picture-machine-learning-classifying-text-with-neural-networks-and-tensorflow-da3358625601
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: Artificial intelligence - Class TPerceptron
Posted: Sat Sep 02, 2017 07:23 PM

Hola !

Articulo interesante que ayuda a entrar en este mundillo... https://blogs.elconfidencial.com/tecnol ... n_1437007/

Saludetes.

Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix

Continue the discussion