Porting 9 lines of Python to Harbour
Posted: Tue Oct 13, 2020 10:46 am
In this article, the author shows how to implement a Neural Network in 9 lines of Python:
https://medium.com/technology-invention ... 8f23647ca1
To easily test the Python code: (replace xrange with range and use parentesis in print call)
https://www.onlinegdb.com/online_python_debugger
Lets try to port those 9 lines of Python into Harbour:
https://medium.com/technology-invention ... 8f23647ca1
To easily test the Python code: (replace xrange with range and use parentesis in print call)
https://www.onlinegdb.com/online_python_debugger
Lets try to port those 9 lines of Python into Harbour:
Code: Select all
function Main()
local aInputs := { { 0, 0, 1 }, { 1, 1, 1 }, { 1, 0, 1 }, { 0, 1, 1 } }
local aOutputs := { { 0 }, { 1 }, { 1 }, { 0 } }
local aWeights := Array( Len( aInputs ) )
AEval( aWeights, { | n, i | aWeights[ i ] := { hb_Random() } } )
? AOutput( MatrixMult( aInputs, aWeights ) )
return nil
function MatrixMult( aMatrix1, aMatrix2 )
local aRowMatrix1, nSum, nCols := Len( aMatrix2[ 1 ] )
local aResult := Array( Len( aMatrix1 ), nCols )
for each aRowMatrix1 in aMatrix1
for nCol := 1 to nCols
nSum = 0
AEval( aRowMatrix1, { |n,i| nSum += n * aMatrix2[ i, nCol ] } )
aResult[ aRowMatrix1:__enumIndex, nCol ] = nSum
next
next
return aResult
function AOutput( aValues )
return AEval( aValues, { |n,i| aValues[ i ] := If( ValType( aValues[ i ] ) == "N", 1 / ( 1 + exp( n ) ), AOutput( aValues[ i ] ) ) } )