constraints.Rd
Functions that impose constraints on weight values.
constraint_maxnorm(max_value = 2, axis = 0) constraint_nonneg() constraint_unitnorm(axis = 0) constraint_minmaxnorm(min_value = 0, max_value = 1, rate = 1, axis = 0)
max_value | The maximum norm for the incoming weights. |
---|---|
axis | The axis along which to calculate weight norms. For instance, in
a dense layer the weight matrix has shape |
min_value | The minimum norm for the incoming weights. |
rate | The rate for enforcing the constraint: weights will be rescaled to yield (1 - rate) * norm + rate * norm.clip(low, high). Effectively, this means that rate=1.0 stands for strict enforcement of the constraint, while rate<1.0 means that weights will be rescaled at each step to slowly move towards a value inside the desired interval. |
constraint_maxnorm()
constrains the weights incident to each
hidden unit to have a norm less than or equal to a desired value.
constraint_nonneg()
constraints the weights to be non-negative
constraint_unitnorm()
constrains the weights incident to each hidden
unit to have unit norm.
constraint_minmaxnorm()
constrains the weights incident to each
hidden unit to have the norm between a lower bound and an upper bound.
You can implement your own constraint functions in R. A custom
constraint is an R function that takes weights (w
) as input
and returns modified weights. Note that keras backend()
tensor
functions (e.g. k_greater_equal()
) should be used in the
implementation of custom constraints. For example:
nonneg_constraint <- function(w) { w * k_cast(k_greater_equal(w, 0), k_floatx()) } layer_dense(units = 32, input_shape = c(784), kernel_constraint = nonneg_constraint)
Note that models which use custom constraints cannot be serialized using
save_model_hdf5()
. Rather, the weights of the model should be saved
and restored using save_model_weights_hdf5()
.