python - 给定keras中隐藏层的输入,权重和偏差,如何获得隐藏层的输出?

标签 python tensorflow keras neural-network deep-learning

假设我已经对下面的模型进行了一个时期的训练:

model = Sequential([
    Dense(32, input_dim=784), # first number is output_dim
    Activation('relu'),
    Dense(10), # output_dim, input_dim is taken for granted from above
    Activation('softmax'),
])

我得到了第一个隐藏层(命名为 dense1)的权重 dense1_w、偏差 dense1_b 和单个数据样本 sample

如何使用这些在 kerassample 上获取 dense1 的输出?

谢谢!

最佳答案

最简单的方法是使用 keras 后端。使用 keras 后端,您可以定义一个函数,为您提供此处定义的 keras 模型的中间输出 ( https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer )。

本质上:

get_1st_layer_output = K.function([model.layers[0].input],
                                  [model.layers[1].output])
layer_output = get_1st_layer_output([X])

关于python - 给定keras中隐藏层的输入,权重和偏差,如何获得隐藏层的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43871162/

相关文章:

tensorflow - 如何在tensorflow中将输入更改为OP

python - keras 模型中损失的 nan 值

python - 如何在 MatPlotLib 中注释/突出显示 3d 图

python - 尝试理解 keras SimpleRNN

python - 有没有类似 HTTP::Recorder for Python 的东西?

python - 带有 bool 张量的 tensorflow 索引

python - 如何使用keras的fit生成器的多处理模式解决多个进程写入同一个文件?

python - 获取 ImportError : libcublas. so.9.0:无法打开共享对象文件:没有这样的文件或目录 - 降级 TF 版本不起作用

python - Tensorflow:提取所有其他元素

python - 如何通过 Python socket.send() 发送字符串以外的任何内容