How to include TFLite inference in a gradient tape

Hi! I’m following this tutorial on generating adversarial images using TensorFlow and reaching the point where we use GradientTape() to generate the gradient of the loss with respect to the image.

I’d really love to experiment with using a TFLite model here, but when I replace this line:

prediction = pretrained_model(input_image)

with a TFLite interpreter performing inference to get a prediction, the following statement returns None:

tape.gradient(loss, input_image)

Can anyone here help me understand why a TFLite interpreter performing inference wouldn’t record to the tape? Could anyone share an example of invoking a TFLite model from within a gradient tape?

Hi @christian-westbrook,Gradient tapes use memory to store intermediate results, including inputs and outputs, for use during the backwards pass to update model parameters. During inference, you only need to run the forward pass of the model to get predictions, and there’s no need to compute gradients or update model parameters. Thank You.

Thanks for responding @Kiran_Sai_Ramineni!!

Keep in mind that the goal here is not to make predictions, which I can do successfully. The goal is to generate the gradient of the loss for a given input image with respect to the image. That gradient can be used to create an adversarial perturbation!