Solved by ptrblck in post #6 
              
              
                You can read a nice explanation of leaf variables in this post. 
Usually you have to avoid modifying a leaf variable with an in-place operation. 
I haven’t seen your error message yet, but it seems it related to an in-place modification, because you are re-assigning the values of W in this line: 
W[…