dongzilu0178 2017-09-22 12:13
浏览 50
已采纳

去切片不更新循环外的值

I'm trying to code an Adaline neurone in Go an I'm having a problem with the scoop of an array, I update the values of it inside a for loop and it looks, like they are being updated, but when I try to access the new values from outside the loop they are always the same, they were just updated in the first iteration. Here is the code:

//This functions creates a [][]float64 and fill it with random numbers
weights := initWeights(inputLength)

// data is a [][]float 64 and expectedY a []float64
for i := 0; i < 10; i++ {
    for j := range data {
        //Calculate estimate
        var estimate float64 = 0
        for x := range data[j]{
            estimate += data[j][x] * weights[x]
        }

        // Update weights (range passes values as a copy)
        for x := 0; x < len(weights); x++ {

            weights[x] = learningRate * (expectedY[j] - estimate) * data[j][x]
        }
        //PRINT #1
    }
    //PRINT #2
    //
    //Some more stuff
    // 
}

If I print weights before the loop it looks like this:

[-0.6046602879796196 0.6645600532184904 -0.4246374970712657 0.06563701921747622 0.09696951891448456 -0.5152126285020654 0.21426387258237492 0.31805817433032985 0.28303415118044517]

So it was created correctly. After I start the loops to adjust the neurone weights. Here is where the weird thing happens.

If I print in #1 I can see that the array is being updated in each iteration, but when I print in #2 the value of the array is always the same, it's the one was calculated on the first iteration of the weights loop.

PRINT #1

[0.06725611377611064 0 0 0.03490734755724929 0.014819026508554914 0.023919277971577904 0.021858582731470875 0.0051309928461725374 0.06915084698345737]
[0.030417970260300468 0.0274737201080031 0 0.02479712906046004 0.01662460439529523 0.014007493148808682 0.029246218179487176 0.004413401238393224 0.05947980105651245]
[0.008861875440076036 0 0.01792998206766924 0.017854161778140868 0.004333887749441702 0.020137868898735412 0.0125224790185058 0.008249247500686795 0.030328115811348512]. 

PRINT #2

[0.007796061340871362 0 0.011035383661848988 0.01289960904315235 0.003797667051516503 0.009918694200771232 0.015234505189042204 0.0008236738380263619 0.023072096303259435]
[0.007796061340871362 0 0.011035383661848988 0.01289960904315235 0.003797667051516503 0.009918694200771232 0.015234505189042204 0.0008236738380263619 0.023072096303259435]
[0.007796061340871362 0 0.011035383661848988 0.01289960904315235 0.003797667051516503 0.009918694200771232 0.015234505189042204 0.0008236738380263619 0.023072096303259435]

I've been struggling with this for the last two days and I couldn't figure out what's happening, I hope you guys can help me.

-- UPDATE --
Here is a more complete and runnable version https://play.golang.org/p/qyZGSJSKcs

In play, looks that the code is working fine... the exact same code in my computer outputs the exact same slice every iteration.

The only difference is that instead of fixed slices I'm creating them from two csv files with several hundreds of rows, so I'm guessing the problem comes from there, I'll continue investigating.

Here you have the raw data if it's helpfull:
Train data: https://pastebin.com/H3YgFF0a
Validate data: https://pastebin.com/aeK6krxD

  • 写回答

2条回答 默认 最新

  • doubian19900911 2017-09-23 10:16
    关注

    FOUND IT! It's such a silly thing.. the weights update process is accumulative

    w(i+1) = w(i) + learningRate * (expected - estimated) * data[j][i]
    

    so I just forgot to add the + to the weights assignment

    weights[x] += learningRate * (expectedY[j] - estimate) * data[j][x]
    

    Here is the complete snippet working properly:

    for i := 0; i < cylces; i++ {
        for j := range data {
            //Calculate estimate
            estimate = 0
            for x := range data[j]{
                estimate += data[j][x] * weights[x]
            }
    
            // Update weights (range passes values as a copy)
            for x := 0; x < len(weights); x++ {
                weights[x] += learningRate * (expectedY[j] - estimate) * data[j][x]
            }
        }
    
        errorData = 0
        for j := range data {
            estimate = 0
            for x := range data[j] {
                estimate += data[j][x] * weights[x]
            }
            errorData += (expectedY[j] - estimate) * (expectedY[j] - estimate)
        }
        errorsCyles = append(errorsCyles, errorData / float64(len(data)))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)