急切模式下访问Tensor-flow 2.0中的中间层的输出 [英] Access output of intermediate layers in Tensor-flow 2.0 in eager mode

查看:538
本文介绍了急切模式下访问Tensor-flow 2.0中的中间层的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有在Tensor-flow 2.0上构建的CNN。我需要访问中间层的输出。我正在研究其他类似的stackoverflow问题,但是所有问题都有涉及Keras顺序模型的解决方案。

I have CNN that I have built using on Tensor-flow 2.0. I need to access outputs of the intermediate layers. I was going over other stackoverflow questions that were similar but all had solutions involving Keras sequential model.

我尝试使用model.layers [index] .output,但得到

I have tried using model.layers[index].output but I get


layer conv2d没有入站节点。

Layer conv2d has no inbound nodes.

我可以在这里发布代码(这是超长的代码),但是即使没有有人可以指出我如何在急切模式下仅使用Tensorflow 2.0即可完成。

I can post my code here (which is super long) but I am sure even without that someone can point to me how it can be done using just Tensorflow 2.0 in eager mode.

推荐答案

我偶然发现了这个问题,而寻找答案,我花了一些时间弄清楚,因为我默认使用TF 2.0中的模型子类化API(如此处 https://www.tensorflow.org/tutorials/quickstart/advanced )。
如果有人处于类似情况,则您需要做的就是分配所需的中间输出作为类的属性。然后,在没有@ tf.function装饰器的情况下保留test_step,并创建其装饰后的副本(例如val_step),以便在训练过程中高效地对验证性能进行内部计算。作为一个简短的示例,我从链接上相应地修改了教程的一些功能。我假设我们需要在展平后访问输出。

I stumbled onto this question while looking for an answer and it took me some time to figure out as I use the model subclassing API in TF 2.0 by default (as in here https://www.tensorflow.org/tutorials/quickstart/advanced). If somebody is in a similar situation, all you need to do is assign the intermediate output you want, as an attribute of the class. Then keep the test_step without the @tf.function decorator and create its decorated copy, say val_step, for efficient internal computation of validation performance during training. As a short example, I have modified a few functions of the tutorial from the link accordingly. I'm assuming we need to access the output after flattening.

def call(self, x):
    x = self.conv1(x)
    x = self.flatten(x)
    self.intermediate=x #assign it as an object attribute for accessing later
    x = self.d1(x)
return self.d2(x)

#Remove @tf.function decorator from test_step for prediction
def test_step(images, labels):
    predictions = model(images, training=False)
    t_loss = loss_object(labels, predictions)
    test_loss(t_loss)
    test_accuracy(labels, predictions)
    return

#Create a decorated val_step for object's internal use during training
@tf.function
def val_step(images, labels):
    return test_step(images, labels)

现在在训练后运行model.predict()时,使用未经修饰的测试步骤,您可以使用model.intermediate访问中间输出获得其值的EagerTensor只需通过model.intermediate.numpy()编辑即可。但是,如果您不从test_step中删除@tf_function装饰器,则会返回一个Tensor,其值并不是很容易获得。

Now when you run model.predict() after training, using the un-decorated test step, you can access the intermediate output using model.intermediate which would be an EagerTensor whose value is obtained simply by model.intermediate.numpy(). However, if you don't remove the @tf_function decorator from test_step, this would return a Tensor whose value is not so straightforward to obtain.

这篇关于急切模式下访问Tensor-flow 2.0中的中间层的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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