# C2\_W1\_Lab\_2\_gradient-tape-basics - one or two gradient tapes

**URL:** <https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347>\
**Category:** Custom and Distributed Training with TF\
**Tags:** week-module-1\
**Created:** [July 1, 2024, 10:43am UTC](https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347 "2024-07-01T10:43:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ip84](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@ip84](https://community.deeplearning.ai/u/ip84)\
**Post date:** [July 1, 2024, 10:43am UTC](https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347/1 "2024-07-01T10:43:58Z")

</div>

I experienced with the following code to see if we can use just one tape here:

```auto
x = tf.Variable(1.0)

with tf.GradientTape(persistent=True) as tape_2:
   
    y = x * x * x

    dy_dx = tape_2.gradient(y, x)
        

d2y_dx2 = tape_2.gradient(dy_dx, x)

print(dy_dx)
print(d2y_dx2)

```

* * *

## I got the right ouput but with a warning: WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives. tf.Tensor(3.0, shape=(), dtype=float32) tf.Tensor(6.0, shape=(), dtype=float32)

Question: As our goal is that the tradient inside the context will be recorded in this case, for calculating the 2nd gradient later, is it a good pratice to use just one tape in this way, or it’s really better to use 2 tapes as we learned and avoid the warning?

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [July 1, 2024, 10:46am UTC](https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347/2 "2024-07-01T10:46:39Z")

</div>

I think its better to avoid the warnings and it gives the reason too, higher CPU and memory usage)!

---

<div class="post-metadata">

**Author:** ![ip84](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@ip84](https://community.deeplearning.ai/u/ip84)\
**Post date:** [July 1, 2024, 10:52am UTC](https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347/3 "2024-07-01T10:52:02Z")

</div>

Why don’t we get the same working in this following case too?  
Here also the first gradient calculation is within a block, aka the second block.

```auto
x = tf.Variable(1.0)

with tf.GradientTape() as tape_2:
    with tf.GradientTape() as tape_1:
        y = x * x * x

        dy_dx = tape_1.gradient(y, x)
        
    # this is also acceptable
    d2y_dx2 = tape_2.gradient(dy_dx, x)

print(dy_dx)
print(d2y_dx2)

```

---

<div class="post-metadata">

**Author:** ![ip84](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@ip84](https://community.deeplearning.ai/u/ip84)\
**Post date:** [July 1, 2024, 10:54am UTC](https://community.deeplearning.ai/t/c2-w1-lab-2-gradient-tape-basics-one-or-two-gradient-tapes/656347/4 "2024-07-01T10:54:14Z")

</div>

OK, it’s not persistent … I got it.
