Reputation: 784
I am trying to rescale the input in the model so that the output of my model is in the range [0,1]. But I don't want to do it with python code outside the model, instead, I want to do it inside the model. I have tried to use Keras Rescaling layer, but the results show values that are below 0, so I'm not sure what I'm doing wrong here..
class Generator(tf.Module):
def __init__(self):
super(Generator, self).__init__(name='Generator')
self.rescale = layers.experimental.preprocessing.Rescaling(1.0 / 255.0)
....
self.leakyrelu_3 = layers.LeakyReLU()
self.conv2dT_3 = layers.Conv2DTranspose(3, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')
@tf.function
def __call__(self):
inputs = tf.random.normal([1, 100,])
inputs = self.rescale(inputs)
....
inputs = self.leakyrelu_3(inputs)
output = self.conv2dT_3(inputs)
return output
gen = Generator()
print(gen().numpy())
array([[[[-4.39650057e-06, -1.98987391e-06, 6.26171777e-06],
[ 5.42304952e-06, -2.60572483e-06, 3.15579837e-06],
[ 6.24479253e-06, -6.08476057e-06, -2.21408982e-07],
...,
[ 1.29237583e-06, 1.32161574e-07, 7.94150219e-06],
[ 1.25768611e-06, -6.39899235e-06, 4.49939853e-06],
[-9.01997112e-07, -1.03935065e-06, 8.86256657e-07]],
[[ 1.23434333e-07, 4.14229225e-06, 2.49262189e-06],
[ 4.24094833e-06, -1.37856159e-05, 4.89129479e-06],
[ 1.17599066e-05, -5.83954352e-06, -5.19962259e-06],
...,
[-1.65802758e-05, -4.44178841e-06, 2.28360886e-06],
[ 2.48740253e-06, 2.20288484e-06, 6.34336720e-06],
[-6.80923858e-06, -7.12390374e-06, -1.00838906e-05]],
...,
Upvotes: 1
Views: 337
Reputation: 36714
You can use, as last layer (or replace your leaky relu activation):
tf.keras.layers.ReLU(max_value=1)
See the docs.
You can also use a sigmoid activation.
Upvotes: 1