Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Gradient Tape | Neural Networks Foundations
Introduction to TensorFlow
セクション 3.  2
single

single

Gradient Tape

メニューを表示するにはスワイプしてください

Gradient Tape

Understanding fundamental tensor operations allows progression to streamlining and accelerating these processes using built-in TensorFlow features. The first of these advanced tools to explore is Gradient Tape.

What is Gradient Tape?

This chapter delves into one of the fundamental concepts in TensorFlow, the Gradient Tape. This feature is essential for understanding and implementing gradient-based optimization techniques, particularly in deep learning.

Gradient Tape in TensorFlow is a tool that records operations for automatic differentiation. When operations are performed inside a Gradient Tape block, TensorFlow keeps track of all the computations that happen. This is particularly useful for training machine learning models, where gradients are needed to optimize model parameters.

Note
Note

Essentially, a gradient is a set of partial derivatives.

Usage of Gradient Tape

To use Gradient Tape, follow these steps:

  • Create a Gradient Tape block: use with tf.GradientTape() as tape:. Inside this block, all the computations are tracked;
  • Define the computations: perform operations with tensors within the block (e.g. define a forward pass of a neural network);
  • Compute gradients: use tape.gradient(target, sources) to calculate the gradients of the target with respect to the sources.

Simple Gradient Calculation

A simple example to understand this better.

123456789101112131415
import tensorflow as tf # Define input variables x = tf.Variable(3.0) # Start recording the operations with tf.GradientTape() as tape: # Define the calculations y = x * x # Extract the gradient for the specific input (`x`) grad = tape.gradient(y, x) print(f'Result of y: {y}') print(f'The gradient of y with respect to x is: {grad.numpy()}')

This code calculates the gradient of y = x^2 at x = 3. This is the same as the partial derivative of y with respect to x.

simple_derivative

Several Partial Derivatives

When output is influenced by multiple inputs, a partial derivative can be computed with respect to each of these inputs (or just a selected few). This is achieved by providing a list of variables as the sources parameter.

The output from this operation will be a corresponding list of tensors, where each tensor represents the partial derivative with respect to each of the variables specified in sources.

1234567891011121314151617
import tensorflow as tf # Define input variables x = tf.Variable(tf.fill((2, 3), 3.0)) z = tf.Variable(5.0) # Start recording the operations with tf.GradientTape() as tape: # Define the calculations y = tf.reduce_sum(x * x + 2 * z) # Extract the gradient for the specific inputs (`x` and `z`) grad = tape.gradient(y, [x, z]) print(f'Result of y: {y}') print(f"The gradient of y with respect to x is:\n{grad[0].numpy()}") print(f"The gradient of y with respect to z is: {grad[1].numpy()}")

This code computes the gradient of the function y = sum(x^2 + 2*z) for given values of x and z. In this example, the gradient of x is shown as a 2D tensor, where each element corresponds to the partial derivative of the respective value in the original x matrix.

partial_derivatives
Example Description
expand arrow

Calculating the Gradient with Respect to x

When we calculate the derivative of y with respect to x, we're essentially asking, "If we change the value of x a little bit, how much does y change?" The operation on x is squaring (x^2), so the derivative tells us that for each unit increase in x, y will increase by 2*x.

Since all elements of x are 3.0:

  • Each element's gradient is 2*3.0 = 6.0;
  • The gradient matrix with respect to x mirrors the shape of x itself (2x3), where every element is 6.0. This indicates that increasing any single element of x by 1 would increase y by 6 units.

Calculating the Gradient with Respect to z

For z, we're considering how changes in z affect y. Since z is added to each squared element of x (after doubling), we're effectively asking, "If z increases by 1, how much does y increase?" By the rules of broadcasting, 2*z is added to each element of x^2, as if we had a matrix the same size as x, filled with 2*z.

  • The operation of 2*z with respect to z is straightforward: its derivative is 2 because 2*z changes by 2 units for every unit change in z;
  • However, since this operation affects each element of the matrix x, the total change in y for a unit change in z is 2 times the number of elements in x (which is 6, from the 2x3 matrix). Thus, the total gradient with respect to z is 2*6 = 12. This means that increasing z by 1 unit increases y by 12 units, reflecting the combined effect of the change across all elements of x.

In Summary

  • The gradient with respect to x is a matrix that shows how a unit increase in any element of x leads to a 6-unit increase in y, reflecting the direct relationship between changes in x and changes in y;
  • The gradient with respect to z is a scalar (12), showing the total effect of a unit increase in z on y, taking into account the impact across the entire matrix x.
Note
Note

For additional insights into Gradient Tape capabilities, including higher-order derivatives and extracting the Jacobian matrix, refer to the official TensorFlow documentation.

タスク

スワイプしてコーディングを開始

Your goal is to compute the gradient (derivative) of a given mathematical function at a specified point using TensorFlow's Gradient Tape. The function and point will be provided, and you will see how to use TensorFlow to find the gradient at that point.

Consider a quadratic function of a single variable x, defined as:

f(x) = x^2 + 2x - 1

Your task is to calculate the derivative of this function at x = 2.

Steps

  1. Define the variable x at the point where you want to compute the derivative.
  2. Use Gradient Tape to record the computation of the function f(x).
  3. Calculate the gradient of f(x) at the specified point.

Note

Gradient can only be computed for values of floating-point type.

Derivative of a Function at a Point

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  2
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt