파이썬 에러
RuntimeError: Detected a call to `Model.predict` inside a `tf.function`.
나는야석사
2024. 1. 4. 10:30
파이썬 3.8에 tensorflow-gpu==2.9.0 버전을 사용하는 텐서플로우 환경에서 학습을 진행하는 도중 아래와 같은 오류가 발생했다.
오류메세지:
RuntimeError: Detected a call to `Model.predict` inside a `tf.function`. `Model.predict is a high-level endpoint that manages its own `tf.function`. Please move the call to `Model.predict` outside of all enclosing `tf.function`s. Note that you can call a `Model` directly on `Tensor`s inside a `tf.function` like: `model(x)`.
구글링한 결과, 텐서플로우에서 Model.predict 메소드가 tf.function 내부에서 호출될 때 발생한다. Model.predict는 자체적으로 tf.function을 관리하는데, 이 메소드를 tf.function의 외부에서 호출해야 한다.
따라서 모델 예측 코드를 tf.function 밖으로 이동시키거나 model(x)와 같은 방식으로 모델을 직접 호출하여 텐서에 대해 예측을 수행해야 한다.
해결방법:
Model.predict를 f.function 바깥에서 사용하거나, model(x) 형태로 직접 모델을 호출해야 한다.