Easy matrix

New to FreeBASIC? Post your questions here.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Easy matrix

Post by fxm »

dodicat wrote: Aug 07, 2022 10:50 fxm has the patience of a saint, ...
No
dodicat wrote: Aug 07, 2022 10:50 ... but maybe he did the same as me, run easy_matx.bas plus your test.
Yes.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Easy matrix

Post by fxm »

Also for fun, my own version where a matrix is defined as an array of vectors of the same size.
This version with two Types (Vector and Matrix) allows to define an operator '[]' in each one, inducing a syntax of access to the elements of a vector ('V') or a matrix ('M') as follows (the arrays themselves being private):
'V[n]' : allows access to the elements of the vector 'V' in read/write mode.
'M[n1][n2]' : allows access to the elements of the matrix 'M' in read/write mode.

Code: Select all

Type Vector
    Public:
        Declare Constructor()
        Declare Constructor(Byval n As Uinteger)
        Declare Operator [](Byval n As Uinteger) Byref As Double
        Declare Property UpperBound() As Integer
    Private:
        Dim As Double array(Any)
End Type

Constructor Vector()
End Constructor

Constructor Vector(Byval n As Uinteger)
    If n > 0 Then
        Redim This.array(n - 1)
    End If
End Constructor

Operator Vector.[](Byval n As Uinteger) Byref As Double
    Return This.array(n)
End Operator

Property Vector.UpperBound() As Integer
    Return Ubound(This.array)
End Property

Type Matrix
    Public:
        Declare Constructor()
        Declare Constructor(Byval n1 As Uinteger, Byval n2 As Uinteger)
        Declare Operator [](Byval n As Uinteger) Byref As Vector
        Declare Property UpperBound1() As Integer
        Declare Property UpperBound2() As Integer
    Private:
        Dim As Vector array(Any)
End Type

Constructor Matrix()
End Constructor

Constructor Matrix(Byval n1 As Uinteger, Byval n2 As Uinteger)
    If n1 > 0 And n2 > 0 Then
        Redim This.array(n1 - 1)
        For i As Integer = 0 To n1 - 1
            This.array(i) = Vector(n2)
        Next I
    End If
End Constructor

Operator Matrix.[](Byval n As Uinteger) Byref As Vector
    Return This.array(n)
End Operator

Property Matrix.UpperBound1() As Integer
    Return Ubound(This.array)
End Property

Property Matrix.UpperBound2() As Integer
    If Ubound(This.array) >= 0 Then
        Return This.array(0).UpperBound
    Else
        Return -1
    End If
End Property

Operator *(Byref M As Matrix, Byref V As Vector) As Vector
    If M.UpperBound2 = V.UpperBound Then
        Dim As Vector R = Vector(M.UpperBound1 + 1)
        For i As Integer = 0 To M.UpperBound1
            For k As Integer = 0 To V.UpperBound
                R[i] += M[i][k] * V[k]
            Next k
        Next i
        Return R
    Else
        Error 1
    End If
End Operator

Operator *(Byref M1 As Matrix, Byref M2 As Matrix) As Matrix
    If M1.UpperBound2 = M2.UpperBound1 Then
        Dim As Matrix R = Matrix(M1.UpperBound1 + 1, M2.UpperBound2 + 1)
        For i As Integer = 0 To M1.UpperBound1
            For j As Integer = 0 To M2.UpperBound2
                For k As Integer = 0 To M1.UpperBound2
                    R[i][j] += M1[i][k] * M2[k][j]
                Next k
            Next j
        Next i
        Return R
    Else
        Error 1
    End If
End Operator

'-----------------------------------------------------------------------------

Dim As Vector V = Vector(3)
V[0] = 1
V[1] = 2
V[2] = 3
For i As Integer = 0 To V.UpperBound
    Print V[i]
Next i
Print

Dim As Matrix M1 = Matrix(3, 2)
M1[0][0]= 1 : M1[0][1] = 2
M1[1][0]= 3 : M1[1][1] = 4
M1[2][0]= 5 : M1[2][1] = 6
For i As Integer = 0 To M1.UpperBound1
    For j As Integer = 0 To M1.UpperBound2
        Print M1[i][j]; " ";
    Next j
    Print
Next i
Print

Dim As Matrix M2 = Matrix(2, 3)
M2[0][0]= 1 : M2[0][1] = 2 : M2[0][2] = 3
M2[1][0]= 4 : M2[1][1] = 5 : M2[1][2] = 6
For i As Integer = 0 To M2.UpperBound1
    For j As Integer = 0 To M2.UpperBound2
        Print M2[i][j]; " ";
    Next j
    Print
Next i
Print

Dim As Matrix Mx = M1 * M2
For i As Integer = 0 To Mx.UpperBound1
    For j As Integer = 0 To Mx.UpperBound2
        Print Mx[i][j]; " ";
    Next j
    Print
Next i
Print

Dim As Vector Vx = Mx * V
For i As Integer = 0 To Vx.UpperBound
    Print Vx[i]
Next i
Print

Sleep
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

Thank you for your comments, I'm not always able to immediately action all of your suggestions;
also there may be a delay of 12 to 24 hours before I reply.

You don't need to construct a library from this code, mostly I wanted to see if it was possible to do so,
looks tidier too.

The compilation with the -exx option is something new to me, with this option enabled I'm going to
examine all of the code again.
If you're using a single column matrix, rather than, or as a vector; I'd like to know how you do that within
FreeBasic.

In reply to the m_seq() issue, this is my reworked code.

Code: Select all



'
'  matx_smv.bas
'
'   Successive matrix vector multiplications .
'

'' compile with: fbc matx_test.bas


#include once "easy_matx.bi"

'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
'    Matrix of matrices
'
dim as Matrix z(1 to lms)
'
'   Vector of vectors
'
dim as Vector u(0 to lms)
'
'   Dimension matrices and vectors .
'
for i=0 to lms-1
   nx=m_seq(i)
   ny=m_seq(i+1)
   z(i+1) = Matrix(ny , nx)
   u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
next i
'
'  ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
print " Input vector, a matrix in another version ? "
print
prtv_v(u(), 0)
'
print   " matrix of matrices values "
print
'
for i=1 to lms
    gen_z(z() , i)
    prt_z(z() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(i) = z(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms 
   print " z(";i;") "
   prt_z(z() , i)
   u(i) = z(i)*u(i-1)
   print " u(";i;") "
   prtv_v(u(), i)   
next i


'  end command acts as global destructor 
' of allocated memory ?

end
'
' ======================================================================
'

Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

A little while ago, I rewrote some of the code, resurrected this as a large file and checked everything
whilst using #cmdline "-exx" at the start of all of the code.

No error messages were produced.

Here is the most recent code, hope the message system can handle this.

Code: Select all


'
'  matx_lrg.bas
'
'   Successive matrix vector multiplications .
'

'' compile with: fbc matx_lrg.bas




#cmdline "-exx"

type Matrix
    dim as double m( any , any )
    declare constructor ( )
    declare constructor ( byval x as uinteger , byval y as uinteger )
end type
'
type Vector
dim as double v( any )
    declare constructor ( )
    declare constructor ( byval x as uinteger )
end type

'
'
' ---------------------------- operators -------------------------------
'
declare operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator * ( byref a as Matrix , byref b as Vector ) as Vector

declare operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator - ( byref a as Vector , byref b as Vector ) as Vector

declare operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator + ( byref a as Vector , byref b as Vector ) as Vector

declare operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator / ( byref a as Vector , byref b as Vector ) as Vector

'   The rest from luxan, sciwiseg@gmail.com
'
'  Probably need to make this content
'  available under GNU Free Documentation License 1.2 
'
'
' ............................. procedures .............................
'
declare sub gen_z(z() as Matrix, idx as Integer)
declare sub gen_v(u() as Vector, idx as Integer)

declare sub prtv_v(u() as Vector, idx as Integer)

declare sub prt_z(z() as Matrix, idx as Integer)
declare sub prt_m(z as Matrix)
declare sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)

declare sub prt_v(z as Vector)
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
'    Matrix of matrices
'
dim as Matrix z(1 to lms)
'
'   Vector of vectors
'
dim as Vector u(0 to lms)
'
'   Dimension matrices and vectors .
'
for i=0 to lms-1
   nx=m_seq(i)
   ny=m_seq(i+1)
   z(i+1) = Matrix(ny , nx)
   u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
next i
'
'  ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
print " Input vector, a matrix in another version ? "
print
prtv_v(u(), 0)
'
print   " matrix of matrices values "
print
'
for i=1 to lms
    gen_z(z() , i)
    prt_z(z() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(i) = z(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms 
   print " z(";i;") "
   prt_z(z() , i)
   u(i) = z(i)*u(i-1)
   print " u(";i;") "
   prtv_v(u(), i)   
next i
'
'  end command acts as global destructor 
' of allocated memory ?
'
' With -exx option, no error messages, with or without end command.
'
end
'
' ======================================================================
'
'   Matrix math
'
'  1. dim as Matrix a
'  2. redim a.m( x1 to x2 , y1 to y2 )
'
'  3. dim as Matrix z(z1 to z2)  ?
'  4. redim z.m(w1 to w2)  ?
'
'     Matrix(x , y) ~= redim (0 to x-1, 0 to y-1) ?


' These declarations from :
'  http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
' That content is available under
'  Content is available under GNU Free Documentation License 1.2 unless otherwise noted.
'
'   Using Geany, Build, Set Build Commands
'
'   Compile                fbc -w all "%f" 
'
'   Execute                "./%e" 
'
' Preliminary results checked using Maxima CAS
'
' also at, especially for vector by matrix calculations.
' https://keisan.casio.com/exec/system/15052033860538
'
' https://matrix.reshish.com/multCalculation.php
'
' https://elsenaju.eu/Calculator/matrix-vector-product.htm
'
'
'type Matrix
'    dim as double m( any , any )
'    declare constructor ( )
'    declare constructor ( byval x as uinteger , byval y as uinteger )
'end type
 
constructor Matrix ( )
end constructor
 
constructor Matrix ( byval x as uinteger , byval y as uinteger )
    redim this.m( x - 1 , y - 1 )
end constructor


'type Vector
'dim as double v( any )
'    declare constructor ( )
'    declare constructor ( byval x as uinteger )
'end type

constructor Vector ( )
end constructor

constructor Vector ( byval x as uinteger )
    redim this.v( x - 1 )
end constructor
'
' ====================================================================
'
Public operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
'  Only applicable to square matrices or arrays.
' This routine from :
'  http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
'
' Dodicat suggests only using: if ubound( a.m , 2 ) = ubound( b.m , 1 )
'
'
    dim as Matrix ret
    dim as uinteger i, j, k
    'if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
     if ubound( a.m , 2 ) = ubound( b.m , 1 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                for k = 0 to ubound( b.m , 1 )
                    ret.m( i , j ) += a.m( i , k ) * b.m( k , j )
                next k
            next j
        next i
    end if
    return ret
end operator
'
'
Public operator * ( byref a as Matrix , byref b as Vector ) as Vector
'
'   Vector multiplied by a matrix, maybe correct .
'
'
   dim as Vector ret
   dim as uinteger i, j, k
   if ubound( a.m , 2 ) = ubound( b.v , 1 )  then
        redim ret.v( ubound( a.m , 1 ) )
        for i = 0 to ubound( a.m , 1 )
            
                for k = 0 to ubound( b.v , 1 )
                    ret.v( i ) += a.m( i , k ) * b.v( k  )
                next k
            
        next i
    end if   
   return ret
end operator
'
Public operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                ret.m( i , j ) = a.m( i , j ) - b.m( i , j )
            next j
        next i
    end if
    return ret
end operator
'
Public operator - ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 )  )
        for i = 0 to ubound( a.v , 1 )
            ret.v( i  ) = a.v( i  ) - b.v( i  )
        next i
    end if
    return ret
end operator
 '
Public operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
               ret.m( i , j ) = a.m( i , j ) + b.m( i , j )
            next j
        next i
    end if
    return ret
end operator
'
Public operator + ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 )  )
        for i = 0 to ubound( a.v , 1 )
            ret.v( i  ) = a.v( i  ) + b.v( i  )
        next i
    end if
    return ret
end operator
'
Public operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    dim as double x, y, z
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                x = a.m( i , j )
                y = b.m( i , j )
                y = sgn(x)*10^32 ' assume y = 0
             if y <> 0 then z = x/y
                ret.m( i , j ) = z
            next j
        next i
    end if
    return ret
end operator
'
Public operator / ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    dim as double x, y, z
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 ) )
        for i = 0 to ubound( a.v , 1 )
                x = a.v( i )
                y = b.v( i )
                y = sgn(x)*10^32 ' assume y = 0
             if y <> 0 then z = x/y
                ret.v( i  ) = z
        next i
    end if
    return ret
end operator
'
' ......................................................................
'
Public sub prt_z(z() as Matrix, idx as Integer)
'
'     Matrix of Matrix, index and print .
'
dim as integer i,j
'
'print
for i=0 to ubound(z(idx).m,1)
 for j=0 to ubound(z(idx).m,2) 
    print Using "###.####";z(idx).m(i,j);" ";
  next j
  print
next i
print
'  
end sub
'
' ......................................................................
'
Public sub prt_m(z as Matrix)
'
'  Print elements from rectangular matrix .
'
dim as integer i,j
'

print
print ubound(z.m,1);" ";ubound(z.m,2)
print
 for i=0 to ubound(z.m,1) 
  for j=0 to ubound(z.m,2)  
    print Using "###.####";z.m(i,j);" ";
  next j
  print
next i
print
'  
end sub
'
' ......................................................................
'
Public sub prt_v(z as Vector)
'
'  Print elements from vector .
'
dim as integer i
'
'print
for i=0 to ubound(z.v,1)
    
    print Using "###.####";z.v(i);" ";
  
  
next i
print
print
'  
end sub
'
' ......................................................................
'
Public sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
'
'     Multiply vector by matrix .
'
dim as integer i,j,ub1, ub2
dim as single x,y,s
    
    ub1=ubound(M1.m,1)
    ub2=ubound(M1.m,2)
'    
    redim V3.v(0 to ub1)
'
        for j=0 to ub1
             s=0
         for i=0 to ub2
             x=V1.v(i)
             y=M1.m(j,i)
             s=s+x*y
         next i
          V3.v(j)=s
      next j
 '
