# Custom model trigerring retracing warning

**URL:** <https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938>\
**Category:** General Discussion\
**Tags:** help\_request\
**Created:** [August 16, 2021, 11:21am UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938 "2021-08-16T11:21:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![flr](https://avatars.discourse-cdn.com/v4/letter/f/7c8e57/32.png) [@flr](https://discuss.ai.google.dev/u/flr)\
**Post date:** [August 16, 2021, 11:21am UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/1 "2021-08-16T11:21:34Z")

</div>

Hello!

Can someone please advise me how can I get rid of this warning received while training a custom model?  
If I train several times a custom model with the same data, I receive the warning below once in a while.  
It is strange also that the message says that only in 6 out of 8 tries it encountered a problem, although I use exactly the same data every time.  
I use only tensorflow objects so I do not know what generates this message.

“WARNING:tensorflow:6 out of the last 8 calls to \<function Model.make\_predict\_function..predict\_function at 0x7f90184d5310\> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental\_relax\_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to [Better performance with tf.function &nbsp;|&nbsp; TensorFlow Core](https://www.tensorflow.org/guide/function#controlling_retracing) and [tf.function &nbsp;|&nbsp; TensorFlow v2.16.1](https://www.tensorflow.org/api_docs/python/tf/function) for more details.”

Thank you,

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [August 16, 2021, 11:26am UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/2 "2021-08-16T11:26:27Z")

</div>

Are you using [tf.data](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) API?

---

<div class="post-metadata">

**Author:** ![flr](https://avatars.discourse-cdn.com/v4/letter/f/7c8e57/32.png) [@flr](https://discuss.ai.google.dev/u/flr)\
**Post date:** [August 16, 2021, 1:27pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/4 "2021-08-16T13:27:50Z")

</div>

Training data is in ndarray format.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [August 16, 2021, 2:19pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/5 "2021-08-16T14:19:18Z")

</div>

What is your TF version?

---

<div class="post-metadata">

**Author:** ![flr](https://avatars.discourse-cdn.com/v4/letter/f/7c8e57/32.png) [@flr](https://discuss.ai.google.dev/u/flr)\
**Post date:** [August 16, 2021, 2:54pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/7 "2021-08-16T14:54:48Z")

</div>

TF version Is: 2.5.0

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [August 16, 2021, 3:05pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/8 "2021-08-16T15:05:46Z")

</div>

Do you have a Colab to reproduce It?

---

<div class="post-metadata">

**Author:** ![gabbygab1233](https://avatars.discourse-cdn.com/v4/letter/g/3be4f8/32.png) [@gabbygab1233](https://discuss.ai.google.dev/u/gabbygab1233)\
**Post date:** [August 17, 2021, 8:04am UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/10 "2021-08-17T08:04:24Z")

</div>

same problem with 2.5.0

---

<div class="post-metadata">

**Author:** ![anon10279914](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anon10279914](https://discuss.ai.google.dev/u/anon10279914)\
**Post date:** [August 17, 2021, 12:39pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/12 "2021-08-17T12:39:18Z")

</div>

hi, as the warning message says, you are decorating some code using `tf.function` that is leading parts of the computation getting traced multiple times (for example you are decorating the call of a layer, data transformation, etc). (its just a warning telling you that its not convenient to do that in terms of performance since tracing is expensive) , if you want to decorate functions with `tf.function` make sure you have it either outside of the loop or decorating an expensive operation (for example an update step function where `tf.GradientTape` is used).

one example of bad usage that happened to me when tf 2.0 came out: you have a custom layer where the call is decorated with `tf.function`… then the message will appear

> <https://stackoverflow.com/questions/55149026/tensorflow-2-0-do-you-need-a-tf-function-decorator-on-top-of-each-function>

---

<div class="post-metadata">

**Author:** ![flr](https://avatars.discourse-cdn.com/v4/letter/f/7c8e57/32.png) [@flr](https://discuss.ai.google.dev/u/flr)\
**Post date:** [August 17, 2021, 12:43pm UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/13 "2021-08-17T12:43:53Z")

</div>

Hi, it is a custom layer but I am not using `tf.function` at all.

---

<div class="post-metadata">

**Author:** ![Jiaxin\_LIU](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/jiaxin_liu/32/11337_2.png) [@Jiaxin\_LIU](https://discuss.ai.google.dev/u/Jiaxin_LIU)\
**Post date:** [December 7, 2022, 7:30am UTC](https://discuss.ai.google.dev/t/custom-model-trigerring-retracing-warning/30938/15 "2022-12-07T07:30:42Z")

</div>

have the the same issue here, wondering if you find a way to solve the problem
