Converting from c to freebasic

Windows specific questions.
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Converting from c to freebasic

Postby Rens » Dec 04, 2005 12:44

Can someone explain how to convert this from c into freebasic?

Code: Select all

typedef struct {                     /* A LAYER OF A NET:                     */
        INT           Units;         /* - number of units in this layer       */
        REAL*         Activation;    /* - activation of ith unit              */
        INT*          Output;        /* - output of ith unit                  */
        REAL*         Error;         /* - error term of ith unit              */
        REAL**        Weight;        /* - connection weights to ith unit      */
} LAYER;

typedef struct {                     /* A NET:                                */
        LAYER*        InputLayer;    /* - input layer                         */
        LAYER*        OutputLayer;   /* - output layer                        */
        REAL          Eta;           /* - learning rate                       */
        REAL          Error;         /* - total net error                     */
        REAL          Epsilon;       /* - net error to terminate training     */
} NET;


/******************************************************************************
                          I N I T I A L I Z A T I O N
 ******************************************************************************/


void GenerateNetwork(NET* Net)
{
  INT i;

  // fprintf(f,"Sub : GenerateNetwork\n");

  Net->InputLayer  = (LAYER*) malloc(sizeof(LAYER));
  Net->OutputLayer = (LAYER*) malloc(sizeof(LAYER));

  Net->InputLayer->Units       = N;
  Net->InputLayer->Output      = (INT*)   calloc(N+1, sizeof(INT));
  Net->InputLayer->Output[0]   = BIAS;

  Net->OutputLayer->Units      = M;
  Net->OutputLayer->Activation = (REAL*)  calloc(M+1, sizeof(REAL));
  Net->OutputLayer->Output     = (INT*)   calloc(M+1, sizeof(INT));
  Net->OutputLayer->Error      = (REAL*)  calloc(M+1, sizeof(REAL));
  Net->OutputLayer->Weight     = (REAL**) calloc(M+1, sizeof(REAL*));
 
  for (i=1; i<=M; i++) {
    Net->OutputLayer->Weight[i] = (REAL*) calloc(N+1, sizeof(REAL));
  }

  Net->Eta     = 0.1;
  Net->Epsilon = 0.01;
}
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Dec 04, 2005 14:36

Assuming INT_ and REAL were defined already:

Code: Select all

type LAYER
        as INT_           Units
        as REAL ptr         Activation
        as INT_ ptr          Output
        as REAL ptr         Error
        as REAL ptr ptr        Weight
end type

type NET
        as LAYER ptr        InputLayer
        as LAYER ptr        OutputLayer
        as REAL          Eta
        as REAL          Error
        as REAL          Epsilon
end type


sub GenerateNetwork(byval Net as NET ptr)
  dim as INT_ i

  Net->InputLayer  = allocate(len(LAYER))
  Net->OutputLayer = allocate(len(LAYER))

  Net->InputLayer->Units       = N
  Net->InputLayer->Output      = callocate(N+1, len(INT_))
  Net->InputLayer->Output[0]   = BIAS

  Net->OutputLayer->Units      = M
  Net->OutputLayer->Activation = callocate(M+1, len(REAL))
  Net->OutputLayer->Output     = callocate(M+1, len(INT_))
  Net->OutputLayer->Error      = callocate(M+1, len(REAL))
  Net->OutputLayer->Weight     = callocate(M+1, len(REAL tr))
 
  for i = 1 to M
    Net->OutputLayer->Weight[i] = callocate(N+1, len(REAL))
  next

  Net->Eta     = 0.1
  Net->Epsilon = 0.01
 
end sub


Not tested..
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Dec 04, 2005 18:17

line 32: REAL tr = REAL ptr ;)
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Postby Rens » Dec 05, 2005 0:13

Doens't work! It compiles, but when running the program crashed.

Maybe pointers are too difficult for me :(

Following code works, but it's a pitty of the unused space.

Code: Select all

Type LAYER
        As Integer           Units
        As Double            Activation
        As Integer           oOutput
        As Double            oError
        As Double            Weighta
        As Double            Weightb
End Type

Type NET
        As LAYER           InputLayer(10)
        As LAYER           OutputLayer(10)
        As Double          Eta
        As Double          oError
        As Double          Epsilon
End Type

Dim net(10) As NET

net(0).inputlayer(0).units=3
net(1).inputlayer(0).units=5
?net(0).inputlayer(0).units
?net(1).inputlayer(0).units
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Dec 05, 2005 5:20

Your source code is a little difficult to translate because REAL and INT are not native types in both C and BASIC. Plus, N, M, and BIAS aren't defined either. But luckily, I found the complete source at a neural network site:

Code: Select all

Option Explicit

Type BOOL As Integer
Type CHAR As UByte
Type INT_ As Integer
Type REAL As Double

#define FALSE         0
#define TRUE          1

#define MIN(x,y)      iif((x)<(y), (x), (y))
#define MAX(x,y)      iif((x)>(y), (x), (y))

#define LO            -1
#define HI            +1
#define BIAS           1

#define sqr_(x)        ((x)*(x))

Type LAYER
    Units      As INT_
    Activation As REAL Ptr
    Output     As INT_ Ptr
    Error      As REAL Ptr
    Weight     As REAL Ptr Ptr
End Type

Type NET
    InputLayer  As LAYER Ptr
    OutputLayer As LAYER Ptr
    Eta         As REAL
    Error       As REAL
    Epsilon     As REAL
End Type

'******************************************************************************
'               A P P L I C A T I O N - S P E C I F I C   C O D E
'******************************************************************************/
#define NUM_DATA      10
#define X             5
#define Y             7

#define N             (X * Y)
#define M             10

'******************************************************************************
'                         I N I T I A L I Z A T I O N
'******************************************************************************/
Sub GenerateNetwork(ByVal Net As NET Ptr)
    Dim i As Integer
    Print "Sub : GenerateNetwork"
   
    Net->InputLayer = Allocate( SizeOf(LAYER))
    Net->OutputLayer = Allocate( SizeOf(LAYER))
   
    Net->InputLayer->Units       = N
    Net->InputLayer->Output      = CAllocate(N + 1, SizeOf(INT_))
    Net->InputLayer->Output[0]   = BIAS
   
    Net->OutputLayer->Units      = M
    Net->OutputLayer->Activation = CAllocate(M + 1, SizeOf(REAL))
    Net->OutputLayer->Output     = CAllocatE(M + 1, SizeOf(INT_))
    Net->OutputLayer->Error      = CAllocatE(M + 1, SizeOf(REAL))
    Net->OutputLayer->Weight     = CAllocatE(M + 1, SizeOf(REAL))
   
    For i = 1 To M
        Net->OutputLayer->Weight[i] = CAllocate(N + 1, SizeOf(REAL))
    Next i

    Net->Eta     = 0.1
    Net->Epsilon = 0.01
End Sub
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Postby Rens » Dec 05, 2005 14:50

Thanx Jofers,

Don't be offended, but i'm ashame to tell you i have the whole code.
Thought i could translate the rest myself.
You see, i still need an expert to tell me how.

How do i call the sub generatenetwork?

In the c source it's: GenerateNetwork(&Net)

In Freebasic: Call GenerateNetwork(&Net) gives a argument count mismatch

Btw: Are you interessted in programming neural nets? I going to translate another one (luckily in basic). So, if it's finished and you are interessted i will post it?
Imortis
Moderator
Posts: 1744
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Postby Imortis » Dec 05, 2005 15:13

Acctually I would be very interested. I know you didn't ask me, but I figured I would throw in my 0.02 USD.
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Dec 05, 2005 17:05

Code: Select all

 GenerateNetwork(&Net)


in c & is addressof and can be changed for @ in FB
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Dec 05, 2005 17:07

http://www.phim.unibe.ch/comp_doc/c_man ... sions.html

this page shows some of the common operators so should give you an idea what to change to in FB, plus browse the site for more useful stuff
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Dec 05, 2005 21:07

You shouldn't feel ashamed. I couldn't have told you any of that a year ago. Picking goals you can't presently do is how you get better at things, like pushing yourself past the point of being tired in physical exercise.

When people talk about pointers, they mean location of something. A good way to think about this is arrays. As far as your program goes, everything boils down to a giant array of bytes:

Code: Select all

Dim ProgramMemory(2000000) As Byte


You can think of a pointer as an index of that huge array. For instance, this code:

Code: Select all

Dim MyPointer As Byte Ptr
Dim MyVariable As Integer

MyVariable = 4
MyPointer = @MyVariable


Would store the pointer to MyVariable inside MyPointer, so you could say MyVariable is at MyPointer. Another way of thinking about this is:

Code: Select all

Print *MyPointer


Would print 4, and it's the same as:

Code: Select all

Print ProgramMemory(MyPointer)


So that's the basic operation of pointers and what they are. "*MyPointer" gets the value that's stored inside your pointer, the same as "Program(MyPointer)".

Now, you'll see "CAllocate" and "Allocate" all over GenerateNetwork. When you call these, you're asking the Operating System for more memory. Memory like this, which your program can get at a whim is called "Dynamic" memory because you can resize it and stuff, your neural network could have 3 nodes or 10,000 nodes.

Allocate returns a pointer (think: array index) to the new memory. Though this upcoming analogy is technically wrong, it might help you mneumonically to think of CAllocate like this:

Code: Select all

Dim MyMemory As Byte Ptr
MyMemory = Allocate(2000)


This just allocated 2000 bytes.

Code: Select all

Dim Program(2000000) As Byte
Dim MyMemory As Integer

ReDim Program(2002000) As Byte
MyMemory = 2000001


This effectively does the same thing. You just got 2000 more bytes, and stored the location of where those 2000 bytes begin. CAllocate is the same, only FreeBASIC sets all the memory to "0" before your program can use it. Otherwise it might have some random values in it.

The rest is all convention:

Code: Select all

FreeBASIC Code        = C Equivalent
-------------------------------------
@MyVariable           = &MyVariable
*MyVariable           = *MyVariable
Allocate(x)           = malloc(x)
CAllocate(x)          = calloc(x)
Deallocate(x)         = free(x)
Dim x As Byte Ptr     = char *x;
Dim x As Byte Ptr Ptr = char **x;


More convention:

Code: Select all

Dim MyTypePointer  As MyType Ptr
Dim MyTypeVariable As MyType

MyTypeVariable = *MyTypePointer
Print MyTypeVariable.FieldA


Will print the same thing as:

Code: Select all

Print *MyTypePointer.FieldA


Will print hte same thing as:

Code: Select all

Print MyTypePointer->FieldA


It's the same for both C and FreeBASIC
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Postby Rens » Dec 08, 2005 22:36

Thanx all of you.
I think i'm getting the hang of pointer now after your lessons!

Imortis, i see you are interessted at sources of neural nets.
I have translated freebasic and non translated visual basic, c and qbasic sources.
The programs are split up in different files and some with documentation so they are too big and/or difficult to post them in a forum.

My email adres is rensvanschie@gmail.com, so anyone who is interressted can sent me a personal mail asking for it and tell me where to sent it to. Doesnt have to be your mail adress.

I have no commercial intentions whatsoever.
The programs are not written by me so i think it wouldn't be fair to make money out of it, but that's up to you.
dkl
Site Admin
Posts: 3221
Joined: Jul 28, 2005 14:45
Location: Germany

Postby dkl » Dec 09, 2005 13:08

Does this really work?

Code: Select all

Print *MyTypePointer.FieldA

I think it will not :-)

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 3 guests