Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

go - Why does pointer assignment cause variable assignment to not always stick?

Pointer assignment of index is being consistently inconsistent within addData(..). I expect memory address is moving around as underlying array increases in size.

Behavior: I assign to variable A, then assign B = A*0.2, then assign y = sig(B), finally B = y. Sometimes on the next loop B == y || B == A*0.2. It is perfectly consistent across multiple executions.

I made a simpler and more full version of the code.

package main

import(
    "fmt"
    "math"
)

func main(){
    //Structure setup
    l := lots{}; l.addData(2); l.addData(2); l.addData(2); l.addData(2)
    
    l.val[0].y[0] = 0.20700021
    l.val[0].y[1] = 0.30003001
    l.propagate()
}

type data struct{
    y []float64
}

type pair struct {
    one *data
    two *data
}

// lots is the biggest part of the structure
// the problem seems to occure when this is introduced
type lots struct{
    val []data
    join []pair
}

// addData appends a data struct and a pair struct to
// the corresponding parts of lots struct
func (l *lots)addData(size int){
    l.val = append(l.val, data{make([]float64, size)})
    
    // should be skipped first call only
    if(len(l.join) < len(l.val)-1){
        fmt.Println("len of l.val: ", len(l.val)) 
        l.join = append(l.join, pair{})
        l.join[len(l.join)-1].one = &l.val[len(l.val)-2]
        l.join[len(l.join)-1].two = &l.val[len(l.val)-1]
    }
}

// propagate 
func (l *lots)propagate(){
    for _, v := range l.join{
        v.travel()
    }
}

// travel modifies values going from p.one -> p.two
func (p *pair) travel(){
    fmt.Println("p.one.y: ", p.one.y)
    p.mathy()
    fmt.Println("p.two.y: ", p.two.y)
    
    p.two.y = sigmoid(p.two.y)
    fmt.Println("p.two.y: ", p.two.y)
}

func (p *pair) mathy(){
    for i := range p.one.y {
        p.two.y[i] = p.one.y[i] * p.one.y[i]
    }
}

// sigmoid seems to be causing some problems.
// Works fine on it's own though
func sigmoid(x []float64)(y []float64){
    y = make([]float64, len(x))
    for i := range x{
        y[i] = 1./(1.+math.Exp(-x[i]))
    }
    return
}

I expect the #'s of p.two.y: [#'s] to equal the following lines #'s of p.one.y: [#'s] all the time. The output I am getting is not consistently equal. Sometimes the p.one.y: [#'s] #'s are equal to the p.two.y: [#'s] from the line preceding line; that value was over written and then that value came back.

p.one.y:  [0.20700021 0.30003001]
p.two.y:  [0.04284908694004409 0.0900180069006001]/////// bad
p.two.y:  [0.5107106330188076 0.5224893174114301]      // overwritten
p.one.y:  [0.04284908694004409 0.0900180069006001]/////// reappeared
p.two.y:  [0.0018360442515954571 0.008103241566356488]
p.two.y:  [0.5004590109339528 0.5020257993066767]//// overwritten
p.one.y:  [0.5004590109339528 0.5020257993066767]//// good
p.two.y:  [0.25045922162499035 0.25202990316950763]
p.two.y:  [0.5622895277500193 0.5626760660176802]

I have tried to reduce function nesting, and it worked when I put everything into the Propagate() function and assigned directly to p.two.y[i] with the sigmoid function. (below)

// propagate 
func (l *lots)propagate(){
    for _, p := range l.join{
        fmt.Println("p.one.y: ", p.one.y)
        
        for i := range p.one.y {
            p.two.y[i] = p.one.y[i] * p.one.y[i]
        }
        fmt.Println("p.two.y: ", p.two.y)
        
        // using this extra variable causes the problem of inconsistent assignment
        //y := make([]float64, len(p.two.y))
        for i := range p.two.y{
            //y[i] = 1./(1.+math.Exp(-p.two.y[i]))
            p.two.y[i] = 1./(1.+math.Exp(-p.two.y[i]))
        }
        //p.two.y = y
        
        fmt.Println("p.two.y: ", p.two.y)
    }
}

This version provides good data, but takes away so much of the specialization I like.

p.one.y:  [0.20700021 0.30003001]
p.two.y:  [0.04284908694004409 0.0900180069006001]
p.two.y:  [0.5107106330188076 0.5224893174114301]////
p.one.y:  [0.5107106330188076 0.5224893174114301]//// Good
p.two.y:  [0.2608253506784712 0.27299508680906215]
p.two.y:  [0.564839170446528 0.5678280461350629]////
p.one.y:  [0.564839170446528 0.5678280461350629]//// Good
p.two.y:  [0.3190432884707219 0.3224286899775631]
p.two.y:  [0.5790910765397528 0.5799160282084651]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...