在keras中,给定隐藏层的输入,权重和偏差,如何获得隐藏层的输出? [英] How to get output of hidden layer given an input, weights and biases of the hidden layer in keras?

查看:286
本文介绍了在keras中,给定隐藏层的输入,权重和偏差,如何获得隐藏层的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经为以下某个时期训练了模型:

Suppose I have trained the model below for an epoch:

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_w ,对第一个隐藏层(命名为 dense1 )的 dense1_b 和单个数据样本样本

And I got the weights dense1_w, biases dense1_b of first hidden layer (named it dense1) and a single data sample sample.

如何使用这些数据在样本上获取 dense1 的输出 keras 中的code>?

How do I use these to get the output of dense1 on the sample in keras?

谢谢!

推荐答案

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

The easiest way is to use the keras backend. With the keras backend you can define a function that gives you the intermediate output of a keras model as defined here (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])

这篇关于在keras中,给定隐藏层的输入,权重和偏差,如何获得隐藏层的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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