Skip to content

Commit 15241eb

Browse files
committed
go
1 parent 7861196 commit 15241eb

File tree

4 files changed

+153
-21
lines changed

4 files changed

+153
-21
lines changed

c/LogisticRegression.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void test_lr(void) {
9494
int i, j, epoch;
9595

9696
double learning_rate = 0.1;
97-
double n_epochs = 500;
97+
int n_epochs = 500;
9898

9999
int train_N = 6;
100100
int test_N = 2;

cpp/LogisticRegression.cpp

+1-19
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,12 @@ void test_lr() {
8686
srand(0);
8787

8888
double learning_rate = 0.1;
89-
double n_epochs = 500;
89+
int n_epochs = 500;
9090

9191
int train_N = 6;
9292
int test_N = 2;
9393
int n_in = 6;
9494
int n_out = 2;
95-
// int **train_X;
96-
// int **train_Y;
97-
// int **test_X;
98-
// double **test_Y;
99-
100-
// train_X = new int*[train_N];
101-
// train_Y = new int*[train_N];
102-
// for(i=0; i<train_N; i++){
103-
// train_X[i] = new int[n_in];
104-
// train_Y[i] = new int[n_out];
105-
// };
106-
107-
// test_X = new int*[test_N];
108-
// test_Y = new double*[test_N];
109-
// for(i=0; i<test_N; i++){
110-
// test_X[i] = new int[n_in];
111-
// test_Y[i] = new double[n_out];
112-
// }
11395

11496

11597
// training data

go/LogisticRegression.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
type LogisticRegression struct {
9+
N int
10+
n_in int
11+
n_out int
12+
W [][]float64
13+
b []float64
14+
}
15+
16+
17+
func LogisticRegression__construct(this *LogisticRegression, N int, n_in int, n_out int) {
18+
this.N = N
19+
this.n_in = n_in
20+
this.n_out = n_out
21+
22+
this.W = make([][]float64, n_out)
23+
for i := 0; i < n_out; i++ { this.W[i] = make([]float64, n_in) }
24+
25+
this.b = make([]float64, n_out)
26+
}
27+
28+
func LogisticRegression_train(this *LogisticRegression, x []int, y []int, lr float64) {
29+
p_y_given_x := make([]float64, this.n_out)
30+
dy := make([]float64, this.n_out)
31+
32+
for i := 0; i < this.n_out; i++ {
33+
p_y_given_x[i] = 0
34+
for j := 0; j < this.n_in; j++ {
35+
p_y_given_x[i] += this.W[i][j] * float64(x[j])
36+
}
37+
p_y_given_x[i] += this.b[i]
38+
}
39+
LogisticRegression_softmax(this, p_y_given_x)
40+
41+
for i := 0; i < this.n_out; i++ {
42+
dy[i] = float64(y[i]) - p_y_given_x[i]
43+
44+
for j := 0; j < this.n_in; j++ {
45+
this.W[i][j] += lr * dy[i] * float64(x[j]) / float64(this.N)
46+
}
47+
48+
this.b[i] += lr * dy[i] / float64(this.N)
49+
}
50+
51+
}
52+
53+
func LogisticRegression_softmax(this *LogisticRegression, x []float64) {
54+
var (
55+
max float64
56+
sum float64
57+
)
58+
59+
for i := 0; i < this.n_out; i++ { if max < x[i] {max = x[i]} }
60+
for i := 0; i < this.n_out; i++ {
61+
x[i] = math.Exp(x[i] - max)
62+
sum += x[i]
63+
}
64+
65+
for i := 0; i < this.n_out; i++ { x[i] /= sum }
66+
}
67+
68+
func LogisticRegression_predict(this *LogisticRegression, x []int, y []float64) {
69+
for i := 0; i < this.n_out; i++ {
70+
y[i] = 0
71+
for j := 0; j < this.n_in; j++ {
72+
y[i] += this.W[i][j] * float64(x[j])
73+
}
74+
y[i] += this.b[i]
75+
}
76+
77+
LogisticRegression_softmax(this, y)
78+
}
79+
80+
81+
82+
func test_lr() {
83+
84+
learning_rate := 0.1
85+
n_epochs := 500
86+
87+
train_N := 6
88+
test_N := 2
89+
n_in := 6
90+
n_out := 2
91+
92+
93+
// training data
94+
train_X := [][]int {
95+
{1, 1, 1, 0, 0, 0},
96+
{1, 0, 1, 0, 0, 0},
97+
{1, 1, 1, 0, 0, 0},
98+
{0, 0, 1, 1, 1, 0},
99+
{0, 0, 1, 1, 0, 0},
100+
{0, 0, 1, 1, 1, 0},
101+
}
102+
103+
104+
train_Y := [][]int {
105+
{1, 0},
106+
{1, 0},
107+
{1, 0},
108+
{0, 1},
109+
{0, 1},
110+
{0, 1},
111+
}
112+
113+
114+
// construct LogisticRegression
115+
var classifier LogisticRegression
116+
LogisticRegression__construct(&classifier, train_N, n_in, n_out)
117+
118+
// train
119+
for epoch := 0; epoch < n_epochs; epoch++ {
120+
for i := 0; i < train_N; i++ {
121+
LogisticRegression_train(&classifier, train_X[i], train_Y[i], learning_rate)
122+
}
123+
}
124+
125+
// test data
126+
test_X := [][]int {
127+
{1, 0, 1, 0, 0, 0},
128+
{0, 0, 1, 1, 1, 0},
129+
}
130+
131+
test_Y := make([][]float64, test_N)
132+
for i := 0; i < test_N; i++ { test_Y[i] = make([]float64, n_out) }
133+
134+
135+
// test
136+
for i := 0; i < test_N; i++ {
137+
LogisticRegression_predict(&classifier, test_X[i], test_Y[i])
138+
for j := 0; j < n_out; j++ {
139+
fmt.Printf("%f ", test_Y[i][j])
140+
}
141+
fmt.Printf("\n")
142+
}
143+
144+
}
145+
146+
147+
func main() {
148+
test_lr()
149+
}
150+

java/LogisticRegression/src/LogisticRegression.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void predict(int[] x, double[] y) {
6767

6868
private static void test_lr() {
6969
double learning_rate = 0.1;
70-
double n_epochs = 500;
70+
int n_epochs = 500;
7171

7272
int train_N = 6;
7373
int test_N = 2;

0 commit comments

Comments
 (0)