I am following the tutorial on neural style transfer. The style transfer is done by minimizing a loss function with respect to an image (initialized with the content image). What confuses me is the following piece of code:
preprocessed_input = tf.keras.applications.vgg19.preprocess_input(inputs)
which is part of the call
method in the StyleContentModel
class. How does TensorFlow know the gradient of this operation? I have checked if this operation has a gradient function using get_gradient_function
in the module tensorflow.python.framework.ops
, and as far as I can tell it does not.
It is very simple, the function internally uses symbolic tensor operations that are differentiable. TensorFlow can compute gradients through functions that internally use TensorFlow operations, there is no need to manually define a gradient for each function.
You can confirm by looking at the code of that function here, specially if you look at the
_preprocess_symbolic_function
here which is using normal scalar operations and Keras backend functions (which are just TensorFlow functions intf.keras
).