【问题标题】:Confusion with weights dumping from neural net in keras与keras中神经网络的权重倾倒混淆
【发布时间】:2016-09-11 04:52:03
【问题描述】:

我创建了一个简单的 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)

谁能解释一下原因?

【问题讨论】:

  • 我知道了,为了验证请告诉我len(weights[0][0])返回什么?

标签: python arrays list numpy keras


【解决方案1】:

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

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

这里,列表中的第一个元素包含n个长度的元素,第二个列表也可以是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])。所以当我要求你回来时

len(weights[0][0]) 

它返回:

100

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

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

【讨论】:

  • 谢谢。我的列表来自使用 keras list = model.layers[0].get_weights() 转储神经网络的权重。
  • 那么你是从你的输入层转储权重吗?是卷积神经网络还是简单的反向传播?什么是神经网络架构?另外,请编辑问题,因为这是完全不同的问题。
  • 我认为它与 keras 无关,但也许是。我编辑了我的问题。
  • 所以 weights[0] 是我正在寻找的矩阵,两层之间的 500×300 权重矩阵,而 weights[0][i] 是第 i 个神经元的权重的 500 向量第二层。对吗?
猜你喜欢
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多