TensorFlow + Kerasを使ってみた6(層構成の変更、畳み込み層の統計情報)”の続き。

前回は、層の構成を変更して、学習を行った。今回は、畳み込み層の統計情報を取得してみよう。
なお、使用するのは MNIST の手書き数字を認識するCNN で畳み込み層の特徴マップの数は 10 個となっている。

前回、model.summary() で取得した各層の情報を元に畳み込み層の中間出力を取り出そう。

# Convolution layerの中間出力を取り出す 
from keras.models import Model

conv_layer_name = 'conv2d_4'

conv_layer = model.get_layer(conv_layer_name)
conv_layer_wb = conv_layer.get_weights()

conv_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(conv_layer_name).output)
conv_output = conv_layer_model.predict(x_test, verbose=1)


表示を示す。

10000/10000 [==============================] - 1s 150us/step


最初に重みやバイアスの配列の構成やその値を見てみよう。

conv_layer_weight = conv_layer_wb[0]
conv_layer_bias = conv_layer_wb[1]

print(conv_layer_weight.shape)
print(conv_layer_weight.T.shape)
print(conv_layer_bias.shape)


結果を示す。

(5, 5, 1, 10)
(10, 1, 5, 5)
(10,)


conv_layer_weight は (5, 5, 1, 10) なので、いつもあつかている配列の構成ではない。そして、conv_layer_weight.T で転置してみたところ、いつも使っている構成の (10, 1, 5, 5) つまり、カーネル数、チャネル数、縦の幅、横の幅の配列になった。

print("conv_layer_weight.T = {0}".format(conv_layer_weight.T))

で転置した重みの配列を示す。

conv_layer_weight.T = [[[[ 0.20261094 -0.3398506  -0.5767307   0.11835691  0.13021287]
   [-0.07934965 -0.33518496 -0.4275438   0.25123549  0.38388866]
   [-0.22467291 -0.39792794 -0.07211141  0.38731813  0.24981308]
   [-0.43532223 -0.08618319  0.3187846   0.27912328  0.02272184]
   [-0.23157348  0.16632372  0.2654636   0.15583257 -0.04710154]]]


 [[[-0.48611653  0.15439186  0.4068115   0.3514016   0.16548221]
   [-0.6372757  -0.34480083  0.3836496   0.3169199   0.2640638 ]
   [-0.40548003 -0.56099683  0.10779987  0.32510042  0.3619229 ]
   [-0.33244497 -0.3915109  -0.1230321   0.2981098   0.35238296]
   [-0.13435255 -0.41839477 -0.4722871  -0.10132303  0.11304493]]]


 [[[-0.13130069  0.06223634  0.10510171  0.02183475 -0.16628554]
   [ 0.18190795  0.35684425  0.25642243  0.00863578  0.12985978]
   [ 0.15537558 -0.11242905 -0.2288756   0.04026176  0.08550146]
   [-0.1676253  -0.44136783 -0.29937005 -0.0171281   0.2620432 ]
   [-0.14785497 -0.10125857  0.12721944  0.05586093  0.10579447]]]


 [[[ 0.15156339 -0.20048767 -0.5791418  -0.65549827 -0.25779864]
   [ 0.4940948   0.3314954  -0.1274401  -0.3982863  -0.3313806 ]
   [ 0.22510909  0.45060343  0.15244117 -0.23712645 -0.02554286]
   [ 0.19534814  0.11640821  0.2987521  -0.04862794  0.04132852]
   [ 0.24242142  0.0540004  -0.00865097 -0.0300091   0.12885101]]]


 [[[ 0.36471996  0.35694337  0.27650365  0.35590482  0.13169082]
   [ 0.11910628  0.07778469  0.19447733  0.06036808 -0.12147922]
   [ 0.0868587   0.1454417   0.02258768 -0.2499182  -0.19614659]
   [-0.26476386 -0.27914035 -0.4387378  -0.33735904 -0.03323634]
   [-0.40310845 -0.43084973 -0.27778476 -0.2462857   0.04993651]]]


 [[[-0.08484216  0.19511358  0.58113253 -0.12703945 -0.516542  ]
   [ 0.17010233  0.2240115   0.23622094 -0.31102535 -0.59745365]
   [ 0.397805    0.23805015 -0.1035163  -0.45656392 -0.34286296]
   [ 0.18052064 -0.28208354 -0.29351595 -0.36484626  0.06465741]
   [-0.20084426 -0.30468363 -0.2777929   0.08292956 -0.01636941]]]


 [[[-0.05747546  0.10129268  0.0927546   0.01556351 -0.16821466]
   [-0.0250085   0.140934    0.12933072  0.19052765  0.20077062]
   [ 0.2489682   0.18465307  0.23520534  0.26735055  0.24849436]
   [ 0.00098434 -0.29655868 -0.13283624 -0.11904856 -0.02703394]
   [-0.31173185 -0.3589846  -0.2216169   0.05286852 -0.00669706]]]


 [[[-0.46006405 -0.41662437 -0.26404095 -0.27005908  0.00341533]
   [-0.07625411 -0.01859824 -0.0235228   0.0303653   0.10755768]
   [ 0.07276727  0.20107509  0.15815544  0.3283318   0.23039222]
   [ 0.21414295  0.14830865  0.24796312  0.01516124 -0.05039264]
   [ 0.01465091  0.08253051 -0.08803863  0.01456806 -0.17668988]]]


 [[[ 0.09082198  0.38919494  0.33294797 -0.5168951  -0.62100536]
   [-0.03360464  0.21474971  0.37199846 -0.29824486 -0.6191712 ]
   [ 0.11340497  0.20264329  0.37084493 -0.32331055 -0.5669018 ]
   [ 0.20493641  0.2751836   0.10829608 -0.20219678 -0.39315876]
   [ 0.14139216  0.20002662  0.17661056 -0.22110288 -0.28045934]]]


 [[[ 0.12587526 -0.01364575 -0.2322505  -0.14462651 -0.03129309]
   [ 0.171594    0.22744659 -0.05975187 -0.18951881 -0.2751198 ]
   [ 0.19060391  0.12572204  0.3344037   0.26089588 -0.12050828]
   [-0.17589498 -0.02884873  0.20712087  0.19588387  0.0149854 ]
   [-0.2094244  -0.37416157 -0.08472645  0.12522626  0.06411268]]]]


