与keras中神经网络的权重倾销混淆

Confusion with weights dumping from neural net in keras

我创建了一个简单的 2 层网络,一个隐藏层。我正在倾倒中间层的权重,以可视化隐藏神经元正在学习的内容。 我正在使用

weights = model.layers[0].get_weights() 

当我查看权重结构时,我得到:

因此 len(weights) = 2len(weights[0]) = 500len(weights[1]) = 100

我想创建一个大小为 (500,100) 的数组 m,以便 m.shape = (500,100)。 我尝试了 numpy.reshape(weights, 500, 100)zip(weights[0], weights[1]),然后,偶然地,我写了 numpy.array(weights[0]),结果以 (500,100).

的形式返回

有人可以解释为什么吗?

Keras 张量的工作方式不同,它们是 n 维列表。为了说明这个概念,请考虑以下列表:

>>> list=[[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]],[1,2,3]]

这里,list中的第一个元素包含n个长度的元素,第二个list也可以是n个长度的元素。当你这样做时:

>>> len(list)

输出为:

2( which is 2 in your case)

此外,

>>> len(list[0])

5(which is 500 in your case)

>>> len(list[1])

3(which is 100 in your case)

但是当您尝试转换为数组时:

>>> np.array(list[0]).shape

答案是:

(5, 3) (which is 500,100 in your case)

这是因为您的 list[0] 中有一个长度为 n 的列表元素(在您的例子中是 weights[0])。所以当我让你 return

len(weights[0][0]) 

它 return编辑:

100

因为它包含该列表中的 100 个长度元素和其中的 500 个这样的元素。现在,如果您想知道每 100 个值是什么意思,那么它们就是连接的相应权重,即

weights[0][0] = weights between first input to all 100 hidden neurons