tensorflow2.0 的Layer 的 call build init方法

官方API

显然,这三个函数都是从tf.keras.layers.Layer处继承而来的。

_init_ 可以在其中进行所有与输入无关的初始化

build 知道输入张量的形状,并可以进行其余的初始化

call 可以在其中进行前向计算

官方API的例子:

class MyDenseLayer(tf.keras.layers.Layer):
  def __init__(self, num_outputs):
    super(MyDenseLayer, self).__init__()
    self.num_outputs = num_outputs

  def build(self, input_shape):
    self.kernel = self.add_weight("kernel",
                                  shape=[int(input_shape[-1]),
                                         self.num_outputs])

  def call(self, inputs):
    return tf.matmul(inputs, self.kernel)

layer = MyDenseLayer(10)

_ = layer(tf.zeros([10, 5]))

print([var.name for var in layer.trainable_variables])


输出: ['my_dense_layer/kernel:0']

从直观上理解,似乎__init__()和build()函数都在对Layer进行初始化,都初始化了一些成员函数,而call()函数则是在该layer被调用时执行。

根据_吟游诗人 的博客

简单翻译,就是说官方推荐凡是tf.keras.layers.Layer的派生类都要实现__init__(),build(), call()这三个方法

init():保存成员变量的设置

build():在call()函数第一次执行时会被调用一次,这时候可以知道输入数据的shape。
返回去看一看,果然是__init__()函数中只初始化了输出数据的shape,而输入数据的shape需要在build()函数中动态获取,这也解释了为什么在有__init__()函数时还需要使用build()函数

call(): call()函数把对象当做函数来使用,即当其被调用时会被执行。当call被第一次调用的时候,会先执行build()方法初始化变量,但后面再调用到call的时候,是不会再去执行build()方法初始化变量。

从上面的官方例子可以简单梳理脉络,但是对我来,发现程序正如我们前面提到把对象当做函数来使用

_ = layer(tf.zeros([10, 5]))

调用了call()

并没有调用 build方法 去源码 tensorflow.python.keras.layers.Layer.call方法中查看:

input_shapes = None
if all(hasattr(x, 'shape') for x in input_list):
	 input_shapes = nest.map_structure(lambda x: x.shape, inputs)
if not hasattr(self.build, '_is_default'):
	with tf_utils.maybe_init_scope(self):
		self.build(input_shape)

发现call方法中调用了build 并且提供参数 input_shape。

  • 为什么有些要重写build呢?

官方是这么写的:

the advantage of creating them in build is that it enables late variable creation based on the shape of the inputs the layer will operate on.

因为可以单独调用build生成输入权重信息,支持基于层将操作的输入的形状的后期变量创建。

即初始化时的操作可能需要自定义。

例:

来源自 DeepCtr的deepctr/layers/utils.py

    def build(self, input_shape):
        if self.use_bias:
            self.bias = self.add_weight(name='linear_bias',
                                        shape=(1,),
                                        initializer=tf.keras.initializers.Zeros(),
                                        trainable=True)
        if self.mode == 1:
            self.kernel = self.add_weight(
                'linear_kernel',
                shape=[int(input_shape[-1]), 1],
                initializer=tf.keras.initializers.glorot_normal(self.seed),
                regularizer=tf.keras.regularizers.l2(self.l2_reg),
                trainable=True)
        elif self.mode == 2:
            self.kernel = self.add_weight(
                'linear_kernel',
                shape=[int(input_shape[1][-1]), 1],
                initializer=tf.keras.initializers.glorot_normal(self.seed),
                regularizer=tf.keras.regularizers.l2(self.l2_reg),
                trainable=True)

        super(Linear, self).build(input_shape)  # Be sure to call this somewhere!



参考 时光碎了天 的博客 | beking00700 的博客

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
code! 此为源代码! 视频教程见: May 1, 2020 Machine Learning Projects with TensorFlow 2.0: Supercharge your Machine Learning skills with Tensorflow 2 English | MP4 | AVC 1920×1080 | AAC 48KHz 2ch | 4h 20m | 965 MB eLearning | Skill level: All Levels Build and train models for real-world machine learning projects using Tensorflow 2.0 TensorFlow is the world’s most widely adopted framework for Machine Learning and Deep Learning. TensorFlow 2.0 is a major milestone due to its inclusion of some major changes making TensorFlow easier to learn and use such as “Eager Execution”. It will support more platforms and languages, improved compatibility and remove deprecated APIs. This course will guide you to upgrade your skills in Machine Learning by practically applying them by building real-world Machine Learning projects. Each section should cover a specific project on a Machine Learning task and you will learn how to implement it into your system using TensorFlow 2. You will implement various Machine Learning techniques and algorithms using the TensorFlow 2 library. Each project will put your skills to test, help you understand and overcome the challenges you can face in a real-world scenario and provide some tips and tricks to help you become more efficient. Throughout the course, you will cover the new features of TensorFlow 2 such as Eager Execution. You will cover at least 3-4 projects. You will also cover some tasks such as Reinforcement Learning and Transfer Learning. By the end of the course, you will be confident to build your own Machine Learning Systems with TensorFlow 2 and will be able to add this valuable skill to your CV. Learn Strengthen your foundations to build TensorFlow 2.0 projects by exploring its new features Analyze the Titanic data set to obtain desired results with ease Implement and organize your Tensorflow projects in a professional manner Use Tensorboard to inspect various metrics and monitor your project’s performance Research and make the most of other people’s Kaggle solutions Use OpenAI Gym Environments for implementing state of the art reinforcement learning techniques using TF-Agents Apply the latest Transfer Learning techniques from Tensorflow + Table of Contents Regression Task Airbnb Prices in New York 1 Course Overview 2 Setting Up TensorFlow 2.0 3 Getting Started with TensorFlow 2.0 4 Analyzing the Airbnb Dataset and Making a Plan 5 Implementing a Simple Linear Regression Algorithm 6 Implementing a Multi Layer Perceptron (Artificial Neural Network) 7 Improving the Network with Better Activation Functions and Dropout 8 Adding More Metrics to Gain a Better Understanding 9 Putting It All Together in a Professional Way Classification Task Build Real World Apps – Who Will Win the Next UFC 10 Collecting Possible Kaggle Data 11 Analysis and Planning of the Dataset 12 Introduction to Google Colab and How It Benefits Us 13 Setting Up Training on Google Colab 14 Some Advanced Neural Network Approaches 15 Introducing a Deeper Network 16 Inspecting Metrics with TensorBoard 17 Inspecting the Existing Kaggle Solutions Natural Language Processing Task – How to Generate Our Own Text 18 Introduction to Natural Language Processing 19 NLP and the Importance of Data Preprocessing 20 A Simple Text Classifier 21 Text Generation Methods 22 Text Generation with a Recurrent Neural Network 23 Refinements with Federated Learning Reinforcement Learning Task – How to Become Best at Pacman 24 Introduction to Reinforcement Learning 25 OpenAI Gym Environments 26 The Pacman Gym Environment That We Are Going to Use 27 Reinforcement Learning Principles with TF-Agents 28 TF-Agents for Our Pacman Gym Environment 29 The Agents That We Are Going to Use 30 Selecting the Best Approaches and Real World Applications Transfer Learning Task – How to Build a Powerful Image Classifier 31 Introduction to Transfer Learning in TensorFlow 2 32 Picking a Kaggle Dataset to Work On 33 Picking a Base Model Suitable for Transfer Learning with Our Dataset 34 Implementing our Transfer Learning approach 35 How Well Are We Doing and Can We Do Better 36 Conclusions and Future Work

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值