本文介绍了为什么从tf.keras.Model继承时,用'self'定义的变量会自动赋予ListWrapper()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ListWrapper()并不熟悉,但是当我的课程从tf.keras.Model继承时,它将应用于由self创建的所有列表变量. https://www.tensorflow.org/api_docs/python/tf/keras/models/Model

I am not familiar with ListWrapper(), but it is being applied to all list variables created with self when my class inherits from tf.keras.Model. https://www.tensorflow.org/api_docs/python/tf/keras/models/Model

这很糟糕,因为当我在某些功能中使用它,甚至只是将其传递到我的Tensorflow模型中时,都会导致它产生IndexError. (我正在执行急切的操作)

This is bad because it is causing an IndexError when I use it in certain functions, or even by just passing it through my Tensorflow model. (I am using eager execution)

使用以下代码可以看到问题的一小部分再现:

A small reproduction of the problem can be seen with this code:

import tensorflow as tf

class my_class(tf.keras.Model):

    def __init__(self):
        super(my_class, self).__init__()

        self.x = [0]
        print(self.x)

model = my_class()

输出:

ListWrapper([0])

将继承设置为object即可解决此问题,这就是我知道导致此问题的tf.keras.Model的原因.

Switching the inheritance to be from object solves the issue, which is how I know its tf.keras.Model that is causing this.

我尝试查找它,但找不到任何东西.有小费吗?谢谢!

I tried looking it up but can't find anything on this. Any tips? Thanks!

推荐答案

原来这是tf.keras.Model和急切执行之间的Tensorflow中的错误.正如评论所建议的那样,这不是"tensorflow如何修补"设置属性".

Turns out this was a bug in Tensorflow between tf.keras.Model and eager execution. This was not "how the tensorflow has 'patched' setting attributes", as suggested by a comment.

这是Tensorflow上已解决问题的链接: https://github.com/tensorflow/tensorflow/issues/22853

This is a link to the closed issue on Tensorflow:https://github.com/tensorflow/tensorflow/issues/22853

如果您有此问题,则应在下一个Tensorflow更新中修复.此错误是1.11.0版中的

If you have this problem, it should be fixed in the next Tensorflow update.This bug was in version 1.11.0

这篇关于为什么从tf.keras.Model继承时,用'self'定义的变量会自动赋予ListWrapper()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 20:53