繁体   English   中英

为什么我的变量被 function 覆盖,即使它是间接引用的?

[英]Why is my variable being overwritten by the function even though it's indirectly referenced?

为什么这段代码:

import random, decimal

data = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3333333333333333, 0.05555555555555555, 0.05555555555555555, 0.05555555555555555, 0.007936507936507936, 0.007352941176470588, 0.005714285714285714, 0.038461538461538464, 0.006024096385542169, 0.00392156862745098, 0.004048582995951417, 0.007874015748031496, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03333333333333333, 0.027777777777777776, 0.010638297872340425, 0.006493506493506494, 0.0058823529411764705, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.0044444444444444444, 0.005813953488372093, 0.003952569169960474, 0.004132231404958678, 0.005128205128205128, 0.015625, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.02040816326530612, 0.004201680672268907, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.00398406374501992, 0.010752688172043012, 0.012195121951219513, 0.012195121951219513, 0.017857142857142856, 0.02564102564102564, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.0, 0.05555555555555555, 0.0045662100456621, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.005050505050505051, 0.005494505494505495, 0.004048582995951417, 0.004149377593360996, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0125, 0.00641025641025641, 0.009345794392523364, 0.003952569169960474, 0.003952569169960474, 0.004878048780487805, 0.09090909090909091, 
0.0, 0.023255813953488372, 0.006493506493506494, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07142857142857142, 1.0, 0.006493506493506494, 0.003952569169960474, 0.011111111111111112, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.007194244604316547, 0.003952569169960474, 0.005263157894736842, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09090909090909091, 0.005263157894736842, 0.003952569169960474, 0.014285714285714285, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.02857142857142857, 0.004149377593360996, 0.0044444444444444444, 0.00625, 0.009259259259259259, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.012345679012345678, 0.004166666666666667, 0.003952569169960474, 0.003952569169960474, 0.008403361344537815, 0.04, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.022222222222222223, 0.005376344086021506, 0.003952569169960474, 0.003952569169960474, 0.006666666666666667, 0.037037037037037035, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0625, 0.010752688172043012, 0.003968253968253968, 0.003952569169960474, 0.0053475935828877, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.004016064257028112, 0.003952569169960474, 0.004016064257028112, 0.015625, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.021739130434782608, 0.007692307692307693, 0.00546448087431694, 0.003952569169960474, 0.003952569169960474, 0.004830917874396135, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.02564102564102564, 0.006756756756756757, 0.004366812227074236, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.004, 0.005494505494505495, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.041666666666666664, 0.008771929824561403, 0.004524886877828055, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.004975124378109453, 0.01282051282051282, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.043478260869565216, 0.015151515151515152, 0.004694835680751174, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.005050505050505051, 0.012345679012345678, 
0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.05555555555555555, 0.005847953216374269, 0.0045662100456621, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.005128205128205128, 0.0125, 0.1111111111111111, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.01818181818181818, 0.005813953488372093, 0.004424778761061947, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.004098360655737705, 0.007518796992481203, 0.09090909090909091, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.007352941176470588, 0.003952569169960474, 0.003952569169960474, 0.003952569169960474, 0.0047169811320754715, 0.007407407407407408, 0.007575757575757576, 0.0625, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
[[0.2958373547424454, 0.7017788649452896, 0.062273689823917136, 0.5972807574704273, 0.9954328393432262, 0.06846660132933566, 0.951370577952487, 0.13857649386421894, 0.9155594510837761, 0.25332617322833023], [0.9435904403108412, 0.8308103147735971, 0.5642138942684856, 0.569439896252594, 0.008026089778913156, 0.14708273242122766, 0.025129344862662672, 0.10605633957699719, 0.017149039312628126, 0.9685317671516491, 0.9604996293414068, 0.13009421347484584, 0.011168020783984038, 0.9660212281911376, 0.009785512162735856, 0.774118311034036], [0.39828514946703536, 0.9719812893589492, 0.29744104658316833, 0.2407147714636478, 0.17834097142046978, 0.30339140369565476, 0.23414714143729679, 0.010316054064037888, 0.9665891555382209, 0.2709023215219119, 0.18678849879884543, 0.6853950395070337, 0.49793613573495626, 0.7369771877120291, 0.5822901326349779, 0.35804830353223377]]

def rnd():
    return float(decimal.Decimal(random.randrange(-200, 200))/100)

def neuronPositivityTotal(aIndex, imageData, weightsBalances):
    """ Calculates the total positivity of all nodes.."""

    # Step 1: Level 1 Neurons
    for z in range(len(aIndex[2])):
        value = 0.0
        # weightsBalances[2] is the level 1 values
        balance = weightsBalances[2][z][1]
        weights = weightsBalances[2][z][0]

        for y in range(len(imageData)):
            for x in range(len(imageData[y])):
                value += imageData[x][y]*weights[x][y]
        value += balance
        aIndex[2][z] = value

activationIndex = [[0.0 for x in range(10)], [0.0 for x in range(16)], [0.0 for x in range(16)]]

answerWB = {}
n1WB = {}
n2WB = {}
for x in range(10):
    answerWB[x] = [[rnd() for y in range(16)], rnd()]
for x in range(16):
    n2WB[x] = [[rnd() for y in range(16)], rnd()]
    n1WB[x] = [[[rnd() for y in range(28)] for x in range(28)], rnd()]

weightsBalances = [answerWB, n2WB, n1WB]

print(activationIndex)
neuronPositivityTotal(activationIndex, data, weightsBalances)
print(activationIndex)

输出:

[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.7268240240758053, 2.1047003177465773, -0.14702590533853588, 1.6819534808112164, 2.1421710797784743, -2.0840036534506106, -0.6067792109947316, 0.40979446487814936, 1.9165752482288656, 0.5045996467720566, -0.08187273316902777, -0.9549459648875294, -0.19719768439019592, -3.4785770307824064, -4.423173617897677, -1.8256438294590946]] 

即使程序中没有全局标签? 这是我正在进行的一个更大的深度学习项目的一部分,我无法弄清楚。 activationIndex 的值只设置一次,并且这些值与 aIndex 变量一起使用。 (使用 python 3.8)

将参数传递给 function 会在 function 的本地参数名称中生成指向同一 object 的别名。 当你这样做时:

aIndex[2][z] = value

这相当于做:

activationIndex[2][z] = value

因为aIndex是绑定到activationIndex同一个list的别名; 复制了指向该list的“指针”(所以说aIndex = 'foo'不会更改activationIndex ,因为它只是分配了一个新的“指针”),但是通过两个别名可以看到指向 object 的突变,直到一个其中反弹到新的 object。

如果您想切断与调用者的别名链接,请在收到它时对aIndex进行深度复制,并添加:

from copy import deepcopy  # At top of file

并将neuronPositivityTotal的开头更改为:

def neuronPositivityTotal(aIndex, imageData, weightsBalances):
    """ Calculates the total positivity of all nodes.."""
    aIndex = deepcopy(aIndex)
    # ... rest of function ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM