Skip to content

Instantly share code, notes, and snippets.

View yang-song's full-sized avatar

Yang Song yang-song

View GitHub Profile
@yang-song
yang-song / grad_lib.py
Last active August 8, 2022 19:38
L operator and R operator in Tensorflow
import tensorflow as tf
def gradients(f, x, grad_ys=None):
'''
An easier way of computing gradients in tensorflow. The difference from tf.gradients is
* If f is not connected with x in the graph, it will output 0s instead of Nones. This will be more meaningful
for computing higher-order gradients.
* The output will have the same shape and type as x. If x is a list, it will be a list. If x is a Tensor, it