3

I was following the tutorial in tensorflow-2.0. When defining the generator, there were no variables given, but when calling the function, there are two variables are given.

def 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 = generator_model()
noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False) 

This is an official tutorial in tensorflow site.

qinlong
  • 731
  • 8
  • 19

1 Answers1

4

def generator_model() creates and returns model object. And then you can give data into the generator object to generate an image. There is no contradiction. def generator_model() only creates the generator object which will be used later.

As you can see here https://www.tensorflow.org/api_docs/python/tf/keras/models/Sequential#call

tf.keras.Sequential() object has __call__ function which means you can call the instance.(Python __call__ special method practical example) And it just wraps another call function as stated.

srknzl
  • 776
  • 5
  • 13