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
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)))
}
Don't provide partial code with bits removed, just provide a runnable example - the process of doing this may well help you find the problem. Your initWeights function isn't really required for this purpose - better to use known start data.
Here is the incomplete code you have with data added. The algorithm seems to tend towards a certain set of results (presumably, with different data it might just get there quicker within 10 runs, I've upped the runs to 100).
https://play.golang.org/p/IqfCjNtd8a
Are you sure this is not working as intended? Do you have test data with expected results to test it with? I'd expect print 2 to always match print the last print 1, given the code you posted, but it was obviously incomplete.
[EDIT] It's not clear this is a go code problem as opposed to results which surprise you from your algorithm/data.
You need to:
If you can't reproduce with static data, show us how you load the data into the variables, because that is probably where your problem lies. Are you sure you are loading the data you expect and not loading lots of copies of one row for example? Are you sure the algorithm isn't working as intended (if so how)? Your descriptions of results don't match what you have shown us so far.