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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么