keras tensorflow 獲取某層輸出和層內參數的值


1.查看模型的輸出和形狀

# 構建模型
Transformer_encoder = build_transformer_model(config_path, checkpoint_path)
gp_layer = GlobalPointer(len(categories), 64)
final_output = gp_layer(Transformer_encoder.output) # 將bert的編碼送入后面接的分類層
model = Model(Transformer_encoder.input, final_output) # 由輸入和輸出定義

# 准備數據,可以自己寫,不過這里復用了訓練用的數據生成器
train_generator = data_generator(train_data, batch_size)
data_ge = train_generator.__iter__()
embeding_and_segments, labels = next(data_ge)

# K為keras的后端,也就是tensorflow
# K.function和上面那個Model很像,也由輸入和輸出定義,通過該函數就可以獲取模型內部的輸出
f = K.function(model.input, model.output)
out = f(embeding_and_segments) # 給模型輸入數據,out就是模型的輸出
print(out)
print(out.shape)

2.想要查看模型某一層的輸出

model = Model(Transformer_encoder.input, final_output) # 由輸入和輸出定義
print(model.layers) # 模型有很多層
# 如果想看最后一層的輸出,就直接這樣寫,其他層改一下索引即可
f = K.function(model.input, model.layers[-1].output)
out = f(embeding_and_segments)
print(out)
print(out.shape)

3.如果想看某一層里面的更細致的參數,比如最后一層GlobalPointer里面的某些數據處理

比如GlobalPointer的call前面幾行input的處理,就自己新加代碼獲取輸出

        inputs = self.dense(inputs)
        self.my = inputs # 新加代碼
        inputs = tf.split(inputs, self.heads, axis=-1)
        inputs = K.stack(inputs, axis=-2)
        qw, kw = inputs[..., :self.head_size], inputs[..., self.head_size:]

就可以通過最后一層model.layer[-1].my查看在dense處理后的input和其形狀,上面代碼的self不是model本身,是GlobalPointer,這是模型的最后一層,用model.my肯定報錯的

f = K.function(model.input, model.layer[-1].my)
out = f(embeding_and_segments)
print(out)
print(out.shape)

4.如果想看dense的參數,dense是在GlobalPointer的build函數里定義的

    def build(self, input_shape):
        super(GlobalPointer, self).build(input_shape)
        self.dense = Dense(
            units=self.head_size * self.heads * 2,
            use_bias=self.use_bias,
            kernel_initializer=self.kernel_initializer
        )

dense由Dense定義,則進入Dense源碼,截取部分如下,可以注意到有一個self.kernel

    def build(self, input_shape):
        assert len(input_shape) >= 2
        input_dim = input_shape[-1]

        self.kernel = self.add_weight(shape=(input_dim, self.units),
                                      initializer=self.kernel_initializer,
                                      name='kernel',
                                      regularizer=self.kernel_regularizer,
                                      constraint=self.kernel_constraint)

那么就可以這樣獲取

f = K.function(model.input, model.layers[-1].dense.kernel)
out = f(embeding_and_segments)
print(out)
print(out.shape)

最后贈送兩個函數,一個將模型summary輸出到文件,有時候控制台顯示不了那么寬的文字;另一個將模型打印成圖,看了一下,細節不夠,好像用處不大

def model_to_picture(model):
    # 畫一個模型結構圖
    from tensorflow.keras.utils import plot_model
    plot_model(model, to_file='model_structure.png', show_shapes=True)


def model_summary_tofile(model, file_name="model_summary.txt"):
    from contextlib import redirect_stdout
    with open(file_name, 'w') as f:
        with redirect_stdout(f):
            model.summary(line_length=250)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM