Created
March 13, 2018 04:22
-
-
Save dotbmp/ff6b0b7b3d4034e9d5798eed0b81bbac to your computer and use it in GitHub Desktop.
incorrect results Odin vs C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://cognitivedemons.wordpress.com/2017/07/06/a-neural-network-in-10-lines-of-c-code/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "core:fmt.odin" | |
import "core:math.odin" | |
import "core:os.odin" | |
exp :: proc[exp32, exp64]; | |
exp32 :: proc(x: f32) -> f32 do return math.pow(math.E, x); | |
exp64 :: proc(x: f64) -> f64 do return math.pow(math.E, x); | |
dot :: proc(m1, m2 : []$T, m1_rows, m1_cols, m2_cols : int) -> []T { | |
output := make([]T, m1_rows*m2_cols); | |
for row := 0; row != m1_rows; row += 1 { | |
for col := 0; col != m2_cols; col += 1 { | |
for k := 0; k != m1_cols; k += 1 { | |
output[row * m2_cols + col] += m1[row * m1_cols + k] * m2[k * m2_cols + col]; | |
} | |
} | |
} | |
return output; | |
} | |
transpose :: proc(m : []$T, C, R : int) -> []T { | |
out := make([]T, C*R); | |
for n := 0; n != C*R; n += 1 { | |
i := n/C; | |
j := n%C; | |
out[n] = m[R*j + i]; | |
} | |
return out; | |
} | |
sigmoid :: proc(m1 : []$T) -> []T { | |
output := make([]T, len(m1)); | |
for i := 0; i != len(m1); i += 1 { | |
output[i] = T(1.0) / (T(1.0) + exp(-m1[i])); | |
} | |
return output; | |
} | |
sigmoid_d :: proc(m1 : []$T) -> []T { | |
output := make([]T, len(m1)); | |
for i := 0; i != len(m1); i += 1 { | |
output[i] = m1[i] * (T(1.0) - m1[i]); | |
} | |
return output; | |
} | |
add :: proc(lhs, rhs : []$T) -> []T { | |
output := make([]T, len(lhs)); | |
for _, i in lhs { | |
output[i] = lhs[i] + rhs[i]; | |
} | |
return output; | |
} | |
sub :: proc(lhs, rhs : []$T) -> []T { | |
output := make([]T, len(lhs)); | |
for _, i in lhs { | |
output[i] = lhs[i] - rhs[i]; | |
} | |
return output; | |
} | |
mul :: proc(lhs, rhs : []$T) -> []T { | |
output := make([]T, len(lhs)); | |
for _, i in lhs { | |
output[i] = lhs[i] * rhs[i]; | |
} | |
return output; | |
} | |
div :: proc(lhs, rhs : []$T) -> []T { | |
output := make([]T, len(lhs)); | |
for _, i in lhs { | |
output[i] = lhs[i] / rhs[i]; | |
} | |
return output; | |
} | |
mod :: proc(lhs, rhs : []$T) -> []T { | |
output := make([]T, len(lhs)); | |
for _, i in lhs { | |
output[i] = lhs[i] % rhs[i]; | |
} | |
return output; | |
} | |
print :: proc(m : []$T, rows, cols : int) { | |
for i := 0; i != rows; i += 1 { | |
for j := 0; j != cols; j += 1 { | |
fmt.print(m[i * cols + j], " "); | |
} | |
fmt.println(); | |
} | |
fmt.println(); | |
} | |
main :: proc() { | |
X := []f32{5.1, 3.5, 1.4, 0.2, | |
4.9, 3.0, 1.4, 0.2, | |
6.2, 3.4, 5.4, 2.3, | |
5.9, 3.0, 5.1, 1.8}; | |
y := []f32{0.0, 0.0, 1.0, 1.0}; | |
W := []f32{0.5, 0.5, 0.5, 0.5}; | |
for i := 0; i != 50; i += 1 { | |
pred := sigmoid(dot(X, W, 4, 4, 1)); | |
pred_error := sub(y, pred); | |
pred_delta := mul(pred_error, sigmoid_d(pred)); | |
W_delta := dot(transpose(X, 4, 4), pred_delta, 4, 4, 1); | |
W = add(W, W_delta); | |
if i == 49 { | |
print(pred, 4, 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment