forked from stevenmiller888/go-mind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stevenmiller888#5 from vibbix/master
Added Examples
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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 | ||
} |
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
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} | ||
} |
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
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 | ||
} |