Animation of optimization in torch
In this post I will show you how to use the {gganimate} R package to make an animated GIF illustrating Adam optimization of a function using {torch}:
library(torch)
library(gganimate)
library(tidyverse)
We will use
torch::optim_adam()
to find the value of x that minimizes the following function:
f <- function(x) (6 * x - 2) ^ 2 * sin(12 * x - 4)
The function looks as follows:
The adam_iters
dataset will contain an iter
column (for the iteration/step identifier) and an x
column (the value of x after each iteration):