end sub
'
' ......................................................................
'
Public sub gen_z(z() as Matrix, idx as Integer)
'
'     Matrix of Matrix, index and generate values .
'
dim as integer i,j
'
Randomize
  for j=0 to ubound(z(idx).m,2) 
   for i=0 to ubound(z(idx).m,1)
      z(idx).m(i,j) = -1 + 2*rnd
  next i
next j

'  
end sub
'
' ......................................................................
'
Public sub gen_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and generate values .
'
dim as integer i,j
Randomize
'
for i=0 to ubound(u(idx).v,1)
   
      u(idx).v(i) = -1 + 2*rnd
  
next i

'  
end sub
'
' ......................................................................
'
Public sub prtv_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and print values .
'
dim as integer i,j
'
for i=0 to ubound(u(idx).v,1)
   print Using "###.####";u(idx).v(i)
next i
print 
'  
end sub
'
' ......................................................................
'



Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

Now I search for ways to use this structure.
How do I assign values to the Matrix type .
For an array, this is possible :

dim Xa(0 to 5,0 to 2) as integer => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}

or this

dim ya(0 to 5) as integer => {(0),(1),(0),(1),(1),(0)}

however, this isn't possible
ux=5
dim ya(0 to ux) as integer => {(0),(1),(0),(1),(1),(0)}
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Easy matrix

Post by fxm »

Only fix-len arrays can be initialized this way.

To construct your matrix, you can use another constructor with a 2D array as parameter, and construct the matrix by passing a 2D fix-len array initialized as desired:

Code: Select all

type Matrix
    dim as double m( any , any )
    declare constructor ( )
    declare constructor ( array( any, any ) as double )
end type

constructor Matrix ( )
end constructor

constructor Matrix ( array( any, any ) as double )
    if ubound(array, 2) > = 0 and lbound(array, 1) = 0 and lbound(array, 2) = 0 Then  '' passed array non-empty with all indexes 0 based
        redim m(ubound(array, 1), ubound(array, 2))
        fb_memcopy(m(0, 0), array(0, 0), (ubound(array, 1) + 1) * (ubound(array, 2) + 1) * sizeof(double)) '' shallow copy because DOUBLE type
    end if
end constructor

'------------------------------------------------------------------------------

dim X(0 to 5,0 to 2) as double => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
dim M as Matrix = Matrix(X())

'------------------------------------------------------------------------------

for I as integer = 0 to ubound(M.m, 1)
    for J as integer = 0 to ubound(M.m, 2)
        print M.m(I, J),
    next J
    print
next I

sleep

Similar principle for assignment:
'M = Matrix(X())'
Note:
We can not do a direct assignment ('M = X()') because the operator 'let' does not support an array as parameter for the assignment syntax).
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Easy matrix

Post by dodicat »

You can use my initmatrix from a previous post, using 0 to instead of 1 to for zero based arrays.
Here is another way using a macro

Code: Select all

#cmdline "-exx"
Type Matrix
    As Double e(any,any)
End Type

sub show(m as matrix)
    Dim As String ans,comma
     for r as long=0 to ubound(m.e,1)
        for c as long=0 to ubound(m.e,2)
            if c=0 then print "[";
             If c=Ubound(m.e,2) Then comma="" Else comma=","
            print str(m.e(r,c));comma;
            if c=ubound(m.e,2) then print "]";
        next c
        print
    next r
    end sub


#macro setmatrix(m,r,c,d...) 
#macro spl()
s+=","
For n As Long=0 To Len(s)-1
    t+=Chr(s[n])
    If s[n]=44 Then
        Redim Preserve db(Ubound(db)+1)
        db(Ubound(db))=Val(t)
        t=""
    End If
Next n
#endmacro
Scope
    Dim As String t
    Redim As Double db()
    Dim As Long count
    Var s=#d
    Redim m.e(0 To r,0 To c)
    spl()
    For r1 As Long=0 To r
        For c1 As Long=0 To c
            m.e(r1,c1)= db(count)
            count+=1
        Next
    Next
End Scope
#endmacro 

 dim as matrix m
 
    dim as long r=5
    dim as long c=2
    
    setmatrix(m,r,c,   0,0,1,_
                       0,1,1,_
                       1,0,0,_
                       1,1,0,_
                       1,0,1,_
                       1,1,1)
    show(m)
     print                  
   
    setmatrix(m,2,2,  4,6.6,7,_
                      1,-7,4,_
                      .05,23,2)
        
    show(m)
    print
    setmatrix(m,3,0,  8,_
                      7,_
                      -6,_
                      5.5)
    show(m)
    print
    setmatrix(m,0,3,  8,7,-6,5.5)
                      
        show(m)
           
    sleep
                       
 
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

Thanks dodicat, your setmatrix may well be what I require.

Now in the projects, neural network section, I came across, and ran; a Basic program called 'Flappy bird.

This prompted me to find a way to understand neural networks, using matrices and vectors.
The ability to have a matrix of matrices was necessary, this has been mostly resolved.

Looking at the literature, on the internet, there are a lot of diverse interpretations and
approaches, many carry historical baggage in how the programs are constructed.

One almost comprehensible article, that I'm attempting to decipher is here:

https://page.mi.fu-berlin.de/rojas/neur ... ter/K7.pdf

He has included some python code for his, small, neural network, I'm attempting to
translate this to Basic; however the structure and numpy specific routines, make this
difficult.

and more extensively, at this location

https://page.mi.fu-berlin.de/rojas/neural/neuron.pdf


As for a general forward propagation, using my structure, I think that I have this sorted.

Code: Select all

' easy_matx.bas

' On my computer
' cd Downloads/fb/matrices_x/dodicat/merge_1/lib
'

' ' compile with: fbc -lib easy_matx.bas

'   Matrix math
'
'  1. dim as Matrix a
'  2. redim a.m( x1 to x2 , y1 to y2 )
'
'  3. dim as Matrix z(z1 to z2)  ?
'  4. redim z.m(w1 to w2)  ?
'
'     Matrix(x , y) ~= redim (0 to x-1, 0 to y-1) ?


' These declarations from :
'  http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
' That content is available under
'  Content is available under GNU Free Documentation License 1.2 unless otherwise noted.
'
'   Using Geany, Build, Set Build Commands
'
'   Compile                fbc -w all "%f" 
'
'   Execute                "./%e" 
'
' Preliminary results checked using Maxima CAS
'
' also at, especially for vector by matrix calculations.
' https://keisan.casio.com/exec/system/15052033860538
'
' https://matrix.reshish.com/multCalculation.php
'
' https://elsenaju.eu/Calculator/matrix-vector-product.htm
'
'
type Matrix
    dim as double m( any , any )
    declare constructor ( )
    declare constructor ( byval x as uinteger , byval y as uinteger )
end type
 
constructor Matrix ( )
end constructor
 
constructor Matrix ( byval x as uinteger , byval y as uinteger )
    redim this.m( x - 1 , y - 1 )
end constructor


type Vector
dim as double v( any )
    declare constructor ( )
    declare constructor ( byval x as uinteger )
end type

constructor Vector ( )
end constructor

constructor Vector ( byval x as uinteger )
    redim this.v( x - 1 )
end constructor
'
' ====================================================================
'
Public operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
'  Only applicable to square matrices or arrays.
' This routine from :
'  http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
'
' Dodicat suggests only using: if ubound( a.m , 2 ) = ubound( b.m , 1 )
'
'
    dim as Matrix ret
    dim as uinteger i, j, k
    'if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
     if ubound( a.m , 2 ) = ubound( b.m , 1 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                for k = 0 to ubound( b.m , 1 )
                    ret.m( i , j ) += a.m( i , k ) * b.m( k , j )
                next k
            next j
        next i
    end if
    return ret
end operator
'
'
Public operator * ( byref a as Matrix , byref b as Vector ) as Vector
'
'   Vector multiplied by a matrix, maybe correct .
'
'
   dim as Vector ret
   dim as uinteger i, j, k
   if ubound( a.m , 2 ) = ubound( b.v , 1 )  then
        redim ret.v( ubound( a.m , 1 ) )
        for i = 0 to ubound( a.m , 1 )
            
                for k = 0 to ubound( b.v , 1 )
                    ret.v( i ) += a.m( i , k ) * b.v( k  )
                next k
            
        next i
    end if   
   return ret
end operator
'
Public operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                ret.m( i , j ) = a.m( i , j ) - b.m( i , j )
            next j
        next i
    end if
    return ret
end operator
'
Public operator - ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 )  )
        for i = 0 to ubound( a.v , 1 )
            ret.v( i  ) = a.v( i  ) - b.v( i  )
        next i
    end if
    return ret
end operator
 '
Public operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
               ret.m( i , j ) = a.m( i , j ) + b.m( i , j )
            next j
        next i
    end if
    return ret
end operator
'
Public operator + ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 )  )
        for i = 0 to ubound( a.v , 1 )
            ret.v( i  ) = a.v( i  ) + b.v( i  )
        next i
    end if
    return ret
end operator
'
Public operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    dim as double x, y, z
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                x = a.m( i , j )
                y = b.m( i , j )
                y = sgn(x)*10^32 ' assume y = 0
             if y <> 0 then z = x/y
                ret.m( i , j ) = z
            next j
        next i
    end if
    return ret
end operator
'
Public operator / ( byref a as Vector , byref b as Vector ) as Vector
    dim as Vector ret
    dim as uinteger i
    dim as double x, y, z
    if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
        redim ret.v( ubound( a.v , 1 ) )
        for i = 0 to ubound( a.v , 1 )
                x = a.v( i )
                y = b.v( i )
                y = sgn(x)*10^32 ' assume y = 0
             if y <> 0 then z = x/y
                ret.v( i  ) = z
        next i
    end if
    return ret
end operator
'
' ......................................................................
'
Public sub prt_z(z() as Matrix, idx as Integer)
'
'     Matrix of Matrix, index and print .
'
dim as integer i,j
'
'print
for i=0 to ubound(z(idx).m,1)
 for j=0 to ubound(z(idx).m,2) 
    print Using "###.####";z(idx).m(i,j);" ";
  next j
  print
next i
print
'  
end sub
'
' ......................................................................
'
Public sub prt_m(z as Matrix)
'
'  Print elements from rectangular matrix .
'
dim as integer i,j
'

print
print ubound(z.m,1);" ";ubound(z.m,2)
print
 for i=0 to ubound(z.m,1) 
  for j=0 to ubound(z.m,2)  
    print Using "###.####";z.m(i,j);" ";
  next j
  print
next i
print
'  
end sub
'
' ......................................................................
'
Public sub prt_v(z as Vector)
'
'  Print elements from vector .
'
dim as integer i
'
'print
for i=0 to ubound(z.v,1)
    
    print Using "###.####";z.v(i);" ";
  
  
next i
print
print
'  
end sub
'
' ......................................................................
'
Public sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
'
'     Multiply vector by matrix .
'
dim as integer i,j,ub1, ub2
dim as single x,y,s
    
    ub1=ubound(M1.m,1)
    ub2=ubound(M1.m,2)
'    
    redim V3.v(0 to ub1)
'
        for j=0 to ub1
             s=0
         for i=0 to ub2
             x=V1.v(i)
             y=M1.m(j,i)
             s=s+x*y
         next i
          V3.v(j)=s
      next j
 '
end sub
'
' ......................................................................
'
Public sub gen_z(z() as Matrix, idx as Integer)
'
'     Matrix of Matrix, index and generate values .
'
dim as integer i,j
'
Randomize
  for j=0 to ubound(z(idx).m,2) 
   for i=0 to ubound(z(idx).m,1)
      z(idx).m(i,j) = -1 + 2*rnd
  next i
next j

'  
end sub
'
' ......................................................................
'
Public sub gen_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and generate values .
'
dim as integer i,j
Randomize
'
for i=0 to ubound(u(idx).v,1)
   
      u(idx).v(i) = -1 + 2*rnd
  
next i

'  
end sub
'
' ......................................................................
'
Public sub prtv_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and print values .
'
dim as integer i,j
'
for i=0 to ubound(u(idx).v,1)
   print Using "###.####";u(idx).v(i)
next i
print 
'  
end sub
'
' ......................................................................
'
Public sub Sigmoid_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and Sigmoid function of values .
'
dim as integer i,j
dim as single y
'
for i=0 to ubound(u(idx).v,1)
    y=u(idx).v(i)
    y=1/(1+exp(-y))
    u(idx).v(i) = y
next i
print 
'  
end sub
'
' ......................................................................
'
Public sub dSigmoid_v(u() as Vector, idx as Integer)
'
'     Vector of Vector, index and Derivative of Sigmoid function of values .
'
dim as integer i,j
dim as single y
'
for i=0 to ubound(u(idx).v,1)
    y=u(idx).v(i)
    'y=1/(1+exp(-y))
    y=-exp(-y)/(1-exp(-y))^2
    u(idx).v(i) = y
next i
print 
'  
end sub
'
' ......................................................................
'
' ---------------------------------------------- 2 ---------------------------

Code: Select all

' easy_matx.bi


#inclib "easy_matx"

'  For some reason, may need to duplicate types, but not constructors,
' in this *.bi

type Matrix
    dim as double m( any , any )
    declare constructor ( )
    declare constructor ( byval x as uinteger , byval y as uinteger )
end type
'
type Vector
dim as double v( any )
    declare constructor ( )
    declare constructor ( byval x as uinteger )
end type

'
'
' ---------------------------- operators -------------------------------
'
declare operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator * ( byref a as Matrix , byref b as Vector ) as Vector

declare operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator - ( byref a as Vector , byref b as Vector ) as Vector

declare operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator + ( byref a as Vector , byref b as Vector ) as Vector

declare operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator / ( byref a as Vector , byref b as Vector ) as Vector

'   The rest from luxan, sciwiseg@gmail.com
'
'  Probably need to make this content
'  available under GNU Free Documentation License 1.2 
'
'
' ............................. procedures .............................
'
declare sub gen_z(z() as Matrix, idx as Integer)
declare sub gen_v(u() as Vector, idx as Integer)

declare sub prtv_v(u() as Vector, idx as Integer)

declare sub prt_z(z() as Matrix, idx as Integer)
declare sub prt_m(z as Matrix)
declare sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)

declare sub prt_v(z as Vector)
'
declare sub Sigmoid_v(u() as Vector, idx as Integer)
declare sub dSigmoid_v(u() as Vector, idx as Integer)

' --------------------------------------- 3 --------------------------------------

Code: Select all


'
'  matx_smv.bas
'
'   Successive matrix vector multiplications .
'

'' compile with: fbc matx_test.bas


#include once "easy_matx.bi"

'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
'    Matrix of matrices
'
dim as Matrix W(1 to lms)
dim as Matrix X = Matrix(6,3)

dim as integer j,ux,uy

'dim as Matrix X = Matrix((0, 0, 1),(0, 1, 1),(1,0,0),(1,1,0),(1,0,1),(1,1,1))

'end

dim  Xa(0 to 5,0 to 2) as integer => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}


X.m={{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}


ux=ubound(Xa,1)
uy=ubound(Xa,2)
for j=0 to uy
 for i=0 to ux
   X.m(i,j) = Xa(i,j)
 next i
next j 
'
prt_m(X)
print
print " ux = ";ux
dim as Vector y = Vector(ux+1)
'dim ya(0 to ux+1) as integer
dim ya(0 to 5) as integer  => {(0),(1),(0),(1),(1),(0)}

'redim ya(0 to ux) as integer  => {0,1,0,1,1,0}
'ya(0 to ux) => {0,1,0,1,1,0}

for i=0 to ux+1
   y.v(i) = ya(i)
 next i

prt_v(y)

end
'
'   Vector of vectors
'
dim as Vector u(0 to lms)  ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
'   Dimension matrices and vectors .
'
for i=0 to lms-1
   nx=m_seq(i)
   ny=m_seq(i+1)
   W(i+1) = Matrix(ny , nx)
   u(i) = Vector(nx)    ' subtract bias vector, apply threshold function; return ?
   up(i) = Vector(nx)   ' subtract bias vector, apply derivative of threshold function; return ?
   ub(i+1) = Vector(ny) ' bias vector . 
   gen_v(ub(), i+1)     ' bias vector[s], random values [-1,1] . 
next i
'
'  ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print   " matrix of matrices values "
print
'
for i=1 to lms
    gen_z(W() , i)
    'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms 
   print " W(";i;") "
   prt_z(W() , i)
   u(i) = W(i)*u(i-1)
   u(i) = u(i) - ub(i)
   up(i) = u(i)
   Sigmoid_v(u(), i)
   dSigmoid_v(up(), i)
   print " u(";i;") "
   prtv_v(u(), i) 
   print " u(";i;")' "
   prtv_v(up(), i) 
next i


'  end command acts as global destructor 
' of allocated memory ?

end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7

fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Easy matrix

Post by fxm »

Please stop posting code that doesn't work.
Two errors: compilation + execution.
(otherwise I will soon stop reading your posts!)
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

Hi fxm

The code is in a state of flux, comment out 'X.m={{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}} and try again.

I tidied up the code a bit too,

Here are the two pieces of code that utilize easy_matx.bas and easy_matx.bi, in the
investigation of NN . Both compile and run upon my computer, I also used #cmdline "-exx"
at the beginning of the library and code files, and by implication the header file.

Code: Select all


'
'  bp_smv.bas
'
'   Successive matrix vector multiplications .
'   back propagation preliminaries.
'   
'' compile with: fbc  bp_smv.bas

#cmdline "-exx"
#include once "easy_matx.bi"

'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
'    Matrix of matrices
'
dim as Matrix W(1 to lms)
dim as Matrix X = Matrix(6,3)

dim as integer j,ux,uy

'dim as Matrix X = Matrix((0, 0, 1),(0, 1, 1),(1,0,0),(1,1,0),(1,0,1),(1,1,1))

'end

dim  Xa(0 to 5,0 to 2) as integer => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}


'X.m={{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}


ux=ubound(Xa,1)
uy=ubound(Xa,2)
for j=0 to uy
 for i=0 to ux
   X.m(i,j) = Xa(i,j)
 next i
next j 
'
prt_m(X)
print
print " ux = ";ux
dim as Vector y = Vector(ux+1)
'dim ya(0 to ux+1) as integer
dim ya(0 to 5) as integer  => {(0),(1),(0),(1),(1),(0)}

'redim ya(0 to ux) as integer  => {0,1,0,1,1,0}
'ya(0 to ux) => {0,1,0,1,1,0}

for i=0 to ux+1
   y.v(i) = ya(i)
 next i

prt_v(y)

end
'
'   Vector of vectors
'
dim as Vector u(0 to lms)  ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
'   Dimension matrices and vectors .
'
for i=0 to lms-1
   nx=m_seq(i)
   ny=m_seq(i+1)
   W(i+1) = Matrix(ny , nx)
   u(i) = Vector(nx)    ' subtract bias vector, apply threshold function; return ?
   up(i) = Vector(nx)   ' subtract bias vector, apply derivative of threshold function; return ?
   ub(i+1) = Vector(ny) ' bias vector . 
   gen_v(ub(), i+1)     ' bias vector[s], random values [-1,1] . 
