import tensorflow as tf

w = tf.Variable([[1.0]])

with tf.GradientTape() as tape:
  loss = w * w

grad = tape.gradient(loss, w)
print("grad:",grad)  

# grad: tf.Tensor([[2.]], shape=(1, 1), dtype=float32)

