__iter__() is only supported inside of tf.function or when eager execution is enabled

def test(model):
count_test = 0 # 记录预测对了的图片数

pred_prob = []  # 初始化置信度列表,得到每张图片属于各个种类的置信度
real_test = []  # 初始化标签列表,得到每张图片的真实标签
pred_test = []

for i, (image, label) in enumerate(dataset_test):
    pred_label_prob = model(image)  # 得到每张图片属于各个种类的置信度
    pred_prob += list(pred_label_prob.numpy())  # 更新置信度列表
    
    pred_label_test = tf.cast(tf.argmax(pred_label_prob, axis=1), dtype=tf.int32)  # 得到预测到的每张图片的种类
    pred_test += list(pred_label_test.numpy())
    
    real_label_test = tf.cast(tf.argmax(label, axis=1), dtype=tf.int32)  # 得到每张图片的真实标签
    real_test += list(real_label_test.numpy())  # 更新标签列表

    correct_test = tf.equal(pred_label_test, real_label_test)  # 计算该批次预测正确的图片数
    count_test += tf.reduce_sum(tf.cast(correct_test, dtype=tf.int32))  # 累加得到测试集中预测正确的图片总数

acc_test = count_test / image_count_test  # 计算准确率
print('Accuracy: {}'.format(acc_test))
return real_test, pred_prob, pred_test

proposed_real_test, proposed_pred_prob, proposed_pred_test = test(model_1)

巡行上述代码后出现RuntimeError: iter() is only supported inside of tf.function or when eager execution is enabled. 我的版本是2.0的 不知道怎么解决

和这个有关?
默认情况下,Eager execution处于启用状态,可以用tf.executing_eargerly()查看Eager Execution当前的启动状态,返回True则是开启,False是关闭。可以用tf.compat.v1.enable_eager_execution()启动eager模式。