经过搜索,实践,最优解: python - tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead - Stack Overflow 主要是加这两行代码: import tensorflow as tf tf.compat.v1.disable_eager_execution()