Skip to content

Commit 41c7a0c

Browse files
author
Yusuke Sugomori
committed
c init 0
1 parent 41f7e86 commit 41c7a0c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

c/DBN.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,14 @@ void RBM__construct(RBM* this, int N, int n_visible, int n_hidden, \
302302

303303
if(hbias == NULL) {
304304
this->hbias = (double *)malloc(sizeof(double) * n_hidden);
305+
for(i=0; i<n_hidden; i++) this->hbias[i] = 0;
305306
} else {
306307
this->hbias = hbias;
307308
}
308309

309310
if(vbias == NULL) {
310311
this->vbias = (double *)malloc(sizeof(double) * n_visible);
312+
for(i=0; i<n_visible; i++) this->vbias[i] = 0;
311313
} else {
312314
this->vbias = vbias;
313315
}
@@ -430,7 +432,7 @@ void RBM_reconstruct(RBM* this, int *v, double *reconstructed_v) {
430432

431433
// LogisticRegression
432434
void LogisticRegression__construct(LogisticRegression *this, int N, int n_in, int n_out) {
433-
int i;
435+
int i, j;
434436
this->N = N;
435437
this->n_in = n_in;
436438
this->n_out = n_out;
@@ -439,6 +441,13 @@ void LogisticRegression__construct(LogisticRegression *this, int N, int n_in, in
439441
this->W[0] = (double *)malloc(sizeof(double) * n_in * n_out);
440442
for(i=0; i<n_out; i++) this->W[i] = this->W[0] + i * n_in;
441443
this->b = (double *)malloc(sizeof(double) * n_out);
444+
445+
for(i=0; i<n_out; i++) {
446+
for(j=0; j<n_in; j++) {
447+
this->W[i][j] = 0;
448+
}
449+
this->b[i] = 0;
450+
}
442451
}
443452

444453
void LogisticRegression__destruct(LogisticRegression *this) {
@@ -453,6 +462,7 @@ void LogisticRegression_train(LogisticRegression *this, int *x, int *y, double l
453462
double *dy = (double *)malloc(sizeof(double) * this->n_out);
454463

455464
for(i=0; i<this->n_out; i++) {
465+
p_y_given_x[i] = 0;
456466
for(j=0; j<this->n_in; j++) {
457467
p_y_given_x[i] += this->W[i][j] * x[j];
458468
}
@@ -492,6 +502,7 @@ void LogisticRegression_predict(LogisticRegression *this, int *x, double *y) {
492502
int i,j;
493503

494504
for(i=0; i<this->n_out; i++) {
505+
y[i] = 0;
495506
for(j=0; j<this->n_in; j++) {
496507
y[i] += this->W[i][j] * x[j];
497508
}

c/LogisticRegression.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ void test_lr(void);
77

88

99
void LogisticRegression__construct(LogisticRegression *this, int N, int n_in, int n_out) {
10-
int i;
10+
int i, j;
1111
this->N = N;
1212
this->n_in = n_in;
1313
this->n_out = n_out;
@@ -16,6 +16,13 @@ void LogisticRegression__construct(LogisticRegression *this, int N, int n_in, in
1616
this->W[0] = (double *)malloc(sizeof(double) * n_in * n_out);
1717
for(i=0; i<n_out; i++) this->W[i] = this->W[0] + i * n_in;
1818
this->b = (double *)malloc(sizeof(double) * n_out);
19+
20+
for(i=0; i<n_out; i++) {
21+
for(j=0; j<n_in; j++) {
22+
this->W[i][j] = 0;
23+
}
24+
this->b[i] = 0;
25+
}
1926
}
2027

2128
void LogisticRegression__destruct(LogisticRegression *this) {
@@ -30,6 +37,7 @@ void LogisticRegression_train(LogisticRegression *this, int *x, int *y, double l
3037
double *dy = (double *)malloc(sizeof(double) * this->n_out);
3138

3239
for(i=0; i<this->n_out; i++) {
40+
p_y_given_x[i] = 0;
3341
for(j=0; j<this->n_in; j++) {
3442
p_y_given_x[i] += this->W[i][j] * x[j];
3543
}
@@ -69,6 +77,7 @@ void LogisticRegression_predict(LogisticRegression *this, int *x, double *y) {
6977
int i,j;
7078

7179
for(i=0; i<this->n_out; i++) {
80+
y[i] = 0;
7281
for(j=0; j<this->n_in; j++) {
7382
y[i] += this->W[i][j] * x[j];
7483
}

c/RBM.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ void RBM__construct(RBM* this, int N, int n_visible, int n_hidden, \
6060

6161
if(hbias == NULL) {
6262
this->hbias = (double *)malloc(sizeof(double) * n_hidden);
63+
for(i=0; i<n_hidden; i++) this->hbias[i] = 0;
6364
} else {
6465
this->hbias = hbias;
6566
}
6667

6768
if(vbias == NULL) {
6869
this->vbias = (double *)malloc(sizeof(double) * n_visible);
70+
for(i=0; i<n_visible; i++) this->vbias[i] = 0;
6971
} else {
7072
this->vbias = vbias;
7173
}

0 commit comments

Comments
 (0)