Munichong
Munichong

Reputation: 4041

Define a positive variable in TensorFlow

Using Tensorflow, I am trying to optimize the shape parameter of Weillbull distribution. The shape parameter should be always more than zero.

What is a nice way to define a positive variable in TensorFlow?

Upvotes: 2

Views: 1066

Answers (1)

xdurch0
xdurch0

Reputation: 10474

I see two ways of doing this:

  1. Assuming you have the parameter in a tf.Variable, you can use the constraint parameter (get_variable has it as well). This needs to be a function that basically projects the variable into the allowed space. E.g. you could pass tf.nn.relu here, which would map any negative values to 0 should they occur.
  2. Probably more elegant: Use an unconstrained variable, but map it to a positive value via a suitable function such as tf.exp. E.g. if your variable is x, you pass tf.exp(x) as the parameter to the Weibull distribution. Then x represents the log of the parameter.

Upvotes: 1

Related Questions