问题
运行报错:
TypeError: 'Specificity' does not have the same nested structure after one iteration.
The two structures don't have the same nested structure.
First structure: type=list str=[]
Second structure: type=list str=[<tf.Tensor 'regular_loss/while/Cast_1:0' shape=() dtype=float32>]
More specifically: The two structures don't have the same number of elements. First structure: type=list str=[]. Second structure: type=list str=[<tf.Tensor 'regular_loss/while/Cast_1:0' shape=() dtype=float32>]
Entire first structure:[]
Entire second structure:[.]
解决方法
用tf.TensorArray()的方法声明“Specificity”变量,如:
Specificity = tf.TensorArray(tf.float32, size=nb_classes)
用对象的write()方法(形如:Specificity = Specificity.write())写入值;用stack()的方法提取值。
原因
函数中定义的“Specificity”在for循环中,属于变化的中间值。当用列表或者字典方法定义时,tf.2x默认为长度固定,所以出现迭代后长度(结构)不同的报错信息。当改用tf.TensorArray()方法时就可以顺利存储和调用循环中动态变化的值。
参考
https://www.tensorflow/guide/function
tensorflow2.0系列(4): Eager Execution和Auto Graph_lxy_Alex的博客-CSDN博'


发布评论