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, 224, 3))
image = tf.image.resize(data.chelsea(), (224, 224))/255
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, 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)>
推荐阅读
- python - 如何在 PyQt 中添加到标题栏右键菜单
- mongodb - 如何调用子数组中另一个集合中的属性?
- mdx - 如何按同一维度上的两个条件进行排序
- html - 每当浏览器窗口调整大小时,边距会导致错位
- flutter - 我想将数据传递到购物篮页面,但屏幕是空白的,它什么也不显示
- oracle - 如何编写连接两个表的案例函数
- reactjs - 如何从自定义(非crud,自定义数据)json显示自定义页面
- python - 为 QFrame 中的小部件设置相同的边距
- python - 如何在 PyQt5 QOpenGLWidget 中对球体进行纹理处理
- cpu-architecture - 教科书上一个多发CPU的例子,为什么分支指令之后的指令要等一个周期再发?