ascend: Perform A Single Gradient Ascent

Description Usage Arguments Value Examples

View source: R/7-AM.R

Description

Perform A Single Gradient Ascent

Usage

1
ascend(model, input, loss, gradient, lr = 1)

Arguments

model

A keras model.

input

The input to perturb.

loss

The output from get_layer_loss.

gradient

The output from get_layer_gradient.

lr

The learning rate used to update input.

Value

The perturbed input.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
library(keras)
library(caress)
data(iris)
x <- as.matrix(iris[,1:4])
y <- to_categorical(as.numeric(iris[,5])-1)
input <- from_input(x, name = "input")
output <- input %>%
  layer_dense(units = 2, activation = "linear", name = "middle") %>%
  to_output(y, name = "output")
model <- prepare(input, output)
build(model, x, y, epochs = 10, batch_size = 8)
loss <- get_layer_loss(model, "middle")
gradient <- get_layer_gradient(model, loss)
x <- matrix(runif(4), 1, 4)
for(i in 1:20){
  x <- ascend(model, x, loss, gradient)
}

tpq/caress documentation built on March 11, 2021, 8:03 p.m.