next i
'
'  ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print   " matrix of matrices values "
print
'
for i=1 to lms
    gen_z(W() , i)
    'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms 
   print " W(";i;") "
   prt_z(W() , i)
   u(i) = W(i)*u(i-1)
   u(i) = u(i) - ub(i)
   up(i) = u(i)
   Sigmoid_v(u(), i)
   dSigmoid_v(up(), i)
   print " u(";i;") "
   prtv_v(u(), i) 
   print " u(";i;")' "
   prtv_v(up(), i) 
next i


'  end command acts as global destructor 
' of allocated memory ?

end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7


Code: Select all


'
'  matx_smv.bas
'
'   Successive matrix vector multiplications .
'   And evaluation of sigmoid, derivative of sigmoid.
'
'' compile with: fbc matx_smv.bas

#cmdline "-exx"
#include once "easy_matx.bi"

'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
'    Matrix of matrices
'
dim as Matrix W(1 to lms)
'
'   Vector of vectors
'
dim as Vector u(0 to lms)  ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
'   Dimension matrices and vectors .
'
for i=0 to lms-1
   nx=m_seq(i)
   ny=m_seq(i+1)
   W(i+1) = Matrix(ny , nx)
   u(i) = Vector(nx)    ' subtract bias vector, apply threshold function; return ?
   up(i) = Vector(nx)   ' subtract bias vector, apply derivative of threshold function; return ?
   ub(i+1) = Vector(ny) ' bias vector . 
   gen_v(ub(), i+1)     ' bias vector[s], random values [-1,1] . 
next i
'
'  ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print   " matrix of matrices values "
print
'
for i=1 to lms
    gen_z(W() , i)
    'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms 
   print " W(";i;") "
   prt_z(W() , i)
   u(i) = W(i)*u(i-1)
   u(i) = u(i) - ub(i)
   up(i) = u(i)
   Sigmoid_v(u(), i)
   dSigmoid_v(up(), i)
   print " u(";i;") "
   prtv_v(u(), i) 
   print " u(";i;")' "
   prtv_v(up(), i) 
next i


'  end command acts as global destructor 
' of allocated memory ?

end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7


fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Easy matrix

Post by fxm »

Luxan wrote: Aug 12, 2022 22:10 Both compile and run upon my computer

Not true for the first:
.....
dim as Vector y = Vector(ux+1)
.....
for i=0 to ux+1 '' <== No
y.v(i) = ya(i)
next i
.....

inducing:
Aborting due to runtime error 6 (out of bounds array access) at line .....
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

Hi fxm

You're quite right, bp_smv.bas did have that error and was detected just now when
I compiled it through Geany; not sure how I missed that.

Removing the +1 returns a vector of the right length.

I'm attempting to use documentation, and Python code, from :

https://python-course.eu/python_and_mac ... arning.pdf

if you want I can upload the python code I'm using from there.

If I have another look around the internet, I might find an explanation that's
easier for everyone to understand.

The Julia programming language was developed, in part, to address the slow run
times of Python; FreeBasic doesn't have that difficulty.
Also, if properly constructed and documented,, a BASIC program can provide good
instruction.
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

A little bit of an update.

I found a fragment of python code that kinda illustrated how back propagation
is performed, in a separate for loop, after the forward propagation for loop.
The derivative of the activation function is used in the back propagation loop.

I had difficulty using the python fragment, so I translated it with some difficulty
to Maxima CAS code; it runs to completion, whether the results are sensible is yet
to be determined.
Therefore, worked examples from the internet are necessary for comparison; this
may take awhile.

If I'm satisfied with that, then more coding before I translate this to BASIC, then
more testing, tidy up and a lot of accompanying documentation.
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

I now have code for a NN from Maxima CAS,
translated into FreeBASIC code; tested to some extent
in both.
Also Python code that's similar and another way to
co-develop code and test results.
.

The FreeBASIC results .

The input for training, in both instances is :

1.0000 1.0000 1.0000
1.0000 0.0000 1.0000
0.0000 0.0000 1.0000
0.0000 1.0000 1.0000

And the target data is:

1.0000
1.0000
0.0000
0.0000

The test data is :

1.0000 0.0000 0.0000

The result is :

0.9972

meant to be close to 1

With m_seq(1 to 6) = {4,3,4,4,1,4}, that's
three layers .
The NN was trained for 800 epochs and took, at most,
a few seconds ; try a timer eventually.

More testing required, there's bound to be more hidden
glitches.

For this version I'm only using matrices and all of the
indexing to these starts at 1 .
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Easy matrix

Post by Luxan »

The time to train the three layer Neural Network for 800 epochs, mentioned in my previous post is:

0.02568411827087402 seconds
Post Reply