代码之家  ›  专栏  ›  技术社区  ›  grabbag

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

  •  0
  • grabbag  · 技术社区  · 6 年前

    我的keras正在使用tensorflow后端。这是在docker图像中运行的,是从jupyter笔记本上运行的。

    print(model.summary())将生成所有可训练参数的列表。

    _____________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_1 (InputLayer)         (None, 512, 512, 3)       0         
    _________________________________________________________________
    conv2d_1 (Conv2D)            (None, 512, 512, 16)      448       
    _________________________________________________________________
    activation_1 (Activation)    (None, 512, 512, 16)      0         
    _________________________________________________________________
    batch_normalization_1 (Batch (None, 512, 512, 16)      64        
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 256, 256, 16)      0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 256, 256, 32)      4640  
    

    [<tf.Variable 'conv2d_1/kernel:0' shape=(3, 3, 3, 16) dtype=float32_ref>,
     <tf.Variable 'conv2d_1/bias:0' shape=(16,) dtype=float32_ref>,
     <tf.Variable 'batch_normalization_1/gamma:0' shape=(16,) dtype=float32_ref>,
     <tf.Variable 'batch_normalization_1/beta:0' shape=(16,) dtype=float32_ref>,
     <tf.Variable 'conv2d_2/kernel:0' shape=(3, 3, 16, 32) dtype=float32_ref>,
     <tf.Variable 'conv2d_2/bias:0' shape=(32,) dtype=float32_ref>,
    

    我如何打印这些变量的值,以查看有多少得到疯狂的值,如0、1或无穷大?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Daniele Grattarola    6 年前

    最简单的方法是计算重量张量:

    from keras import backend as K
    
    for w in model.trainable_weights:
        print(K.eval(w))
    

    K.eval(w)

    np.isnan(w)
    np.isinf(w)
    w == 0
    w == 1
    

    你可以使用 np.any np.argwhere

    干杯

    推荐文章