Last active
August 29, 2015 14:13
-
-
Save yukihir0/49d4c5d0d7aed738084c to your computer and use it in GitHub Desktop.
Golangでパーセプトロンを実装した。
This file contains hidden or 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
| package main | |
| import ( | |
| "code.google.com/p/plotinum/plot" | |
| "code.google.com/p/plotinum/plotter" | |
| "code.google.com/p/plotinum/plotutil" | |
| "code.google.com/p/plotinum/vg" | |
| "fmt" | |
| "image/color" | |
| ) | |
| func main() { | |
| // a, b, c | |
| w := []float64{0.0, 0.0, 0.0} | |
| // x, y, 1.0 | |
| x := [][]float64{ | |
| {1.0, 0.5, 1.0}, | |
| {2.0, 1.0, 1.0}, | |
| {3.0, 2.5, 1.0}, | |
| {4.0, 3.0, 1.0}, | |
| {0.0, 1.0, 1.0}, | |
| {1.5, 2.0, 1.0}, | |
| {2.0, 3.0, 1.0}, | |
| {3.5, 4.0, 1.0}, | |
| } | |
| t := []float64{ | |
| 1.0, | |
| 1.0, | |
| 1.0, | |
| 1.0, | |
| -1.0, | |
| -1.0, | |
| -1.0, | |
| -1.0, | |
| } | |
| for i := 0; i < 10; i++ { | |
| for j, _ := range x { | |
| w = train(w, x[j], t[j]) | |
| } | |
| } | |
| y := classify(w, []float64{0.0, 0.0, 0.0}) | |
| fmt.Printf("w: %v\n", w) | |
| fmt.Printf("y: %v\n", y) | |
| p, err := plot.New() | |
| if err != nil { | |
| panic(err) | |
| } | |
| p.Title.Text = "Perceptron" | |
| p.X.Label.Text = "X" | |
| p.Y.Label.Text = "Y" | |
| for _, xp := range x { | |
| bs, err := plotter.NewBubbles(plotter.XYZs{{xp[0], xp[1], 1}}, vg.Points(2), vg.Points(2)) | |
| if err != nil { | |
| panic(err) | |
| } | |
| bs.Color = color.RGBA{R: 0, G: 0, B: 255, A: 255} | |
| p.Add(bs) | |
| } | |
| plotutil.AddLinePoints(p, "", plotter.XYs{{0, plane(w, 0)}, {4, plane(w, 4)}}) | |
| if err := p.Save(4, 4, "perception.png"); err != nil { | |
| panic(err) | |
| } | |
| } | |
| func train(w, x []float64, t float64) (nw []float64) { | |
| eta := 0.1 | |
| if classify(w, x) == t { | |
| nw = w | |
| } else { | |
| nw = add(w, multiple(eta*t, x)) | |
| } | |
| return | |
| } | |
| func classify(w, x []float64) (t float64) { | |
| if innerProduct(w, x) >= 0 { | |
| t = 1.0 | |
| } else { | |
| t = -1.0 | |
| } | |
| return | |
| } | |
| func innerProduct(w, x []float64) (f float64) { | |
| if len(w) != len(x) { | |
| panic("not same dimension") | |
| } | |
| for i, _ := range w { | |
| f += w[i] * x[i] | |
| } | |
| return | |
| } | |
| func add(x, y []float64) (z []float64) { | |
| if len(x) != len(y) { | |
| panic("not same dimension") | |
| } | |
| for i, _ := range x { | |
| z = append(z, x[i]+y[i]) | |
| } | |
| return | |
| } | |
| func multiple(a float64, x []float64) (y []float64) { | |
| for i, _ := range x { | |
| y = append(y, a*x[i]) | |
| } | |
| return | |
| } | |
| func plane(w []float64, x float64) (y float64) { | |
| // ax + by + c = 0 | |
| return -w[0]/w[1]*x - w[2]/w[1] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment