gpt4 book ai didi

immutability - Sage 不可变向量错误

转载 作者:行者123 更新时间:2023-12-04 05:25:33 33 4
gpt4 key购买 nike

我正在尝试在 sage 中实现一些东西,但我不断收到以下错误:

*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())*

我已经确定了确切的位置(最后在 while 循环中“print 'marker 1'”和“print 'marker 2'”之间的线,见下面的代码),似乎我不允许更改来自循环内部的矩阵“权重”(我在循环之前定义)的条目。该错误消息说,使用副本()函数,但我不明白怎么会解决我的问题,因为我只会做一个本地副本,并在循环的下一次迭代不会得到这些更改后的值,对不对?那么有谁知道如何定义这个矩阵,以便我可以从循环内部更改它?如果不可能,有人可以解释原因吗?

谢谢你的帮助。

代码:
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

最佳答案

这是 Sage 向量的基本属性 - 默认情况下,它们是不可变的 Python 对象。

sage: M = matrix([[2,3],[3,2]])
sage: M[0][1] = 5
---------------------------------------------------------------------------
<snip>
ValueError: vector is immutable; please change a copy instead (use copy())

请注意,错误在于向量是不可变的。那是因为你拿了 0行,它是一个向量(我猜是不可变的、可散列的,等等)。

但是如果你使用下面的语法,你应该是黄金。
sage: M[0,1] = 5
sage: M
[2 5]
[3 2]

这里是直接修改元素。希望这会有所帮助,享受 Sage!

关于immutability - Sage 不可变向量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300290/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com