eval_loss_and_grads <- function(x, loss_and_grads) {
outs <- loss_and_grads(list(x))
loss_value <- outs[[1]]
grad_values <- outs[[2]]
list(loss_value, grad_values)
}
gradient_ascent <- function(x, loss_and_grads, iterations, step, max_loss = NULL) {
for (i in 1:iterations) {
c(loss_value, grad_values) %<-% eval_loss_and_grads(x, loss_and_grads)
if (!is.null(max_loss) && loss_value > max_loss)
break
cat("...Loss value at", i, ":", loss_value, "\n")
x <- x + (step * grad_values)
}
x
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.