-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_try.go
98 lines (82 loc) · 2.39 KB
/
nn_try.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt" // Import the neural network package
)
// Test XOR function
func testXOR() {
// XOR training data
trainingData := []map[string][]float64{
{"input": {0, 0}, "output": {0}},
{"input": {0, 1}, "output": {1}},
{"input": {1, 0}, "output": {1}},
{"input": {1, 1}, "output": {0}},
}
// Initialize Neural Network with 2 input, 2 hidden neurons, and 1 output
network := NewNeuralNetwork(2, []int{2}, 1, "relu", 0.1)
// Train the network
network.train(trainingData, 1000)
// Test the network
fmt.Println("TESTING XOR...")
testData := [][]float64{
{0, 0},
{0, 1},
{1, 0},
{1, 1},
}
for _, input := range testData {
prediction := network.forward(input)
fmt.Printf("Input: %v, Prediction: %v\n", input, prediction)
}
}
// Test Polynomial (y = x²)
func testPolynomial() {
// Polynomial data (y = x^2)
polynomialData := []map[string][]float64{}
for x := -5.0; x <= 5.0; x += 0.1 {
polynomialData = append(polynomialData, map[string][]float64{
"input": {x},
"output": {x * x},
})
}
// Initialize Neural Network with 1 input, 5 hidden neurons, and 1 output
network := NewNeuralNetwork(1, []int{5, 5}, 1, "swish", 0.001)
// Train the network
network.train(polynomialData, 10000)
// Test the network
fmt.Println("TESTING Polynomial (y = x^2)...")
for x := -2.0; x <= 2.0; x += 0.4 {
prediction := network.forward([]float64{x})
fmt.Printf("Input: %.2f, Predicted: %.2f\n", x, prediction[0])
}
}
// Test Fibonacci sequence prediction
func testFibonacci() {
// Fibonacci data
fibonacci := []float64{0, 1}
for i := 2; i < 11; i++ {
fibonacci = append(fibonacci, fibonacci[i-1]+fibonacci[i-2])
}
trainingData := []map[string][]float64{}
for i := 0; i < len(fibonacci)-1; i++ {
trainingData = append(trainingData, map[string][]float64{
"input": {fibonacci[i]},
"output": {fibonacci[i+1]},
})
}
// Initialize Neural Network with 1 input, 20 and 10 hidden neurons, and 1 output
network := NewNeuralNetwork(1, []int{20, 10, 10}, 1, "swish", 0.0001)
// Train the network
network.train(trainingData, 20000)
// Test the network
fmt.Println("TESTING Fibonacci sequence prediction...")
for i := 0; i < len(fibonacci)-1; i++ {
prediction := network.forward([]float64{fibonacci[i]})
fmt.Printf("Input: %.0f, Predicted: %.0f\n", fibonacci[i], prediction[0])
}
}
func main() {
// Run all tests
//testXOR()
testPolynomial()
//testFibonacci()
}