R/layers.noise.R

Defines functions GaussianDropout GaussianNoise

Documented in GaussianDropout GaussianNoise GaussianNoise

#' Apply Gaussian noise layer
#'
#' The function [GaussianNoise] applies additive noise,
#' centered around 0 and [GaussianDropout] applied multiplicative
#' noise centered around 1.
#'
#' @param stddev         standard deviation of the random Gaussian
#' @param rate           float, drop probability
#' @param input_shape    only need when first layer of a model;
#'                       sets the input shape of the data
#'
#' @example inst/examples/noise.R
#' @template boilerplate
#' @name GaussianNoise
NULL

#' @rdname GaussianNoise
#' @export
#' @family layers
GaussianNoise <- function(stddev = 1, input_shape = NULL) {
  keras_check()

  # Need special logic for input_shape because it is passed
  # via kwargs and needs to be manually adjusted
  if (is.null(input_shape)) {
    res <- modules$keras.layers.noise$GaussianNoise(stddev = stddev)
  } else {

    input_shape <- as.list(input_shape)
    input_shape <- modules$builtin$tuple(int32(input_shape))

    res <- modules$keras.layers.noise$GaussianNoise(stddev = stddev,
                              input_shape = input_shape)

  }

  return(res)
}


#' @rdname GaussianNoise
#' @export
GaussianDropout <- function(rate = 0.5, input_shape = NULL) {
  keras_check()

  # Need special logic for input_shape because it is passed
  # via kwargs and needs to be manually adjusted
  if (is.null(input_shape)) {
    res <- modules$keras.layers.noise$GaussianDropout(rate = rate)
  } else {

    input_shape <- as.list(input_shape)
    input_shape <- modules$builtin$tuple(int32(input_shape))

    res <- modules$keras.layers.noise$GaussianDropout(rate = rate,
                  input_shape = input_shape)

  }

  return(res)
}

Try the kerasR package in your browser

Any scripts or data that you put into this service are public.

kerasR documentation built on Aug. 17, 2022, 5:06 p.m.