简体   繁体   中英

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?

(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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM