How To Restrict Output Of A Neural Net To A Specific Range?
I'm using Keras for a regression task and want to restrict my output to a range (say between 1 and 10) Is there a way to ensure this?
Solution 1:
Write a custom activation function like this
# a simple custom activationfrom keras import backend as BK
defmapping_to_target_range( x, target_min=1, target_max=10) :
x02 = BK.tanh(x) + 1# x in range(0,2)
scale = ( target_max-target_min )/2.return x02 * scale + target_min
# create a simple modelfrom keras.layers import Input, Dense
from keras.models import Model
x = Input(shape=(1000,))
y = Dense(4, activation=mapping_to_target_range )(x)
model = Model(inputs=x, outputs=y)
# testingimport numpy as np
a = np.random.randn(10,1000)
b = model.predict(a)
print b.min(), b.max()
And you are expected to see the min
and max
values of b
are very close to 1
and 10
, respectively.
Solution 2:
Normalize your outputs so they are in the range 0, 1. Make sure your normalization function lets you transform them back later.
The sigmoid activation function always outputs between 0, 1. Just make sure your last layer has sigmoid activation to restrict your output into that range. Now you can take your outputs and transform them back into the range you wanted.
You could also look into writing your own activation function to transform your data.
Post a Comment for "How To Restrict Output Of A Neural Net To A Specific Range?"