Reputation: 235
I'm trying to implement something in sage and I keep getting the following error:
*Error in lines 38-53
Traceback (most recent call last):
File "/projects/42e45a19-7a43-4495-8dcd-353625dfce66/.sagemathcloud/sage_server.py", line 879, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 13, in <module>
File "sage/modules/vector_integer_dense.pyx", line 185, in sage.modules.vector_integer_dense.Vector_integer_dense.__setitem__ (build/cythonized/sage/modules/vector_integer_dense.c:3700)
raise ValueError("vector is immutable; please change a copy instead (use copy())")
ValueError: vector is immutable; please change a copy instead (use copy())*
I have pinpointed the exact location (the line between "print 'marker 1'" and "print 'marker 2'" in the while-loop at the end, see code below) and it seems that I'm not allowed to change the entries of the matrix "weights" (which I defined before the loop) from inside the loop. The error message says to use the copy() function, but I don't see how that would solve my problem since I would only be making a local copy and the next iteration of the loop wouldn't get these changed values, right? So does anyone know how to define this matrix so that I can change it from inside the loop? If it's not possible, can someone explain why?
Thanks for your help.
Code:
m = 3 # Dimension of inputs to nodes
n = 1 # Dimension of output
v = 4 # Number of training vectors
r = 0.1 # Learning Rate
T = 10 # Number of iterations
# Input static Biases, i.e. sum must be smaller than this vector. For dynamic biases, set this vector to 0, increase m by one and set xi[0]=-1 for all inputs i (and start the acual input at xi[1])
bias = list(var('s_%d' % i) for i in range(n))
bias[0] = 0.5
# Input the training vectors and targets
x0 = list(var('s_%d' % i) for i in range(m))
x0[0]=1
x0[1]=0
x0[2]=0
target00=1
x1 = list(var('s_%d' % i) for i in range(m))
x1[0]=1
x1[1]=0
x1[2]=1
target10=1
x2 = list(var('s_%d' % i) for i in range(m))
x2[0]=1
x2[1]=1
x2[2]=0
target20=1
x3 = list(var('s_%d' % i) for i in range(m))
x3[0]=1
x3[1]=1
x3[2]=1
target30=0
targets = matrix(v,n,[[target00],[target10],[target20],[target30]])
g=matrix([x0,x1,x2,x3])
inputs=copy(g)
# Initialize weights, or leave at 0 (i.e.,change nothing)
weights=matrix(m,n)
print weights.transpose()
z = 0
a = list(var('s_%d' % j) for j in range(n))
while(z<T):
Q = inputs*weights
S = copy(Q)
for i in range(v):
y = copy(a)
for j in range(n):
if S[i][j] > bias[j]:
y[j] = 1
else:
y[j] = 0
for k in range(m):
print 'marker 1'
weights[k][j] = weights[k][j] + r*(targets[i][j]-y[j])*inputs[i][k]
print 'marker 2'
print weights.transpose
z +=1
Upvotes: 4
Views: 3341
Reputation: 4402
This is a basic property of Sage vectors - they are immutable Python objects by default.
sage: M = matrix([[2,3],[3,2]])
sage: M[0][1] = 5
---------------------------------------------------------------------------
<snip>
ValueError: vector is immutable; please change a copy instead (use copy())
Notice that the error is that the vector is immutable. That is because you have taken the 0
row, which is a vector (immutable, hashable I guess, etc.).
But if you use the following syntax, you should be golden.
sage: M[0,1] = 5
sage: M
[2 5]
[3 2]
Here you are modifying the element directly. Hope this helps, enjoy Sage!
Upvotes: 8