print("conv_layer_bias = {0}".format(conv_layer_bias))

で表示したバイアス値を示す。

conv_layer_bias = [-0.00722218 0.00386539 -0.10034832 -0.10226133 -0.00783706 -0.00266487
-0.15441592 -0.17244887 0.00067333 -0.17412803]


次に、取得した畳み込み層の中間出力を解析して重みやバイアスの最大値、最小値、絶対値の最大値、最小値、出力の標準偏差、最大値、最小値、絶対値の最大値、最小値を見てみよう。
Python コードを示す。

print("np.max(conv_layer_weight) = {0}".format(np.max(conv_layer_weight)))
print("np.min(conv_layer_weight) = {0}".format(np.min(conv_layer_weight)))
abs_conv_layer_weight = np.absolute(conv_layer_weight)
print("np.max(abs_conv_layer_weight) = {0}".format(np.max(abs_conv_layer_weight)))
print("np.min(abs_conv_layer_weight) = {0}".format(np.min(abs_conv_layer_weight)))

print("np.max(conv_layer_bias) = {0}".format(np.max(conv_layer_bias)))
print("np.min(conv_layer_bias) = {0}".format(np.min(conv_layer_bias)))
abs_conv_layer_bias = np.absolute(conv_layer_bias)
print("np.max(abs_conv_layer_bias) = {0}".format(np.max(abs_conv_layer_bias)))
print("np.min(abs_conv_layer_bias) = {0}".format(np.min(abs_conv_layer_bias)))

print("conv_output = {0}".format(conv_output.shape))
print("np.std(conv_output) = {0}".format(np.std(conv_output)))
print("np.max(conv_output) = {0}".format(np.max(conv_output)))
print("np.min(conv_output) = {0}".format(np.min(conv_output)))

abs_conv_output = np.absolute(conv_output)
print("np.max(abs_conv) = {0}".format(np.max(abs_conv_output)))
print("np.min(abs_conv) = {0}".format(np.min(abs_conv_output)))


出力を示す。

np.max(conv_layer_weight) = 0.581132531166
np.min(conv_layer_weight) = -0.65549826622
np.max(abs_conv_layer_weight) = 0.65549826622
np.min(abs_conv_layer_weight) = 0.000984335667454
np.max(conv_layer_bias) = 0.0038653917145
np.min(conv_layer_bias) = -0.17412802577
np.max(abs_conv_layer_bias) = 0.17412802577
np.min(abs_conv_layer_bias) = 0.000673334288877
conv_output = (10000, 24, 24, 10)
np.std(conv_output) = 0.691880404949
np.max(conv_output) = 3.46592283249
np.min(conv_output) = -4.23804473877
np.max(abs_conv) = 4.23804473877
np.min(abs_conv) = 7.68341124058e-09


畳み込み層の重みのグラフを書いてみた。

# Convolution layerのweightのグラフ
conv_layer_weight_f = conv_layer_weight.flatten()
plt.plot(conv_layer_weight_f)
plt.title('conv_layer_weight')
plt.show()


tensorflow_keras_45_180529.png

畳み込み層のバイアスのグラフを書いてみた。

# Convolution layerのbiasのグラフ
conv_layer_bias_f = conv_layer_bias.flatten()
plt.plot(conv_layer_bias_f)
plt.title('conv_layer_bias')
plt.show()


tensorflow_keras_46_180529.png