使用 Python 代码进行深度学习不再有效.“类型错误:函数构建代码之外的操作正在传递一个“图形"张量." [英] Deep Learning with Python code no longer working. "TypeError: An op outside of the function building code is being passed a "Graph" tensor."

查看:28
本文介绍了使用 Python 代码进行深度学习不再有效.“类型错误:函数构建代码之外的操作正在传递一个“图形"张量."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个 Tensorflow Variational Autoencoder,完全从Python 深度学习"一书中复制代码.直到几天前,代码运行良好,但截至昨天它已停止运行(我没有更改代码).

I'm implementing a Tensorflow Variational Autoencoder, copying the code exactly from the book "Deep Learning with Python". Up until a few days ago, the code was working perfectly but as of yesterday it has stopped working (I have not changed the code).

该代码用于生成模型,该模型可以从 MNIST 数据集中复制图像.

The code is for a generative model which can replicate images from the MNIST dataset.

具体的错误信息如下:

TypeError: 正在传递函数构建代码之外的操作图"张量.有可能有图张量通过包含一个从函数构建上下文中泄漏出来tf.init_scope 在您的函数构建代码中.图张量名称为:dense_2/BiasAdd:0

TypeError: An op outside of the function building code is being passed a "Graph" tensor. It is possible to have Graph tensors leak out of the function building context by including a tf.init_scope in your function building code. The graph tensor has name: dense_2/BiasAdd:0

我在下面的 Google Collaborative 文件中提供了代码,因此您可以尝试自己运行它:

I have made the code available in the Google Collaborative file below, so you can try running it yourself:

https://colab.research.google.com/drive/1ArmP3Nns2T_i-Yt-yDXoudp6Lhnw-ghu?usp=sharing

推荐答案

您定义的用于计算损失的自定义层,即 CustomVariationalLayer,正在访问尚未传递给的模型张量它直接.这是不允许的,因为启用了eager模式,但图层中的函数默认在图形模式下执行.要解决此问题,您可以使用 tf.compat.v1.disable_eager_execution() 完全禁用 Eager 模式,或者改为使用 tf.config.run_functions_eagerly(True) 来使所有功能都急切地运行.

The custom layer you have defined to compute the loss, i.e. CustomVariationalLayer, is accessing tensors of the model which have not been passed to it directly. This is not allowed since eager mode is enabled but the function in the layer is by default executed in graph mode. To resolve this issue, you can either disable eager mode entirely using tf.compat.v1.disable_eager_execution(), or instead use tf.config.run_functions_eagerly(True) to make all the functions run eagerly.

然而,上述两种解决方案可能都不理想,因为它们正在修改 TF 的默认行为(尤其是后者,因为它可能会降低性能).因此,您可以修改 CustomVariationalLayer 的定义以将 z_meanz_log_var 作为其他输入:

However, both of above solutuons may not be desirable since they are modifying the default behavior of TF (especially the latter one, since it might reduce the performance). Therefore, instead of using those solutions, you can modify the definition of CustomVariationalLayer to take z_mean and z_log_var as its other inputs:

class CustomVariationalLayer(keras.layers.Layer):
    def vae_loss(self, x, z_decoded, z_mean, z_log_var):
        # ...

    def call(self, inputs):
        x = inputs[0]
        z_decoded = inputs[1]
        z_mean = inputs[2]
        z_log_var = inputs[3]
        loss = self.vae_loss(x, z_decoded, z_mean, z_log_var)
        # ...

y = CustomVariationalLayer()([input_img, z_decoded, z_mean, z_log_var])

这篇关于使用 Python 代码进行深度学习不再有效.“类型错误:函数构建代码之外的操作正在传递一个“图形"张量."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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