Learn R Programming

reservr (version 0.0.2)

tf_initialise_model: Initialise model weights to a global parameter fit

Description

Initialises a compiled reservr_keras_model weights such that the predictions are equal to, or close to, the distribution parameters given by params.

Usage

tf_initialise_model(
  model,
  params,
  mode = c("scale", "perturb", "zero", "none")
)

Value

Invisibly model with changed weights

Arguments

model

A reservr_compiled_model obtained by tf_compile_model().

params

A list of distribution parameters compatible with model.

mode

An initialisation mode

scale

Initialise the biases according to params and the kernels uniform on [-0.1, 0.1] * bias scale.

perturb

Initialise the biases according to params and leave the kernels as is.

zero

Initialise the biases according to params and set the kernel to zero.

none

Don't modify the weights.

Examples

Run this code
dist <- dist_exponential()
group <- sample(c(0, 1), size = 100, replace = TRUE)
x <- dist$sample(100, with_params = list(rate = group + 1))
global_fit <- fit(dist, x)

if (interactive() && keras::is_keras_available()) {
  library(keras)
  l_in <- layer_input(shape = 1L)
  mod <- tf_compile_model(
    inputs = list(l_in),
    intermediate_output = l_in,
    dist = dist,
    optimizer = optimizer_adam(),
    censoring = FALSE,
    truncation = FALSE
  )
  tf_initialise_model(mod, global_fit$params)
  fit_history <- fit(
    mod,
    x = group,
    y = x,
    epochs = 200L
  )

  predicted_means <- predict(mod, data = k_constant(c(0, 1)))
}

Run the code above in your browser using DataLab