Q:

How to implement customized loss function and perform gradient back-propagation in tensorflow

I need to implement YOLOv2 based on tensorflow framework. 
Firstly, in my network design, there are five anchors for each cell and one class (face), thus finally the network outputs 4D tensor that has n * c * h * w shape. Here n represents the batch size, c = 5 * (location coordinates + objectiveness score + classification probability) = 5 * (4 + 1 + 1) = 30, and h/w represent height and width of feature map respectively.
Secondly, YOLOv2 adopts multi-task loss function:

So I defined the following function to calculate the total loss:
def yolov2_loss_function(pred, ground_truth, global_step)

This function accept three parameters: pred respresents the network output tensor which is already described above, ground_truth represents the corresponding GT, and global_step represents the number of iterations. This function returns a scalar value which is used to denote the totol loss.
Finally I use the following code to perform SGD train:
......
total_loss = yolov2_loss_function(pred, gt, global_step)
optimizer = tf.train.MomentumOptimizer(learning_rate=lr, momentum=momentum).minimize(total_loss, global_step=global_step)
......

I am not sure if the above process is correct. Especially the total_loss variable  is just a scalar, how does the tensorflow framework know the residual/gradient of each element in the output tensor and further perform backward propagation? I know mechanism of automatic differential，but the premise of the automatic differential is each output element should have residual.
Although in the function yolov2_loss_function I firstly calculate each element's residual, then output their total loss. However, how does the tensorflow framework know the residual of each output element?
Thank you very much.

A:

I think you mixed it up.
For any function differentiatable function $f\colon \mathbb{R}^n -> \mathbb{R}, x\mapsto f(x)$ the partial derivatives $\frac{\partial}{\partial x_i} f(x)$ exists.
Hence, even from a scalar-valued function the gradient can be written as vector, if the function maps a vector to a scalar.
For "f(a,b)=a * a * b" the derivative wrt. a is "2 * a * b" and wrt. b it is "a * a". No issue here.

