我正在写一个非常简单的随机漫步功能。整个代码如下。当当前成本函数小于前一个时,我使用数组来跟踪参数值。但由于某种原因,跟踪输出的数组是重写?以前的条目。我认为这与将参数附加为" c"有关,因此存在内存分配问题,因为" c"是附加而不是c的值,但我不知道如何解决这个问题,也不知道为什么它会与成本函数值psi不同,psi会追加并维持其值。
我试图创建类似的东西:
input:
for n in N:
if cost < previous cost: keep current parameters
write current parameters & cost to tracking list
output:
n = 0, parameters = [1,2,3], cost = 4.5, track = [([1,2,3], 4.5)]
n = 1, parameters = [1.1,2.4,2.7], cost = 3.9, track = [([1,2,3], 4.5), ([1.1, 2.4, 2.7], 3.9)]
etc
相反,我得到的是
output:
n = 0, parameters = [1,2,3], cost = 4.5, track = [([1,2,3], 4.5)]
n = 1, parameters = [1.1,2.4,2.7], cost = 3.9, track = [([1.1,2.4,2.7], 4.5), ([1.1, 2.4, 2.7], 3.9)]
所以它替换了参数,但保留了以前的成本。当track函数也在成功(低成本)if语句之外时,会发生这种情况。
帮忙?
#pure random walk where all c parameters are randomly updated at once; no restrictions on pos/neg of parameters
del track
del bigtrack
c = [5,5,-5]
cp = c
kB = -8
M = 10
N = 5000
bigtrack = []
psip = cost(LN, LB, dldtN, dldtB, c[2], kB, c[0], c[1]) #cost using initialized parameters
for m in range(M):
track = []
for n in range(N):
for i in range(len(c)):
rand = np.random.uniform(-1,1)
c[i] = c[i] + rand
#print(c[i])
#print("parameters = ", c)
psi = cost(LN, LB, dldtN, dldtB, c[2], kB, c[0], c[1])
#print("new cost = ", psi)
if psi < psip:
cp = c
resp = res
psip = psi
track.append((c, psi))
print("tracking function: \n", track)
else:
c = cp #keep prior
res = resp
psi = psip
if psi <16:
print("VICTORY")
break
#print(track)
#if track != []:
bigtrack.append((track, m))
这是一个示例输出
tracking function:
[([4.145180382591114, 5.194803875207629, -5.77417154313107], 457.37070071446328)]
tracking function:
[([4.366620027701061, 5.610886161756634, -6.300451985366614], 457.37070071446328), ([4.366620027701061, 5.610886161756634, -6.300451985366614], 406.27520117896)]
tracking function:
[([4.294900222966394, 6.104004008230176, -6.636360393709489], 457.37070071446328), ([4.294900222966394, 6.104004008230176, -6.636360393709489], 406.27520117896), ([4.294900222966394, 6.104004008230176, -6.636360393709489], 377.1702411076343)]
答案 0 :(得分:0)
在插入之间修改结果时,您会在结果中插入相同的list
。你有效地做了以下事情:
>>> track = []
>>> c = [1, 2, 3]
>>> track.append(c)
>>> track
[[1, 2, 3]]
>>> c[1] += 2
>>> track.append(c)
>>> track
[[1, 4, 3], [1, 4, 3]]
为了解决问题,您可以在每次附加到结果时复制列表:
track.append((c[:], psi)) # instead of track.append((c, psi))