“ValueError:您的层或模型处于无效状态”从 0.16.1 升级到 tensorflow federated 0.17.0 后

问题描述

升级到 TFF 0.17.0 后遇到错误。相同的代码在 TFF 0.16.1 中完美运行。训练在两个版本中都运行良好,但是当我尝试将权重从 FL 状态复制到模型以在测试数据集上对其进行评估时,出现以下错误

File "fl/main_fl.py",line 166,in keras_evaluate                                                                 
    loss,accuracy = self.model.evaluate(test_dataset,verbose=0)                                                                             
  File "/home/ubuntu/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_v1.py",line 905,in evaluate
    self._assert_built_as_v1()
  File "/home/ubuntu/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py",line 852,i$ _assert_built_as_v1
    (type(self),))
ValueError: Your Layer or Model is in an invalid state. This can happen for the following cases:
 1. You might be interleaving estimator/non-estimator models or interleaving models/layers made in tf.compat.v1.Graph.as_default() with model$/layers created outside of it. Converting a model to an estimator (via model_to_estimator) invalidates all models/layers made before the conv$rsion (even if they were not the model converted to an estimator). Similarly,making a layer or a model inside a a tf.compat.v1.Graph invalid$tes all layers/models you prevIoUsly made outside of the graph.
2. You might be using a custom keras layer implementation with  custom __init__ which didn't call super().__init__.  Please check the impleme$tation of <class 'tensorflow.python.keras.engine.functional.Functional'> and its bases.

以下是我的 keras_evaluate 方法

    def keras_evaluate(self,test_dataset,mode='test',step=0):
        self.state.model.assign_weights_to(self.model)
        loss,verbose=0)
        print('Mode={},Loss={},Accuracy={}'.format(mode,loss,accuracy))

self.state 是 tff.learning.build_federated_averaging_process 返回的状态,即 tff.templates.IterativeProcess,test_dataset 是 tf.data.Dataset 类型,self.model 是 tf.keras.Model 类型,即 keras 功能模型。我有一个自定义层,但它确实有 super() 方法,因此错误中的第 2 点误导了我。

任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...