Skip to content

Commit

Permalink
update custom model docs to use self
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Aug 2, 2018
1 parent 6a5fcf1 commit 56d1e0e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
18 changes: 9 additions & 9 deletions tests/testthat/test-custom-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ keras_model_simple_mlp <- function(num_classes,
name = NULL) {

# define and return a custom model
keras_model_custom(name = name, function(model) {
keras_model_custom(name = name, function(self) {

# create layers we'll need for the call (this code executes once)
model$dense1 <- layer_dense(units = 32, activation = "relu")
model$dense2 <- layer_dense(units = num_classes, activation = "softmax")
self$dense1 <- layer_dense(units = 32, activation = "relu")
self$dense2 <- layer_dense(units = num_classes, activation = "softmax")
if (use_dp)
model$dp <- layer_dropout(rate = 0.5)
self$dp <- layer_dropout(rate = 0.5)
if (use_bn)
model$bn <- layer_batch_normalization(axis = -1)
self$bn <- layer_batch_normalization(axis = -1)

# implement call (this code executes during training & inference)
function(inputs, mask = NULL) {
x <- model$dense1(inputs)
x <- self$dense1(inputs)
if (use_dp)
x <- model$dp(x)
x <- self$dp(x)
if (use_bn)
x <- model$bn(x)
model$dense2(x)
x <- self$bn(x)
self$dense2(x)
}
})
}
Expand Down
22 changes: 11 additions & 11 deletions vignettes/custom_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,34 @@ keras_model_simple_mlp <- function(num_classes,
name = NULL) {
# define and return a custom model
keras_model_custom(name = name, function(model) {
keras_model_custom(name = name, function(self) {
# create layers we'll need for the call (this code executes once)
model$dense1 <- layer_dense(units = 32, activation = "relu")
model$dense2 <- layer_dense(units = num_classes, activation = "softmax")
self$dense1 <- layer_dense(units = 32, activation = "relu")
self$dense2 <- layer_dense(units = num_classes, activation = "softmax")
if (use_dp)
model$dp <- layer_dropout(rate = 0.5)
self$dp <- layer_dropout(rate = 0.5)
if (use_bn)
model$bn <- layer_batch_normalization(axis = -1)
self$bn <- layer_batch_normalization(axis = -1)
# implement call (this code executes during training & inference)
function(inputs, mask = NULL) {
x <- model$dense1(inputs)
x <- self$dense1(inputs)
if (use_dp)
x <- model$dp(x)
x <- self$dp(x)
if (use_bn)
x <- model$bn(x)
model$dense2(x)
x <- self$bn(x)
self$dense2(x)
}
})
}
```

Note that we include a `name` parameter so that users can optionally provide a human readable name for the model.

Note also that when we create layers to be used in our forward pass we set them onto the `model` object so they are tracked appropriately by Keras.
Note also that when we create layers to be used in our forward pass we set them onto the `self` object so they are tracked appropriately by Keras.

In `call()`, you may specify custom losses by calling `model$add_loss()`. You can also access any other members of the Keras model you need (or even add fields to the model) by using `model$`.
In `call()`, you may specify custom losses by calling `self$add_loss()`. You can also access any other members of the Keras model you need (or even add fields to the model) by using `self$`.

## Using a Custom Model

Expand Down
34 changes: 17 additions & 17 deletions vignettes/examples/nmt_attention.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ attention_encoder <-
embedding_dim,
src_vocab_size,
name = NULL) {
keras_model_custom(name = name, function(model) {
model$embedding <-
keras_model_custom(name = name, function(self) {
self$embedding <-
layer_embedding(input_dim = src_vocab_size,
output_dim = embedding_dim)
model$gru <-
self$gru <-
layer_gru(
units = gru_units,
return_sequences = TRUE,
Expand All @@ -180,8 +180,8 @@ attention_encoder <-
x <- inputs[[1]]
hidden <- inputs[[2]]

x <- model$embedding(x)
c(output, state) %<-% model$gru(x, initial_state = hidden)
x <- self$embedding(x)
c(output, state) %<-% self$gru(x, initial_state = hidden)

list(output, state)
}
Expand All @@ -199,20 +199,20 @@ attention_decoder <-
embedding_dim,
target_vocab_size,
name = NULL) {
keras_model_custom(name = name, function(model) {
model$gru <-
keras_model_custom(name = name, function(self) {
self$gru <-
layer_gru(
units = gru_units,
return_sequences = TRUE,
return_state = TRUE
)
model$embedding <-
self$embedding <-
layer_embedding(input_dim = target_vocab_size, output_dim = embedding_dim)
gru_units <- gru_units
model$fc <- layer_dense(units = target_vocab_size)
model$W1 <- layer_dense(units = gru_units)
model$W2 <- layer_dense(units = gru_units)
model$V <- layer_dense(units = 1L)
self$fc <- layer_dense(units = target_vocab_size)
self$W1 <- layer_dense(units = gru_units)
self$W2 <- layer_dense(units = gru_units)
self$V <- layer_dense(units = 1L)

function(inputs, mask = NULL) {
x <- inputs[[1]]
Expand All @@ -222,25 +222,25 @@ attention_decoder <-
hidden_with_time_axis <- k_expand_dims(hidden, 2)

score <-
model$V(k_tanh(
model$W1(encoder_output) + model$W2(hidden_with_time_axis)
self$V(k_tanh(
self$W1(encoder_output) + self$W2(hidden_with_time_axis)
))

attention_weights <- k_softmax(score, axis = 2)

context_vector <- attention_weights * encoder_output
context_vector <- k_sum(context_vector, axis = 2)

x <- model$embedding(x)
x <- self$embedding(x)

x <-
k_concatenate(list(k_expand_dims(context_vector, 2), x), axis = 3)

c(output, state) %<-% model$gru(x)
c(output, state) %<-% self$gru(x)

output <- k_reshape(output, c(-1, gru_units))

x <- model$fc(output)
x <- self$fc(output)

list(x, state, attention_weights)

Expand Down
16 changes: 8 additions & 8 deletions vignettes/guide_keras.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,20 @@ A custom model is defined by calling `keras_model_custom()` passing a function t
my_model <- function(input_dim, output_dim, name = NULL) {
# define and return a custom model
keras_model_custom(name = name, function(model) {
keras_model_custom(name = name, function(self) {
# create layers we'll need for the call (this code executes once)
# note: the layers have to be created on the model object!
model$dense1 <- layer_dense(units = 64, activation = 'relu', input_shape = input_dim)
model$dense2 <- layer_dense(units = 64, activation = 'relu')
model$dense3 <- layer_dense(units = 10, activation = 'softmax')
# note: the layers have to be created on the self object!
self$dense1 <- layer_dense(units = 64, activation = 'relu', input_shape = input_dim)
self$dense2 <- layer_dense(units = 64, activation = 'relu')
self$dense3 <- layer_dense(units = 10, activation = 'softmax')
# implement call (this code executes during training & inference)
function(inputs, mask = NULL) {
x <- inputs %>%
model$dense1() %>%
model$dense2() %>%
model$dense3()
self$dense1() %>%
self$dense2() %>%
self$dense3()
x
}
})
Expand Down

0 comments on commit 56d1e0e

Please sign in to comment.