如何查看初始权重(即训练前)?

2024-05-28 12:48:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Keras生成一个简单的单层前馈网络。我想在通过kernel_initializer参数初始化权重值时,更好地处理这些值。

有没有一种方法可以让我在初始化后(即训练完成前)查看权重值。

也许我应该解释一下为什么我想看到初始化的权重。在Keras中,我对随机正交矩阵实际上是什么有点困惑。如果我可以打印这些值,它将帮助我更好地理解这个特性。


Tags: 方法网络参数矩阵特性kernelkeras权重
3条回答

您需要指定输入到第一层的维度,否则它将给您一个空列表。比较两个打印结果的唯一区别在于输入形状的初始化。

from keras import backend as K
import numpy as np 
from keras.models import Sequential
from keras.layers import Dense
# first model without input_dim prints an empty list   
model = Sequential()
model.add(Dense(5, weights=[np.ones((3,5)),np.zeros(5)], activation='relu'))
print(model.get_weights())


# second model with input_dim prints the assigned weights
model1 = Sequential()
model1.add(Dense(5,  weights=[np.ones((3,5)),np.zeros(5)],input_dim=3, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))

print(model1.get_weights())

@Chris_K给出的答案应该有效-model.get_weights()在调用fit之前打印正确的初始化权重。尝试运行此代码作为健全性检查-它应该打印两个非零的矩阵(对于两个层),然后打印两个零的矩阵:

from keras.models import Sequential
from keras.layers import Dense
import keras
import numpy as np

X = np.random.randn(10,3)
Y = np.random.randn(10,)

# create model
model1 = Sequential()
model1.add(Dense(12, input_dim=3, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))

print(model1.get_weights())

# create model
model2 = Sequential()
model2.add(Dense(12, input_dim=3, kernel_initializer='zero', activation='relu'))
model2.add(Dense(1, kernel_initializer='zero', activation='sigmoid'))

print(model2.get_weights())

下面是我看到的结果:

[
array([[-0.08758801, -0.20260376,  0.23681498, -0.59153044, -0.26144034,
         0.48446459, -0.02285194,  0.0874517 ,  0.0555284 , -0.14660612,
         0.05574059, -0.14752924],
       [ 0.20496374, -0.4272995 ,  0.07676286, -0.38965166,  0.47710329,
        -0.26640627, -0.33820981, -0.48640659,  0.11153179, -0.01180136,
        -0.52833426,  0.56279379],
       [-0.12849617,  0.2982074 ,  0.38974017, -0.58133346, -0.09883761,
         0.56037289,  0.57482034,  0.08853614,  0.14282584, -0.52498174,
        -0.35414279, -0.49750996]], dtype=float32), array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.], dtype=float32), array([[-0.65539688],
       [-0.58926439],
       [ 0.6232332 ],
       [-0.6493122 ],
       [ 0.57437611],
       [-0.42971158],
       [ 0.66621709],
       [-0.17393446],
       [ 0.57196724],
       [-0.01042461],
       [ 0.32426012],
       [-0.08326346]], dtype=float32), array([ 0.], dtype=float32)]
[array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]], dtype=float32), array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.], dtype=float32), array([[ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.]], dtype=float32), array([ 0.], dtype=float32)]

只需在模型上使用get_weights()。例如:

i = Input((2,))
x = Dense(5)(i)

model = Model(i, x)

print model.get_weights()

这将打印2x5权重矩阵和1x5偏差矩阵:

[array([[-0.46599612,  0.28759909,  0.48267472,  0.55951393,  0.3887372 ],
   [-0.56448901,  0.76363671,  0.88165808, -0.87762225, -0.2169953 ]], dtype=float32), 
 array([ 0.,  0.,  0.,  0.,  0.], dtype=float32)]

偏差为零,因为默认的偏差初始值设定项为零。

相关问题 更多 >

    热门问题