'GAN with tensorflow: model syntax is not clear
I am very new with tensorfflow and am trying to implement the example from the documentation:
def make_generator_model():
model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Reshape((7, 7, 256)))
assert model.output_shape == (None, 7, 7, 256) # Note: None is the batch size
model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
assert model.output_shape == (None, 7, 7, 128)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
assert model.output_shape == (None, 14, 14, 64)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
assert model.output_shape == (None, 28, 28, 1)
return model
generator = make_generator_model()
noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False)
I don't understand the syntax that consist in creating the generator with generator = make_generator_model() and then creating an image with generated_image = generator(noise, training=False). How come the generator doesnt use .fit() in that case?
Solution 1:[1]
generated_image = generator(noise, training=False) is just using the generator to generate a new image on the output from noise using feedforward on the generator network. training=False is used to tell the network that is not a training and some layers can act differently.
BatchNormalization layers act differently when on training or when inferencing: https://keras.io/api/layers/normalization_layers/batch_normalization/
Basically, in training, the input of the layer is used to define mean and variance of the data to update the layer, when inferencing, the data forwarded is not used to update, the layer normalize the data using only the mean and variance calculated when training=True
generated_image = generator.predict(noise) should have the same effect as generated_image = generator(noise, training=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivo Tebexreni |
