Skip to content

Commit

Permalink
Merge pull request stevenmiller888#5 from vibbix/master
Browse files Browse the repository at this point in the history
Added Examples
  • Loading branch information
Steven Miller committed Oct 14, 2015
2 parents 3ba1c9f + ab9b280 commit 2f0cd06
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
Binary file added examples/character-recognition/advanced/A.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/character-recognition/advanced/B.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/character-recognition/advanced/C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/character-recognition/advanced/D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions examples/character-recognition/advanced/advanced.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"github.com/stevenmiller888/go-mind"
"image"
_ "image/png"
"log"
"os"
)

var (
A = ReadImage("A")
B = ReadImage("B")
C = ReadImage("C")
D = ReadImage("D")
)

func main() {
m := mind.New(0.3, 100, 2, "sigmoid")
m.Learn([][][]float64{
{A, mapletter('A')},
{B, mapletter('B')},
{C, mapletter('C')},
{D, mapletter('D')},
})
result := m.Predict([][]float64{C})
log.Println(result) //should be around .50 or so
}

func mapletter(letter byte) []float64 {
if letter == 'A' {
return []float64{0.1}
}
if letter == 'B' {
return []float64{0.3}
}
if letter == 'C' {
return []float64{0.5}
}
if letter == 'D' {
return []float64{0.7}
}
return []float64{0.0}

}

func ReadImage(letter string) []float64 {
file, err := os.Open(letter + ".png") //could be done better with path.join
if err != nil {
log.Fatal("Couldn't open file of letter " + letter)
}
defer file.Close()
//imgread := base64.NewDecoder(base64.StdEncoding, file)
img, _, imgerr := image.Decode(file)
if imgerr != nil {
log.Fatal("Couldn't parse image letter " + letter + "; " + imgerr.Error())
}
bounds := img.Bounds()
flt := make([]float64, ((bounds.Max.Y - bounds.Min.Y) * (bounds.Max.X - bounds.Min.X) * 4))
i := -1 //starts at negative, to iterate
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
i++
flt[i] = (float64(r) / 255.0)
i++
flt[i] = (float64(g) / 255.0)
i++
flt[i] = (float64(b) / 255.0)
i++
flt[i] = (float64(a) / 255.0)
}
}
return flt
}
78 changes: 78 additions & 0 deletions examples/character-recognition/simple/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"github.com/stevenmiller888/go-mind"
"log"
)

var (
a = character(
".#####." +
"#.....#" +
"#.....#" +
"#######" +
"#.....#" +
"#.....#" +
"#.....#")
b = character(
"######." +
"#.....#" +
"#.....#" +
"######." +
"#.....#" +
"#.....#" +
"######.")
c = character(
"#######" +
"#......" +
"#......" +
"#......" +
"#......" +
"#......" +
"#######")
)

func main() {
m := mind.New(0.3, 10000, 3, "sigmoid")
m.Learn([][][]float64{
{c, mapletter('c')},
{b, mapletter('b')},
{a, mapletter('a')},
})

result := m.Predict([][]float64{
character(
"#######" +
"#......" +
"#......" +
"#......" +
"#......" +
"##....." +
"#######")})
log.Println(result)
//Result should be somewhere around .5
}
func character(chars string) []float64 {
flt := make([]float64, len(chars))
for i := 0; i < len(chars); i++ {
if chars[i] == '#' {
flt[i] = 1.0
} else { // if '.'
flt[i] = 0.0
}
}
return flt
}

func mapletter(letter byte) []float64 {
if letter == 'a' {
return []float64{0.1}
}
if letter == 'b' {
return []float64{0.3}
}
if letter == 'c' {
return []float64{0.5}
}
return []float64{0.0}
}
22 changes: 22 additions & 0 deletions examples/xor/xor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/stevenmiller888/go-mind"
"log"
)

func main() {
m := mind.New(0.3, 10000, 3, "sigmoid")

m.Learn([][][]float64{
{{0, 0}, {0}},
{{0, 1}, {1}},
{{1, 0}, {1}},
{{1, 1}, {0}},
})

result := m.Predict([][]float64{
{0, 1},
})
log.Println(result) //should be close to 1.0
}

0 comments on commit 2f0cd06

Please sign in to comment.