Git Product home page Git Product logo

neuro's Introduction

Neural network

- Training with back propagation

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"reflect"
	"runtime"
	"time"

	"github.com/alexber1277/neuro"
)

const (
	fileNameDump = "basenet.data"
)

var inpData = []neuro.DataTeach{
	neuro.DataTeach{[]float64{1, 2, 3}, []float64{1}},
	neuro.DataTeach{[]float64{2, 3, 4}, []float64{0}},
	neuro.DataTeach{[]float64{3, 4, 5}, []float64{1}},
	neuro.DataTeach{[]float64{4, 5, 6}, []float64{0}},
	neuro.DataTeach{[]float64{5, 6, 7}, []float64{1}},
}

func init() {
	rand.Seed(time.Now().UnixNano())
	runtime.GOMAXPROCS(runtime.NumCPU())
}

func main() {
	net, err := neuro.LoadNet(fileNameDump) // load net by filename

	if err != nil {
		log.Println(err)
		net = neuro.InitNetPerc(2, 120) // initialisation (2 layers by 120 neurons)
		net.LRate(0.01)                 // learning rate
		net.CreateNet(inpData, 1000)    // set teach data and epoch
		net.Train(100)                  // show result by (n) iteration
	}

	net.CalcStat(100).GetStat() // check result by random teach data and get statistics

	debug(net.Result) // show result  {"accuracy":100,"false":0,"true":100}
	debug(net.Error)  // result error percent
	debug(net)        // result error percent

	if net.Accuracy(99.0) { // set minimal right answers percent
		net.Save(fileNameDump) // save dump
	}
}

func debug(in interface{}) {
	bt, err := json.Marshal(in)
	if err != nil {
		log.Fatal("error debug: ", err)
	}
	println(string(bt))
}

- Training with genetic algorithm

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"reflect"
	"runtime"
	"time"

	"github.com/alexber1277/neuro"
)

const (
	fileNameDump = "basenet.data"
)

var inpData = []neuro.DataTeach{
	neuro.DataTeach{[]float64{1, 2, 3}, []float64{1}},
	neuro.DataTeach{[]float64{2, 3, 4}, []float64{0}},
	neuro.DataTeach{[]float64{3, 4, 5}, []float64{1}},
	neuro.DataTeach{[]float64{4, 5, 6}, []float64{0}},
	neuro.DataTeach{[]float64{5, 6, 7}, []float64{1}},
}

func init() {
	rand.Seed(time.Now().UnixNano())
	runtime.GOMAXPROCS(runtime.NumCPU())
}

func main() {

	gen := neuro.InitGenetic()
	gen.Add(func() *neuro.NetPerc {
		return neuro.InitNetPerc(2, 60).
			SetWeight(gen.Config.MinRandWeight, gen.Config.MaxRandWeight).
			LRate(0.1).
			CreateNet(inpData, 1)
	})

	gen.Error = 1
	for i := 0; gen.Error > gen.Config.BestResult; i++ {
		gen.Train(false)
		log.Println("iter:", i, "best result percent:", gen.Error)
	}

	n := gen.GetBest()

	for _, el := range inpData {
		fmt.Println(
			el.Inputs, "\t",
			n.PredictClear(el.Inputs), "\t",
			el.Outputs, "\t",
			reflect.DeepEqual(n.PredictClear(el.Inputs), el.Outputs),
		)
	}
}

func debug(in interface{}) {
	bt, err := json.Marshal(in)
	if err != nil {
		log.Fatal("error debug: ", err)
	}
	println(string(bt))
}

neuro's People

Contributors

alexber127 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.