简体   繁体   English

TensorFlow 如何计算 vgg19.preprocess_input 的梯度?

[英]How does TensorFlow compute the gradient of vgg19.preprocess_input?

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).风格转移是通过最小化图像(使用内容图像初始化)的损失 function 来完成的。 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?这是StyleContentModel class中call方法的StyleContentModel是怎么知道这个操作的梯度的呢? 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.我已经使用模块get_gradient_function中的 get_gradient_function 检查了此操作是否具有梯度tensorflow.python.framework.ops ,据我所知它没有。

This has nothing to do with the model or gradients.这与 model 或梯度无关。 What this function does is scale the input images so the pixels are in the range from -1 to +1.这个 function 所做的是缩放输入图像,使像素在 -1 到 +1 的范围内。 This is a common requirement for many models used in transfer learning like VGG and MobileNet.这是迁移学习中使用的许多模型(如 VGG 和 MobileNet)的共同要求。 If you use the ImageDataGenerator it has a parameter preprocessing_function which the generator calls to preprocess the images.如果您使用 ImageDataGenerator,它有一个参数 preprocessing_function,生成器调用它来预处理图像。 Make sure if you preprocess the training images you do the same for the test and validation images.确保如果对训练图像进行预处理,则对测试和验证图像进行相同的处理。

It is very simple, the function internally uses symbolic tensor operations that are differentiable.很简单,function内部使用了可微分的符号张量运算。 TensorFlow can compute gradients through functions that internally use TensorFlow operations, there is no need to manually define a gradient for each function. TensorFlow 可以通过内部使用 TensorFlow 操作的函数计算梯度,无需为每个 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 in tf.keras ).您可以通过在此处查看 function 的代码来确认,特别是如果您在此处查看使用正常标量操作的_preprocess_symbolic_function和 Keras 后端函数(它们只是 tf.keras 中的tf.keras函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM