Python while循环中的变量不会更改
我正在用python做一个自组织映射,就像在教程中一样。它部分工作,但我遇到了一个wierd问题,在我的一个while循环。以下是问题部分的代码:Python while循环中的变量不会更改,python,variables,while-loop,self-organizing-maps,Python,Variables,While Loop,Self Organizing Maps,我正在用python做一个自组织映射,就像在教程中一样。它部分工作,但我遇到了一个wierd问题,在我的一个while循环。以下是问题部分的代码: radius = 15 while radius > 2: #print(radius) while checkW < targetImage.w: while checkH < targetImage.h
radius = 15
while radius > 2:
#print(radius)
while checkW < targetImage.w:
while checkH < targetImage.h:
#print(radius)
nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
if(nodeDistance <= radius):
theta = math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)
checkH = checkH + 1
checkH = 0
checkW = checkW + 1
radius = radius - 1
#print(radius)
radius=15
当半径>2时:
#打印(半径)
当选中w 如果(nodeDistanceOk似乎我错过了循环中的一个关键部分。checkW
和checkW
在每次radius迭代后都不会重置为零,因此循环只会对radius=15
运行一次。在radius迭代结束时添加checkH=0
和checkW=0
后,它修复了问题我的SOM工作得很好
代码如下:
while radius > 2:
#print(radius)
while checkW < targetImage.w:
while checkH < targetImage.h:
#print(radius)
nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
if(nodeDistance <= radius):
theta = math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)
checkH = checkH + 1
checkH = 0
checkW = checkW + 1
radius = radius - 1
checkH = 0
checkW = 0
#print(radius)
半径>2时:
#打印(半径)
当选中w 如果(nodeDistance你确定不仅仅是在循环内打印radius
会将radius
的每个值打印很多次,而且你没有足够深入地查看输出以看到它的变化吗?此外,你真的应该查看的循环。而带有手动调整计数器的循环非常容易出错ne.为什么在checkH=checkH+1
之后立即执行checkH=0
(中间没有任何内容)?基本上,checkW和checkH在矩阵上移动,并相应地更改r、g、b值。因此,对于40x40图像,它类似于(0,0)、(0,1)、(0,2)…当checkH值达到40时,我需要将其重置为零,使其变为(1,0),(1,1),(1,2)…请提供一个示例来说明您的问题。目前,我无法运行您的代码,因为未定义checkW
,checkH
,bmuW
,bmuH
,inputR
,inputB
,math
,以及targetImage
。这里有可以删除的行吗,不改变错误发生的事实?