Skip to content

Commit

Permalink
следующие шаги, загрузка выгрузка сети
Browse files Browse the repository at this point in the history
  • Loading branch information
kpmy committed Sep 8, 2015
1 parent 185d52c commit 10f1dc3
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 503 deletions.
232 changes: 0 additions & 232 deletions gone.in/main.go

This file was deleted.

File renamed without changes
120 changes: 77 additions & 43 deletions gone.to/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package main

import (
"encoding/json"
"github.com/kpmy/ypk/assert"
"gone/perc"
"gone/perc/std"
"io"
"log"
"math/rand"
"os"
)

const (
Nsens = 256 * 256
Nsens = 64 * 64
MaxSensOut = 128
Nass = 256
Nreact = 16
Expand All @@ -19,67 +23,97 @@ func init() {
log.SetFlags(0)
}

func New(nn, no int, w_fn func() int) (ret *model.Layer) {
ret = &model.Layer{}
for i := 0; i < nn; i++ {
next := model.NewNode()
next.Out = make([]interface{}, rand.Intn(no)+1)
next.Weights = make([]int, len(next.Out))
if w_fn != nil {
for i := 0; i < len(next.Weights); i++ {
next.Weights[i] = w_fn()
type Link struct {
NodeId int `json:"node"`
LinkId int `json:"out"`
}

type NodeStruct struct {
Outs []int `json:"out"`
Ins []Link `json:"in,omitempty"`
}

type LayerStruct struct {
Nodes []*NodeStruct `json:"node"`
}

func Store(wr io.Writer, first *model.Layer) error {
res := make([]*LayerStruct, 0)
for l := first; l != nil; l = l.Next {
next := &LayerStruct{}
for _, n := range l.Nodes {
node := &NodeStruct{}
for _, w := range n.Weights {
node.Outs = append(node.Outs, w)
}
for k, _ := range n.In {
link := Link{NodeId: k.NodeId, LinkId: k.LinkId}
node.Ins = append(node.Ins, link)
}
next.Nodes = append(next.Nodes, node)
}
ret.Nodes = append(ret.Nodes, next)
res = append(res, next)
}
return
return json.NewEncoder(wr).Encode(res)
}

func Join(in *model.Layer, out *model.Layer) {
j := 0
type im map[int]interface{}
cache := make([]im, len(out.Nodes))
for k, n := range in.Nodes {
for i := 0; i < len(n.Out); {
assert.For(len(n.Out) <= len(out.Nodes), 20, len(n.Out), len(out.Nodes))
l := model.Link{NodeId: k, LinkId: i}
skip := false
if cache[j] != nil {
if _, ok := cache[j][k]; ok {
skip = true
j++
if j == len(cache) {
j = 0
}
}
func Load(rd io.Reader) (ret *model.Layer, err error) {
ll := make([]LayerStruct, 0)
if err = json.NewDecoder(rd).Decode(ll); err == nil {
var this *model.Layer
for _, l := range ll {
if ret == nil {
this = &model.Layer{}
ret = this
} else {
this.Next = &model.Layer{}
this = this.Next
}
if !skip {
out.Nodes[j].In[l] = nil
if cache[j] == nil {
cache[j] = make(im)
for _, n := range l.Nodes {
node := model.NewNode()
for _, w := range n.Outs {
node.Weights = append(node.Weights, w)
}
cache[j][k] = l
//log.Println(l, "to", j)
i++
node.Out = make([]interface{}, len(node.Weights))
for _, l := range n.Ins {
link := model.Link{LinkId: l.LinkId, NodeId: l.NodeId}
node.In[link] = nil
}
this.Nodes = append(this.Nodes, node)
}
//log.Println(k, len(n.Out), i, j)
}
}
in.Next = out
return
}

func main() {
s := New(Nsens, MaxSensOut, func() int {
s := std.New(Nsens, MaxSensOut, func() int {
x := rand.Intn(2)
if x == 0 {
return 1
} else {
return -1
}
})
a := New(Nass, Nreact, func() int { return rand.Intn(201) - 100 })
r := New(Nreact, 1, nil)
a := std.New(Nass, Nreact, func() int { return rand.Intn(201) - 100 })
r := std.New(Nreact, 1, nil)

std.Join(s, a)
std.Join(a, r)

log.Println(s)

if f, err := os.Create("0.pc"); err == nil {
Store(f, s)
f.Close()
}

if f, err := os.Open("0.pc"); err == nil {
if s, err = Load(f); err != nil {
log.Fatal(err)
}
f.Close()
}

Join(s, a)
Join(a, r)
log.Println(s)
}
File renamed without changes
Loading

0 comments on commit 10f1dc3

Please sign in to comment.