Skip to main content
 首页 » 编程设计

python之了解每一层的Keras权重矩阵

2024年10月01日9小虾米

我正在尝试使用 Keras 中的全连接层架构进行二进制分类,这在 Keras 中称为密集类。

这是我创建的神经网络架构的设计:

 from keras.models import Sequential 
        from keras.layers import Dense, Dropout, Activation 
        from keras.optimizers import SGD 
 
        self.model = Sequential() 
        # Dense(64) is a fully-connected layer with 64 hidden units. 
        # in the first layer, you must specify the expected input data shape: 
        # here, 20-dimensional vectors. 
        self.model.add(Dense(32, activation='relu', input_dim=self.x_train_std.shape[1])) 
        #self.model.add(Dropout(0.5)) 
        #self.model.add(Dense(64, activation='relu')) 
        #self.model.add(Dropout(0.5)) 
        self.model.add(Dense(1, activation='sigmoid')) 

所以我有一个 17000,2000 的输入矩阵,其中 17K 个样本具有 2k 个特征。

我只保留了一个隐藏层,里面有 32 个单元或神经元。

我的输出层是一个具有 sigmoid 激活函数的单神经元。

现在,当我尝试检查第一个隐藏层的权重时,我希望它的大小为 (2000,32),其中每一行对应于每个输入,每一列对应于该层中的每个单元。

这是 Keras 架构设置的配置:

dl_1.model.get_config() 
Out[70]: 
[{'class_name': 'Dense', 
  'config': {'activation': 'relu', 
   'activity_regularizer': None, 
   'batch_input_shape': (None, 2000), 
   'bias_constraint': None, 
   'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 
   'bias_regularizer': None, 
   'dtype': 'float32', 
   'kernel_constraint': None, 
   'kernel_initializer': {'class_name': 'VarianceScaling', 
    'config': {'distribution': 'uniform', 
     'mode': 'fan_avg', 
     'scale': 1.0, 
     'seed': None}}, 
   'kernel_regularizer': None, 
   'name': 'dense_1', 
   'trainable': True, 
   'units': 32, 
   'use_bias': True}}, 
 {'class_name': 'Dense', 
  'config': {'activation': 'sigmoid', 
   'activity_regularizer': None, 
   'bias_constraint': None, 
   'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 
   'bias_regularizer': None, 
   'kernel_constraint': None, 
   'kernel_initializer': {'class_name': 'VarianceScaling', 
    'config': {'distribution': 'uniform', 
     'mode': 'fan_avg', 
     'scale': 1.0, 
     'seed': None}}, 
   'kernel_regularizer': None, 
   'name': 'dense_2', 
   'trainable': True, 
   'units': 1, 
   'use_bias': True}}] 

查看第一个隐藏层的维度:

dl_1.model.get_layer(name="dense_1").input_shape 
 
(None, 2000) 

输出大小:

    dl_1.model.get_layer(name="dense_1").output_shape 
Out[99]: 
(None, 32) 

所以它似乎确实给出了预期的 (2000,32)。

然而,当我尝试检查这一层的权重矩阵时

dl_1.model.get_layer(name="dense_1").get_weights()[0] 

它给了我一个 numpy 数组列表,列表长度为 2000,数组长度在 32 内,如下所示:

array([[ 0.0484077 , -0.02401097, -0.03099879, -0.02864455, -0.01511723, 
         0.01386002,  0.01127522,  0.00844895, -0.02420873,  0.04466306, 
         0.02965425,  0.0410631 ,  0.02397312,  0.0038885 ,  0.04846045, 
         0.00653989, -0.05288456, -0.00325713,  0.0445733 ,  0.04594839, 
         0.02839083,  0.0445912 , -0.0140048 , -0.01198476,  0.05259909, 
        -0.03752745, -0.01337494, -0.02162734, -0.01522341,  0.01208428, 
         0.01122886,  0.01496441], 
       [ 0.05225918,  0.04231448,  0.01388102, -0.03310467, -0.05293509, 
         0.01130457,  0.03127011, -0.04250741, -0.04212657, -0.01595866, 
        -0.002456  ,  0.01112743,  0.0150629 ,  0.03072598, -0.04061607, 
        -0.01131565, -0.02259113,  0.00907649, -0.04728404, -0.00909081, 
         0.03182121, -0.04608218, -0.04411709, -0.03561752,  0.04686243, 
        -0.04555761,  0.04087613,  0.04380137,  0.02079088, -0.02390963, 
        -0.0164928 , -0.01228274], 

我不确定我是否理解这一点。它应该是 32 X2000 而不是 2000 X 32。所以我期望因为我有 32 个单元,并且每个单元有 2000 个权重,所以列表的长度为 32 个元素,每个元素应该是 2000 维的 numpy 数组。但它是相反的。我不确定这是为什么?

权重与隐藏层相关联,而不是输入层,所以如果我认为他们为输入层显示它是没有意义的。

知道这是怎么回事吗?

请您参考如下方法:

您正在创建一个包含 32 个单元的 Dense() 层。 Dense 层是(如您在代码中的注释所示)“完全连接的层”,这意味着数据中的每个特征都连接到每个层。您的数据元素中还有 2000 个特征。

因此,您获得的数组有 2000 个元素,每个元素对应一个特征,每个元素有 32 个权重,每个隐藏层一个,因此您得到的形状。

来自keras docs我们可以看到例子:

model = Sequential() 
model.add(Dense(32, input_shape=(16,))) 
# now the model will take as input arrays of shape (*, 16) 
# and output arrays of shape (*, 32) 
 
# after the first layer, you don't need to specify 
# the size of the input anymore: 
model.add(Dense(32)) 

在你的情况下 * 是 2000 所以你的输出权重应该是 (2000,32) 你得到的形状。这似乎是 Keras 用于其输出的约定。无论哪种方式,您都可以转换数据以赋予它其他形状,因为 (N, M) 数组与 (M, N) 数组具有相同数量的元素。