为什么我无法获得经过训练的模型的内部输出? [英] Why I can't get the internal output of a trained model?

查看:34
本文介绍了为什么我无法获得经过训练的模型的内部输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    model = keras.models.load_model('model/model_test_0.99408.h5', custom_objects={'leaky_relu': tf.nn.leaky_relu})
    model.summary()
    inputs = keras.layers.Input(shape=(28, 28, 1))
    y = model(inputs)
    feature = model.get_layer('conv2d_4').output
    model = keras.Model(inputs=inputs, outputs=[y, feature])
    model.summary()

为什么我无法获得作为模型内部层的conv2d_4"的输出?我收到以下错误.

why i can't get the output of 'conv2d_4' that is the internal layer of the model? And i get the following error.

Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 28, 28, 1), dtype=float32) at layer "conv2d". The following previous layers were accessed without issue: []

推荐答案

我们可以尝试重新堆叠模型,将feature分配给需要的层,

We can try restacking the model, assigning feature to the required layer,

import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    model = keras.models.load_model('model/model_test_0.99408.h5', custom_objects={'leaky_relu': tf.nn.leaky_relu})
    model.summary()
    inputs = keras.layers.Input(shape=(28, 28, 1))
    y = inputs
    for layer in vgg.layers:
        if layer.name == 'conv2d_4':
            feature = y
        y = layer( y )
    model = keras.Model(inputs=inputs, outputs=[y, feature])
    model.summary()

这篇关于为什么我无法获得经过训练的模型的内部输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