如何获得神经元的激活

如何获得神经元的激活

本文介绍了在 tensorflow 的推理过程中,如何获得神经元的激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to specifically know how to get the neurons in a neural network are getting activated (the outputs of each neuron after the activation function)

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

How can I get the activations of all the neurons of a sequential model when I give input during the models' inference in Tensorflow 2?

推荐答案

试试这个:

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

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

You can get a list of the layers with model.layers and pick the one you want.

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)>

这篇关于在 tensorflow 的推理过程中,如何获得神经元的激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:56