I'm quite new to C++ and I find difficulity in rewriting code from c++ to pascal.
Who can help to rewrite variables declarations; main() function until, InitializeApplication(&Net); and functions: GenerateNetwork(), RandomWeights(), to pascal;
This Neural net app.
P.s sorry for my english.
C++ to Pascal
Started by HDi, Jul 21 2008 01:40 AM
2 replies to this topic
#1
Posted 21 July 2008 - 01:40 AM
|
|
|
#2
Posted 21 July 2008 - 07:41 AM
You will have an easier time translating C++ to Object Pascal, as the supported concepts will align better. Having said that, you will still run into issues when you have to recreate libraries in ObjectPascal that exist by default in C++. You also need to realize that they do not use some of the same concepts.
I cannot give you more than general advice without seeing code and your attempts to translate it, however.
I cannot give you more than general advice without seeing code and your attempts to translate it, however.
#3
Posted 22 July 2008 - 02:57 AM
1.
#include <stdlib.h>
2.
#include <stdio.h>
3.
#include <math.h>
4.
5.
typedef int BOOL;
6.
typedef int INT;
7.
typedef double REAL;
8.
9.
10.
11.
12.
#define LO 0.1
13.
#define HI 0.9
14.
#define BIAS 1
15.
16.
17.
typedef struct { /* A LAYER OF A NET: */
18.
INT Units; /* - number of units in this layer */
19.
REAL* Output; /* - output of ith unit */
20.
REAL* Error; /* - error term of ith unit */
21.
REAL** Weight; /* - connection weights to ith unit */
22.
REAL** WeightSave; /* - saved weights for stopped training */
23.
REAL** dWeight; /* - last weight deltas for momentum */
24.
} LAYER;
25.
26.
typedef struct { /* A NET: */
27.
LAYER** Layer; /* - layers of this net */
28.
LAYER* InputLayer; /* - input layer */
29.
LAYER* OutputLayer; /* - output layer */
30.
REAL Alpha; /* - momentum factor */
31.
REAL Eta; /* - learning rate */
32.
REAL Gain; /* - gain of sigmoid function */
33.
REAL Error; /* - total net error */
34.
} NET;
35.
36.
37.
/******************************************************************************
38.
A P P L I C A T I O N - S P E C I F I C C O D E
39.
******************************************************************************/
40.
41.
#define NUM_LAYERS 3
42.
#define N 30
43.
#define M 1
44.
INT Units[NUM_LAYERS] = {N, 10, M};
45.
46.
#define FIRST_YEAR 1700
47.
#define NUM_YEARS 280
48.
49.
#define TRAIN_LWB (N)
50.
#define TRAIN_UPB (179)
51.
#define TRAIN_YEARS (TRAIN_UPB - TRAIN_LWB + 1)
52.
#define TEST_LWB (180)
53.
#define TEST_UPB (259)
54.
#define TEST_YEARS (TEST_UPB - TEST_LWB + 1)
55.
#define EVAL_LWB (260)
56.
#define EVAL_UPB (NUM_YEARS - 1)
57.
#define EVAL_YEARS (EVAL_UPB - EVAL_LWB + 1)
58.
59.
REAL Sunspots_[NUM_YEARS];
60.
REAL Sunspots [NUM_YEARS] = {
61.
62.
0.0262, 0.0575, 0.0837, 0.1203, 0.1883, 0.3033,
63.
0.1517, 0.1046, 0.0523, 0.0418, 0.0157, 0.0000,
64.
0.0000, 0.0105, 0.0575, 0.1412, 0.2458, 0.3295,
65.
0.3138, 0.2040, 0.1464, 0.1360, 0.1151, 0.0575,
66.
0.1098, 0.2092, 0.4079, 0.6381, 0.5387, 0.3818,
67.
0.2458, 0.1831, 0.0575, 0.0262, 0.0837, 0.1778,
68.
0.3661, 0.4236, 0.5805, 0.5282, 0.3818, 0.2092,
69.
0.1046, 0.0837, 0.0262, 0.0575, 0.1151, 0.2092,
70.
0.3138, 0.4231, 0.4362, 0.2495, 0.2500, 0.1606,
71.
0.0638, 0.0502, 0.0534, 0.1700, 0.2489, 0.2824,
72.
0.3290, 0.4493, 0.3201, 0.2359, 0.1904, 0.1093,
73.
0.0596, 0.1977, 0.3651, 0.5549, 0.5272, 0.4268,
74.
0.3478, 0.1820, 0.1600, 0.0366, 0.1036, 0.4838,
75.
0.8075, 0.6585, 0.4435, 0.3562, 0.2014, 0.1192,
76.
0.0534, 0.1260, 0.4336, 0.6904, 0.6846, 0.6177,
77.
0.4702, 0.3483, 0.3138, 0.2453, 0.2144, 0.1114,
78.
0.0837, 0.0335, 0.0214, 0.0356, 0.0758, 0.1778,
79.
0.2354, 0.2254, 0.2484, 0.2207, 0.1470, 0.0528,
80.
0.0424, 0.0131, 0.0000, 0.0073, 0.0262, 0.0638,
81.
0.0727, 0.1851, 0.2395, 0.2150, 0.1574, 0.1250,
82.
0.0816, 0.0345, 0.0209, 0.0094, 0.0445, 0.0868,
83.
0.1898, 0.2594, 0.3358, 0.3504, 0.3708, 0.2500,
84.
0.1438, 0.0445, 0.0690, 0.2976, 0.6354, 0.7233,
85.
0.5397, 0.4482, 0.3379, 0.1919, 0.1266, 0.0560,
86.
0.0785, 0.2097, 0.3216, 0.5152, 0.6522, 0.5036,
87.
0.3483, 0.3373, 0.2829, 0.2040, 0.1077, 0.0350,
88.
0.0225, 0.1187, 0.2866, 0.4906, 0.5010, 0.4038,
89.
0.3091, 0.2301, 0.2458, 0.1595, 0.0853, 0.0382,
90.
0.1966, 0.3870, 0.7270, 0.5816, 0.5314, 0.3462,
91.
0.2338, 0.0889, 0.0591, 0.0649, 0.0178, 0.0314,
92.
0.1689, 0.2840, 0.3122, 0.3332, 0.3321, 0.2730,
93.
0.1328, 0.0685, 0.0356, 0.0330, 0.0371, 0.1862,
94.
0.3818, 0.4451, 0.4079, 0.3347, 0.2186, 0.1370,
95.
0.1396, 0.0633, 0.0497, 0.0141, 0.0262, 0.1276,
96.
0.2197, 0.3321, 0.2814, 0.3243, 0.2537, 0.2296,
97.
0.0973, 0.0298, 0.0188, 0.0073, 0.0502, 0.2479,
98.
0.2986, 0.5434, 0.4215, 0.3326, 0.1966, 0.1365,
99.
0.0743, 0.0303, 0.0873, 0.2317, 0.3342, 0.3609,
100.
0.4069, 0.3394, 0.1867, 0.1109, 0.0581, 0.0298,
101.
0.0455, 0.1888, 0.4168, 0.5983, 0.5732, 0.4644,
102.
0.3546, 0.2484, 0.1600, 0.0853, 0.0502, 0.1736,
103.
0.4843, 0.7929, 0.7128, 0.7045, 0.4388, 0.3630,
104.
0.1647, 0.0727, 0.0230, 0.1987, 0.7411, 0.9947,
105.
0.9665, 0.8316, 0.5873, 0.2819, 0.1961, 0.1459,
106.
0.0534, 0.0790, 0.2458, 0.4906, 0.5539, 0.5518,
107.
0.5465, 0.3483, 0.3603, 0.1987, 0.1804, 0.0811,
108.
0.0659, 0.1428, 0.4838, 0.8127
109.
110.
};
111.
112.
REAL Mean;
113.
REAL TrainError;
114.
REAL TrainErrorPredictingMean;
115.
REAL TestError;
116.
REAL TestErrorPredictingMean;
117.
118.
FILE* f;
119.
120.
121.
void GenerateNetwork(NET* Net)
122.
{
123.
INT l,i;
124.
125.
Net->Layer = (LAYER**) calloc(NUM_LAYERS, sizeof(LAYER*));
126.
127.
for (l=0; l<NUM_LAYERS; l++) {
128.
Net->Layer[l] = (LAYER*) malloc(sizeof(LAYER));
129.
130.
Net->Layer[l]->Units = Units[l];
131.
Net->Layer[l]->Output = (REAL*) calloc(Units[l]+1, sizeof(REAL));
132.
Net->Layer[l]->Error = (REAL*) calloc(Units[l]+1, sizeof(REAL));
133.
Net->Layer[l]->Weight = (REAL**) calloc(Units[l]+1, sizeof(REAL*));
134.
Net->Layer[l]->WeightSave = (REAL**) calloc(Units[l]+1, sizeof(REAL*));
135.
Net->Layer[l]->dWeight = (REAL**) calloc(Units[l]+1, sizeof(REAL*));
136.
Net->Layer[l]->Output[0] = BIAS;
137.
138.
if (l != 0) {
139.
for (i=1; i<=Units[l]; i++) {
140.
Net->Layer[l]->Weight[i] = (REAL*) calloc(Units[l-1]+1,
141.
sizeof(REAL));
142.
Net->Layer[l]->WeightSave[i] = (REAL*) calloc(Units[l-1]+1,
143.
sizeof(REAL));
144.
Net->Layer[l]->dWeight[i] = (REAL*) calloc(Units[l-1]+1,
145.
sizeof(REAL));
146.
}
147.
}
148.
}
149.
Net->InputLayer = Net->Layer[0];
150.
Net->OutputLayer = Net->Layer[NUM_LAYERS - 1];
151.
Net->Alpha = 0.9;
152.
Net->Eta = 0.25;
153.
Net->Gain = 1;
154.
}
155.
156.
void RandomWeights(NET* Net)
157.
{
158.
INT l,i,j;
159.
160.
for (l=1; l<NUM_LAYERS; l++) {
161.
for (i=1; i<=Net->Layer[l]->Units; i++) {
162.
for (j=0; j<=Net->Layer[l-1]->Units; j++) {
163.
Net->Layer[l]->Weight[i][j] = RandomEqualREAL(-0.5, 0.5);
164.
}
165.
}
166.
}
167.
}
168.
169.
170.
int main()
171.
{
172.
NET Net;
173.
BOOL Stop;
174.
REAL MinTestError;
175.
176.
GenerateNetwork(&Net);
177.
RandomWeights(&Net);
178.
179.
}
180.
Edited by WingedPanther, 23 July 2008 - 07:37 AM.
add code tags


Sign In
Create Account

Back to top









