如何在keras模型的所有层中可视化学习的训练权重?

时间:2020-07-09 07:55:52

标签: python tensorflow keras

我正在使用它:

Python version: 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]
TensorFlow version: 2.1.0
Eager execution: True

具有以下摘要的U-Net模型:

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 200, 200, 1) 0
__________________________________________________________________________________________________
conv1_1 (Conv2D)                (None, 200, 200, 64) 1664        input_1[0][0]
__________________________________________________________________________________________________
conv1_2 (Conv2D)                (None, 200, 200, 64) 102464      conv1_1[0][0]
__________________________________________________________________________________________________
pool1 (MaxPooling2D)            (None, 100, 100, 64) 0           conv1_2[0][0]
__________________________________________________________________________________________________
conv2_1 (Conv2D)                (None, 100, 100, 96) 55392       pool1[0][0]
__________________________________________________________________________________________________
conv2_2 (Conv2D)                (None, 100, 100, 96) 83040       conv2_1[0][0]
__________________________________________________________________________________________________
pool2 (MaxPooling2D)            (None, 50, 50, 96)   0           conv2_2[0][0]
__________________________________________________________________________________________________
conv3_1 (Conv2D)                (None, 50, 50, 128)  110720      pool2[0][0]
__________________________________________________________________________________________________
conv3_2 (Conv2D)                (None, 50, 50, 128)  147584      conv3_1[0][0]
__________________________________________________________________________________________________
pool3 (MaxPooling2D)            (None, 25, 25, 128)  0           conv3_2[0][0]
__________________________________________________________________________________________________
conv4_1 (Conv2D)                (None, 25, 25, 256)  295168      pool3[0][0]
__________________________________________________________________________________________________
conv4_2 (Conv2D)                (None, 25, 25, 256)  1048832     conv4_1[0][0]
__________________________________________________________________________________________________
pool4 (MaxPooling2D)            (None, 12, 12, 256)  0           conv4_2[0][0]
__________________________________________________________________________________________________
conv5_1 (Conv2D)                (None, 12, 12, 512)  1180160     pool4[0][0]
__________________________________________________________________________________________________
conv5_2 (Conv2D)                (None, 12, 12, 512)  2359808     conv5_1[0][0]
__________________________________________________________________________________________________
up_conv5 (UpSampling2D)         (None, 24, 24, 512)  0           conv5_2[0][0]
__________________________________________________________________________________________________
crop_conv4 (Cropping2D)         (None, 24, 24, 256)  0           conv4_2[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 24, 24, 768)  0           up_conv5[0][0]
                                                                 crop_conv4[0][0]
__________________________________________________________________________________________________
conv6_1 (Conv2D)                (None, 24, 24, 256)  1769728     concatenate[0][0]
__________________________________________________________________________________________________
conv6_2 (Conv2D)                (None, 24, 24, 256)  590080      conv6_1[0][0]
__________________________________________________________________________________________________
up_conv6 (UpSampling2D)         (None, 48, 48, 256)  0           conv6_2[0][0]
__________________________________________________________________________________________________
crop_conv3 (Cropping2D)         (None, 48, 48, 128)  0           conv3_2[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 48, 48, 384)  0           up_conv6[0][0]
                                                                 crop_conv3[0][0]
__________________________________________________________________________________________________
conv7_1 (Conv2D)                (None, 48, 48, 128)  442496      concatenate_1[0][0]
__________________________________________________________________________________________________
conv7_2 (Conv2D)                (None, 48, 48, 128)  147584      conv7_1[0][0]
__________________________________________________________________________________________________
up_conv7 (UpSampling2D)         (None, 96, 96, 128)  0           conv7_2[0][0]
__________________________________________________________________________________________________
crop_conv2 (Cropping2D)         (None, 96, 96, 96)   0           conv2_2[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 96, 96, 224)  0           up_conv7[0][0]
                                                                 crop_conv2[0][0]
__________________________________________________________________________________________________
conv8_1 (Conv2D)                (None, 96, 96, 96)   193632      concatenate_2[0][0]
__________________________________________________________________________________________________
conv8_2 (Conv2D)                (None, 96, 96, 96)   83040       conv8_1[0][0]
__________________________________________________________________________________________________
up_conv8 (UpSampling2D)         (None, 192, 192, 96) 0           conv8_2[0][0]
__________________________________________________________________________________________________
crop_conv1 (Cropping2D)         (None, 192, 192, 64) 0           conv1_2[0][0]
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 192, 192, 160 0           up_conv8[0][0]
                                                                 crop_conv1[0][0]
__________________________________________________________________________________________________
conv9_1 (Conv2D)                (None, 192, 192, 64) 92224       concatenate_3[0][0]
__________________________________________________________________________________________________
conv9_2 (Conv2D)                (None, 192, 192, 64) 36928       conv9_1[0][0]
__________________________________________________________________________________________________
conv9_3 (ZeroPadding2D)         (None, 200, 200, 64) 0           conv9_2[0][0]
__________________________________________________________________________________________________
conv10_1 (Conv2D)               (None, 200, 200, 1)  65          conv9_3[0][0]
==================================================================================================
Total params: 8,740,609
Trainable params: 8,740,609
Non-trainable params: 0

我想分别显示每层权重的所有值。我已经尝试过了,但是我认为它可以一次显示所有图层:

from keras import backend as K

for w in model.trainable_weights:
    print(K.eval(w))

如何分别显示每一层的所有权重?

1 个答案:

答案 0 :(得分:0)

您可以使用get_weights()保存其他变量或进行打印。

for i in range(len(model.layers)):
    print(model.layers[0].get_weights())