Skip to content

Commit 41f7e86

Browse files
author
Yusuke Sugomori
committed
cpp init 0
1 parent 272a0fc commit 41f7e86

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

cpp/DBN.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,14 @@ RBM::RBM(int size, int n_v, int n_h, double **w, double *hb, double *vb) {
278278

279279
if(hb == NULL) {
280280
hbias = new double[n_hidden];
281+
for(int i=0; i<n_hidden; i++) hbias[i] = 0;
281282
} else {
282283
hbias = hb;
283284
}
284285

285286
if(vb == NULL) {
286287
vbias = new double[n_visible];
288+
for(int i=0; i<n_visible; i++) vbias[i] = 0;
287289
} else {
288290
vbias = vb;
289291
}
@@ -404,6 +406,13 @@ LogisticRegression::LogisticRegression(int size, int in, int out) {
404406
W = new double*[n_out];
405407
for(int i=0; i<n_out; i++) W[i] = new double[n_in];
406408
b = new double[n_out];
409+
410+
for(int i=0; i<n_out; i++) {
411+
for(int j=0; j<n_in; j++) {
412+
W[i][j] = 0;
413+
}
414+
b[i] = 0;
415+
}
407416
}
408417

409418
LogisticRegression::~LogisticRegression() {
@@ -418,6 +427,7 @@ void LogisticRegression::train(int *x, int *y, double lr) {
418427
double *dy = new double[n_out];
419428

420429
for(int i=0; i<n_out; i++) {
430+
p_y_given_x[i] = 0;
421431
for(int j=0; j<n_in; j++) {
422432
p_y_given_x[i] += W[i][j] * x[j];
423433
}
@@ -454,6 +464,7 @@ void LogisticRegression::softmax(double *x) {
454464

455465
void LogisticRegression::predict(int *x, double *y) {
456466
for(int i=0; i<n_out; i++) {
467+
y[i] = 0;
457468
for(int j=0; j<n_in; j++) {
458469
y[i] += W[i][j] * x[j];
459470
}

cpp/LogisticRegression.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ LogisticRegression::LogisticRegression(int size, int in, int out) {
1414
W = new double*[n_out];
1515
for(int i=0; i<n_out; i++) W[i] = new double[n_in];
1616
b = new double[n_out];
17+
18+
for(int i=0; i<n_out; i++) {
19+
for(int j=0; j<n_in; j++) {
20+
W[i][j] = 0;
21+
}
22+
b[i] = 0;
23+
}
1724
}
1825

1926
LogisticRegression::~LogisticRegression() {
@@ -28,6 +35,7 @@ void LogisticRegression::train(int *x, int *y, double lr) {
2835
double *dy = new double[n_out];
2936

3037
for(int i=0; i<n_out; i++) {
38+
p_y_given_x[i] = 0;
3139
for(int j=0; j<n_in; j++) {
3240
p_y_given_x[i] += W[i][j] * x[j];
3341
}
@@ -63,6 +71,7 @@ void LogisticRegression::softmax(double *x) {
6371

6472
void LogisticRegression::predict(int *x, double *y) {
6573
for(int i=0; i<n_out; i++) {
74+
y[i] = 0;
6675
for(int j=0; j<n_in; j++) {
6776
y[i] += W[i][j] * x[j];
6877
}
@@ -74,6 +83,8 @@ void LogisticRegression::predict(int *x, double *y) {
7483

7584

7685
void test_lr() {
86+
srand(0);
87+
7788
double learning_rate = 0.1;
7889
double n_epochs = 500;
7990

cpp/RBM.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ RBM::RBM(int size, int n_v, int n_h, double **w, double *hb, double *vb) {
4747

4848
if(hb == NULL) {
4949
hbias = new double[n_hidden];
50+
for(int i=0; i<n_hidden; i++) hbias[i] = 0;
5051
} else {
5152
hbias = hb;
5253
}
5354

5455
if(vb == NULL) {
5556
vbias = new double[n_visible];
57+
for(int i=0; i<n_visible; i++) vbias[i] = 0;
5658
} else {
5759
vbias = vb;
5860
}

0 commit comments

Comments
 (0)