简体   繁体   English

在Tensorflow2.0文档的keras功能api中创建图层的语法

[英]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 我正在浏览tf2.0文档https://www.tensorflow.org/beta/tutorials/load_data/csv ,无法理解以下代码的一部分

    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? (x)在第二行的末尾是什么意思,它的作用是什么? is it a part of TensorFlow or it's available in python as well? 它是TensorFlow的一部分还是在python中可用?

(x) is just the invocation of the function returned by tf.keras.layers.Dense(units, activation='relu') passing x as first positional parameter. (x)只是对tf.keras.layers.Dense(units, activation='relu')返回的函数的调用,其中x作为第一个位置参数。

This is not something TensorFlow related, but pure Python. 这与TensorFlow无关,而是纯Python。 In fact, every keras layer (like Dense ) just defines a callable object (like a python function) that can, thus, be called. 实际上,每个keras层(如Dense )仅定义了一个可调用的对象(如python函数),因此可以对其进行调用。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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