在 tensorflow 的推理过程中,如何获得神经元的激活?

问题描述

我想具体知道如何让神经网络中的神经元被激活(激活函数后每个神经元的输出

当我在 Tensorflow 2 中的模型推理期间提供输入时,如何获得序列模型的所有神经元的激活?

解决方法

尝试这样的事情:

intermediate_output = tf.keras.Model(model.input,model.get_layer('conv2_block1_3_conv').output)

您可以使用 model.layers 获取图层列表并选择您想要的图层。

list(map(lambda x: x.name,model.layers))
['input_2','conv1_pad','conv1_conv','conv1_bn','conv1_relu','pool1_pad','pool1_pool','conv2_block1_1_conv','conv2_block1_1_bn','conv2_block1_1_relu','conv2_block1_2_conv','conv2_block1_2_bn','conv2_block1_2_relu','conv2_block1_0_conv','conv2_block1_3_conv',...

完整示例:

import tensorflow as tf
from skimage import data
import matplotlib.pyplot as plt

model = tf.keras.applications.resnet.ResNet50(
    weights='imagenet',include_top=True)

model.build(input_shape=(None,224,3))

image = tf.image.resize(data.chelsea(),(224,224))/255

enter image description here

intermediate_output = tf.keras.Model(model.input,model.get_layer('conv2_block1_3_conv').output)

extracted = intermediate_output(image[None,...])
<tf.Tensor: shape=(1,56,256),dtype=float32,numpy=
array([[[[ 0.23296243,-0.19640587,-0.8846314,...,-0.3091477,-0.51000404,-0.00218517],[ 0.27896926,-0.22646117,-0.91138077,-0.4151363,-0.73324907,0.05706196],[ 0.27908558,-0.2267507,-0.9121696,-0.41521263,-0.73362166,0.05721636],[ 0.04438811,0.49058744,-1.5047315,-0.15204512,-1.2029954,-0.29269713],[ 0.04450325,0.4905177,-1.505692,-0.15128748,-1.2025517,-0.29254213],[ 0.05638191,0.22808033,-1.5240382,0.0052015,-0.8789809,-0.19639899]]]],dtype=float32)>