Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks committed Nov 29, 2019
0 parents commit 3471a1f
Show file tree
Hide file tree
Showing 7 changed files with 585 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Lucas Barrena

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# neural-go

Neural networks (deep feedforward)

![](https://img.shields.io/github/go-mod/go-version/LuKks/neural-go) [![](https://img.shields.io/maintenance/yes/2019.svg?style=flat-square)](https://github.com/LuKks/neural-go) ![](https://img.shields.io/github/size/LuKks/neural-go/index.go.svg) ![](https://img.shields.io/github/downloads/LuKks/neural-go/total) ![](https://img.shields.io/github/license/LuKks/neural-go.svg)

```golang
package main

import (
"fmt"
"github.com/lukks/neural-go"
)

func main () {
rgb := neural.NewNeural([]neural.Layer{
neural.NewInputLayer(2, 8), // (inputs, neurons) is also a hidden layer
neural.NewHiddenLayer(8),
neural.NewOutputLayer(1),
})

for i := 0; i <= 5000; i++ {
mse := rgb.LearnRaw([]float64{ 0.0, 0.0 }, []float64{ 0.0 }, 0.2)
mse += rgb.LearnRaw([]float64{ 1.0, 0.0 }, []float64{ 1.0 }, 0.2)
mse += rgb.LearnRaw([]float64{ 0.0, 1.0 }, []float64{ 1.0 }, 0.2)
mse += rgb.LearnRaw([]float64{ 1.0, 1.0 }, []float64{ 0.0 }, 0.2)
mse /= 4;

if i % 1000 == 0 {
fmt.Printf("iter %v, mse %f\n", i, mse)
}
}

fmt.Printf("0, 0 [0] -> %f\n", rgb.Think([]float64{ 0.0, 0.0 }))
fmt.Printf("1, 0 [1] -> %f\n", rgb.Think([]float64{ 1.0, 0.0 }))
fmt.Printf("0, 1 [1] -> %f\n", rgb.Think([]float64{ 0.0, 1.0 }))
fmt.Printf("1, 1 [0] -> %f\n", rgb.Think([]float64{ 1.0, 1.0 }))
}
```

## Import
```golang
import "github.com/lukks/neural-go"
```

## Features
#### Ranges
Set a range of values for every input and output.\
So you use your values as you know but the neural get the values in raw (0-1).\
Check [examples/full.go](https://github.com/LuKks/neural-go/blob/master/examples/full.go) for usage example.

#### Description
From my previous [neural-amxx](https://github.com/LuKks/neural-amxx).

#### Structs
```golang
type Neuron struct
type Layer struct
type Neural struct
```

#### Methods
```golang
// low-level usage:
func NewNeuron (maxInputs int) Neuron
func (neuron *Neuron) Think (inputs []float64) float64

func NewLayer (maxNeurons int, maxInputs int) Layer
func (layer *Layer) Think (inputs []float64) []float64

func NewNeural (layers []Layer) Neural
func (neural *Neural) ThinkRaw (inputs []float64) []float64
func (neural *Neural) LearnRaw (inputs []float64, outputs []float64, rate float64) float64

// high-level usage:
func NewInputLayer (maxInputs int, maxNeurons int) Layer
func NewHiddenLayer (maxNeurons int) Layer
func NewOutputLayer (maxNeurons int) Layer
func (neural *Neural) InputRange (index int, min float64, max float64)
func (neural *Neural) OutputRange (index int, min float64, max float64)
func (neural *Neural) Think (inputs []float64) []float64
func (neural *Neural) Learn (inputs []float64, outputs []float64, rate float64) float64
```

#### Missing
Some methods that are not available yet:
```golang
func (neural *Neural) Save ()
func (neural *Neural) Load ()
func (neural *Neural) Delete ()
func (neural *Neural) Reset ()
```

## Examples
Basic XOR [examples/xor.go](https://github.com/LuKks/neural-go/blob/master/examples/xor.go)\
RGB brightness [examples/full.go](https://github.com/LuKks/neural-go/blob/master/examples/full.go)

```
go run examples/full.go
```

## Tests
```
There are no tests yet
```

## License
Code released under the [MIT License](https://github.com/LuKks/neural-go/blob/master/LICENSE).
95 changes: 95 additions & 0 deletions examples/full.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"fmt"
"time"
"github.com/lukks/neural-go"
)

const DebugColor = "\033[0;36m%s\033[0m"

func main () {
rgb := neural.NewNeural([]neural.Layer{
neural.NewLayer(32, 3),
neural.NewLayer(1, 0),
})

rgb.InputRange(0, 0.0, 255.0)
rgb.InputRange(1, 0.0, 255.0)
rgb.InputRange(2, 0.0, 255.0)
rgb.OutputRange(0, 0.0, 1.0)

fmt.Printf(DebugColor, "benchmark\n")
benchmark(&rgb)

fmt.Printf(DebugColor, "think some values\n")
fmt.Printf("255, 255, 255 [1.0] -> %f\n", rgb.Think([]float64{ 255.0, 255.0, 255.0 }))
fmt.Printf("0 , 0 , 0 [0.0] -> %f\n", rgb.Think([]float64{ 0.0, 0.0, 0.0 }))

fmt.Printf(DebugColor, "learning\n")
for i := 0; i <= 5000; i++ {
var rate float64 = 0.1
var mse float64

light := []float64{ 1.0 }
dark := []float64{ 0.0 }

mse += rgb.LearnRaw([]float64{ 1.0, 0.0, 0.0 }, []float64{ 1.0 }, rate)
mse += rgb.Learn([]float64{ 0.0, 255.0, 0.0 }, light, rate)
mse += rgb.Learn([]float64{ 0.0, 0.0, 255.0 }, light, rate)
mse += rgb.Learn([]float64{ 0.0, 0.0, 0.0 }, dark, rate)
mse += rgb.Learn([]float64{ 100.0, 100.0, 100.0 }, light, rate)
mse += rgb.Learn([]float64{ 107.0, 181.0, 255.0 }, dark, rate)
mse += rgb.Learn([]float64{ 0.0, 53.0, 105.0 }, dark, rate)
mse += rgb.Learn([]float64{ 150.0, 150.0, 75.0 }, light, rate)
mse += rgb.Learn([]float64{ 75.0, 75.0, 0.0 }, dark, rate)
mse += rgb.Learn([]float64{ 0.0, 75.0, 75.0 }, dark, rate)
mse += rgb.Learn([]float64{ 150.0, 74.0, 142.0 }, light, rate)
mse += rgb.Learn([]float64{ 50.0, 50.0, 75.0 }, dark, rate)
mse += rgb.Learn([]float64{ 103.0, 22.0, 94.0 }, dark, rate)
mse /= 13;

if mse < 0.01 {
fmt.Printf("mse threshold on iter %v\n", i)
break
}

if i % 1000 == 0 {
fmt.Printf("iter %v, mse %f\n", i, mse)
}
}

fmt.Printf(DebugColor, "think some values\n")
fmt.Printf("255, 255, 255 [1.0] -> %f\n", rgb.Think([]float64{ 255.0, 255.0, 255.0 }))
fmt.Printf("0 , 0 , 0 [0.0] -> %f\n", rgb.Think([]float64{ 0.0, 0.0, 0.0 }))

fmt.Printf(DebugColor, "think new values\n")
fmt.Printf("243, 179, 10 [1.0] -> %f\n", rgb.ThinkRaw([]float64{ 0.952, 0.701, 0.039 }))
fmt.Printf("75 , 50 , 50 [0.0] -> %f\n", rgb.Think([]float64{ 75.0, 50.0, 50.0 }))
fmt.Printf("95 , 99 , 104 [1.0] -> %f\n", rgb.Think([]float64{ 95.0, 99.0, 104.0 }))
fmt.Printf("65 , 38 , 70 [0.0] -> %f\n", rgb.Think([]float64{ 65.0, 38.0, 70.0 }))
}

func benchmark (rgb *neural.Neural) {
var start, end int64

start = nowMillis()
for i := 0; i < 200000; i++ { // this takes 0.164 seconds
// then this takes ~0.00000082s
rgb.ThinkRaw([]float64{ 1.0, 1.0, 1.0 })
}
end = nowMillis()
fmt.Printf("think raw %v millis\n", end - start)

start = nowMillis()
for i := 0; i < 200000; i++ { // this takes 0.184 seconds
// then this takes ~0.00000092s
rgb.Think([]float64{ 255.0, 255.0, 255.0 })
}
end = nowMillis()
fmt.Printf("think %v millis\n", end - start)
}

func nowMillis() int64 {
return time.Now().UnixNano() / 1e6
}
71 changes: 71 additions & 0 deletions examples/learning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
)

type AnyNeuron struct {
id int
}

type Neuron struct {
AnyNeuron
name string
weights [16]float64
}

func main () {
// float prec
a := 0.1
b := 0.2
fmt.Println(a + b)

// arrays
var arr1 = [3]int{2, 4, 8}
fmt.Println(arr1)

var arr2 = [...]int{2, 4, 8}
fmt.Println(arr2)

// arr multi dim
var arr3 [3][3]int = [3][3]int{ [3]int{1, 0, 0}, [3]int{0, 1, 0}, [3]int{0, 0, 1} }
fmt.Println(arr3)

var arr4 [3][3]int
arr4[0] = [3]int{1, 0, 0}
arr4[1] = [3]int{0, 1, 0}
arr4[2] = [3]int{0, 0, 1}
fmt.Println(arr4)

// slices
arr5 := []int{1, 2, 3}
fmt.Println(len(arr5))
fmt.Println(cap(arr5))

// slices are by default referenced
// arrays must use &

// slice
// res51 := arr5[:] // all
// res52 := arr5[3:] // from 4th
// res53 := arr5[:6] // to 6th

// maps
state := make(map[string]int)
state = map[string]int{
"abc": 111,
"def": 222,
}
val, ok := state["ab"]
fmt.Println(val, ok)

neuron1 := Neuron{
name: "n1",
}
fmt.Println(neuron1)

neuron2 := struct{name string}{
name: "n2",
}
fmt.Println(neuron2)
}
40 changes: 40 additions & 0 deletions examples/xor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"github.com/lukks/neural-go"
)

func main () {
rgb := neural.NewNeural([]neural.Layer{
neural.NewLayer(8, 2), // NewLayer(neurons, inputs)
neural.NewLayer(8, 0), // autorecognize previous layer, the 0 will be 8
neural.NewLayer(1, 0), // due the inputs are the neurons of previous layer
})

fmt.Println("think some values:")
fmt.Printf("0, 0 [0] -> %f\n", rgb.Think([]float64{ 0.0, 0.0 }))
fmt.Printf("1, 0 [1] -> %f\n", rgb.Think([]float64{ 1.0, 0.0 }))
fmt.Printf("0, 1 [1] -> %f\n", rgb.Think([]float64{ 0.0, 1.0 }))
fmt.Printf("1, 1 [0] -> %f\n", rgb.Think([]float64{ 1.0, 1.0 }))

fmt.Println("learning:")
for i := 0; i <= 5000; i++ {
rate := 0.4
mse := rgb.LearnRaw([]float64{ 0.0, 0.0 }, []float64{ 0.0 }, rate)
mse += rgb.LearnRaw([]float64{ 1.0, 0.0 }, []float64{ 1.0 }, rate)
mse += rgb.LearnRaw([]float64{ 0.0, 1.0 }, []float64{ 1.0 }, rate)
mse += rgb.LearnRaw([]float64{ 1.0, 1.0 }, []float64{ 0.0 }, rate)
mse /= 4;

if i % 1000 == 0 {
fmt.Printf("iter %v, mse %f\n", i, mse)
}
}

fmt.Println("think some values:")
fmt.Printf("0, 0 [0] -> %f\n", rgb.Think([]float64{ 0.0, 0.0 }))
fmt.Printf("1, 0 [1] -> %f\n", rgb.Think([]float64{ 1.0, 0.0 }))
fmt.Printf("0, 1 [1] -> %f\n", rgb.Think([]float64{ 0.0, 1.0 }))
fmt.Printf("1, 1 [0] -> %f\n", rgb.Think([]float64{ 1.0, 1.0 }))
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lukks/neural-go

go 1.13
Loading

0 comments on commit 3471a1f

Please sign in to comment.