JimmyFails
JimmyFails

Reputation: 123

Syntax for creating layers in keras functional api for tensorflow2.0 documentation

I was going through the tf2.0 documentation https://www.tensorflow.org/beta/tutorials/load_data/csv and could not understand a part of the following code

    for units in hidden_units:
      x = tf.keras.layers.Dense(units, activation='relu')(x)
    outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)

what does (x) mean at the end of 2nd line and what does it do? is it a part of TensorFlow or it's available in python as well?

Upvotes: 0

Views: 44

Answers (1)

nessuno
nessuno

Reputation: 27042

(x) is just the invocation of the function returned by tf.keras.layers.Dense(units, activation='relu') passing x as first positional parameter.

This is not something TensorFlow related, but pure Python. In fact, every keras layer (like Dense) just defines a callable object (like a python function) that can, thus, be called.

For instance, you can do something like:

class A:
    def __init__(self):
        self.a = 1

    def __call__(self, parameter):
        self.a = parameter
        print("function called. a set to ", self.a)

x = A() #x is a callable object because of the __call__ definition
# Thus you can call it:
x(19)

Upvotes: 1

Related Questions