python - 为什么我的元组值会改变 (Python)?

标签 python artificial-intelligence machine-learning python-3.x

我有一个元组。第一个元素是一个 float ,第二个是一个包含更多包含 float 的嵌套列表的列表(不要担心这些值的含义)。

(0.2742965753780876, [[[33.119], [-8.326]], [[-34.084, -4.385], [-3.047, 18.546], [-10.757, 0.573], [21.395, 23.937], [5.011, -5.234]], [[-23.434, 9.989, 9.113, -23.253, 11.86], [-56.818, 4.771, -3.383, -27.143, 4.81], [-6.564, -40.132, -2.223, -2.663, -10.231], [-2.05, -15.989, 4.369, -20.051, 4.657]], [[-10.868, -39.934, 0.465, 38.103]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[21.633], [-8.972]], [[-35.754, -13.243], [-0.718, 17.724], [-16.452, 6.619], [24.151, 25.037], [1.76, -7.891]], [[-26.011, 9.072, 14.685, -20.044, 10.612], [-55.53, -0.131, 0.15, -27.031, 8.03], [-3.225, -36.499, -2.558, 0.253, -8.292], [-1.274, -22.561, 0.431, -23.405, 6.808]], [[-13.668, -47.758, -6.489, 43.27]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[22.435], [-6.71]], [[-47.591, -8.998], [-1.134, 16.529], [-16.399, 4.369], [23.344, 24.72], [2.175, -14.129]], [[-26.603, 11.472, 9.433, -21.13, 9.759], [-50.109, 1.084, 1.256, -18.826, 9.588], [-6.935, -27.957, 9.045, 1.291, 2.27], [-1.336, -29.908, -0.3, -27.242, 4.555]], [[-12.933, -42.377, 4.077, 38.864]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[26.688], [-4.315]], [[-49.478, -4.214], [0.116, 20.39], [-14.691, 3.496], [15.367, 23.116], [18.075, -2.748]], [[-25.588, 6.249, 4.364, -20.727, 19.639], [-55.524, -2.901, 4.639, -11.759, 11.794], [-8.633, -25.316, 11.841, 1.492, 1.36], [-0.797, -26.306, 1.379, -16.266, -0.291]], [[-24.726, -46.726, 12.765, 38.977]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[21.776], [-8.466]], [[-47.66, -5.868], [1.855, 23.062], [-19.521, 18.331], [29.251, 25.491], [21.32, -5.379]], [[-36.199, 7.786, -1.48, -27.042, 14.769], [-61.468, -12.218, -10.307, -6.156, 8.287], [-17.785, -33.124, 16.564, 2.249, -0.675], [-4.391, -18.11, 7.349, -9.234, -2.31]], [[-23.139, -55.043, 9.106, 35.827]], [-0.889, 1.129, 0.743]])

这些值是通过遗传算法训练人工神经网络获得的。如您所见,我贴出了顶层元组的五代。如果您熟悉 ML,您会注意到第一个元素是 ANN 在对训练数据进行分类时产生的均方误差,第二个是 ANN 中与该误差相关的权重列表。

每次我创建一个新的生成时,我都会把最上面的元组“放回”到一个新的候选列表中,这就是为什么多次出现上述错误的原因。它是每一代中的头号候选人。

但是,您不需要知道这些就可以理解我的问题。

重要的是,每次我用我的遗传算法函数运行新一代时,顶部错误(第一个元素)可能不会改变。这是正常的,因为这意味着算法还没有找到更好的解决方案。当然,权重(第二个元素)也不应该改变,因为如果改变了,它们会给出完全不同的错误。

不幸的是,事实并非如此。我的元组中的数据以某种方式被更改。正如您在上面的打印输出中看到的那样,每次我运行新一代时,错误保持不变但权重发生变化。

为什么会这样?为什么元组中的值会发生变化?

下面是我的一些代码。这是生成突变的代码。 mutate() 方法遍历权重列表( float )并对这些权重进行小的随机增量。这就是它对 0.2742965753780876 所做的,尽管它不应该这样做,因为更改权重而不更新错误是没有意义的。而且它甚至不应该是可能的,因为元组是不可变的。

for i in range(10):
    mList = weightList[random.randint(0,4)][1] # pick a set of weights from the top 5
    newList = mutate(mList, weightList[0][0]) # create a new list by mutating certain values of the unmutated list
    n.setWeights(listToGenome(newList,n)) # give the ANN the new weights
    error = trainSine(n) # train the network on the training set and return the mean-squared error
weightList.append((error, child))

您可能还想知道,每次我创建完新一代后,我都会根据错误对列表进行排序,然后选出前 10 名候选人继续进入下一代。

errorList.sort()
for i in range(10):
    survivorList.append(errorList[i])

就是这样。当然还有很多代码,但如果有人需要,我会分享。有什么想法吗?

最佳答案

元组不可变,但其中的列表仍然可变。您需要对列表进行深层复制,它会在复制时复制值,而无需所有变量引用等。您可以使用 copy 模块执行此操作。语法如下:

import copy
myList = [1, 5, 3, 9, 4]
myOtherList = copy.deepcopy(myList)

使用上面的代码,如果您更改 myList,或者如果任何对 myList 有贡献的变量发生变化,myOtherList 将保持不变, 独立于其他。

关于python - 为什么我的元组值会改变 (Python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259956/

相关文章:

python - 有没有办法将调试信息打印到日志中?

machine-learning - Tesauro 的 TD-Gammon 中的板编码

python - 如何将自己的数据集提供给keras image_ocr

python - 如何使用 Scikit Learn 调整随机森林中的参数?

python - as_list() 未在未知 TensorShape 上定义

Python 将 lambda 函数应用于 csv 文件(大文件)

python - Spark中的MultiLabelBinarizer?

python - 如何更改plot.ly 中旭日图的颜色?

f# - 纸牌游戏的遗传算法(Dominion)

python - 类型错误 : object of type 'Tensor' has no len() when using a custom metric in Tensorflow