Introduction to modules, layers, and models

  • Model: To do machine learning in TensorFlow, you are likely to need to define, save, and restore a model.

    A model is, abstractly:

    • A function that computes something on tensors (a forward pass)
    • Some variables that can be updated in response to training

      In this guide, you will go below the surface of Keras to see how TensorFlow models are defined.

      This looks at how TensorFlow collects variables and models, as well as how they are saved and restored.
    • **Most models are made of layers.
    • Layers are functions with a known mathematical structure that can be reused and have trainable variables.
    • In TensorFlow, most high-level implementations of layers and models, are built on the same foundational class: tf.Module.
    • Modules and, by extension, layers are deep-learning terminology for "objects": they have internal state, and methods that use that state.
    • Note:

      tf.Module is the base class for both tf.keras.layers.Layer and tf.keras.Model,

      so everything you come across here also applies in Keras.

      For historical compatibility reasons Keras layers do not collect variables from modules,

      so your models should use only modules or only Keras layers.

      However, the methods shown below for inspecting variables are the same in either case.
    • By subclassing tf.Module, any tf.Variable or tf.Module instances assigned to this object's properties are automatically collected.

      This allows you to save and load variables, and also create collections of tf.Modules.
  • TensorFlow Modules

    Building Modules

    Here's an example of a very simple tf.Module that operates on a scalar tensor:

class SimpleModule(tf.Module):
def __init__(self, name=None):
super().__init__(name=name)
self.a_variable = tf.Variable(5.0, name="train_me")
self.non_trainable_variable = tf.Variable(5.0, trainable=False, name="do_not_train_me")
def __call__(self, x):
return self.a_variable * x + self.non_trainable_variable simple_module = SimpleModule(name="simple")
simple_module(tf.constant(5.0))

There is nothing special about call except to act like a Python callable;

you can invoke your models with whatever functions you wish.

You can set the trainability of variables on and off for any reason, including freezing layers and variables during fine-tuning.

This is an example of a two-layer linear layer model made out of modules.

# First a dense (linear) layer:
class Dense(tf.Module):
def __init__(self, in_features, out_features, name=None):
super().__init__(name=name)
self.w = tf.Variable(
tf.random.normal([in_features, out_features]), name='w')
self.b = tf.Variable(tf.zeros([out_features]), name='b')
def __call__(self, x):
y = tf.matmul(x, self.w) + self.b
return tf.nn.relu(y) # And then the complete model, which makes two layer instances and applies them:
class SequentialModule(tf.Module):
def __init__(self, name=None):
super().__init__(name=name) self.dense_1 = Dense(in_features=3, out_features=3)
self.dense_2 = Dense(in_features=3, out_features=2) def __call__(self, x):
x = self.dense_1(x)
return self.dense_2(x) # You have made a model!
my_model = SequentialModule(name="the_model") # Call it, with random results
print("Model results:", my_model(tf.constant([[2.0, 2.0, 2.0]])))

SciTech-BigDataAIML-Tensorflow-Introduction to modules, layers, and models的更多相关文章

  1. 吴恩达课后习题第二课第三周:TensorFlow Introduction

    目录 第二课第三周:TensorFlow Introduction Introduction to TensorFlow 1 - Packages 1.1 - Checking TensorFlow ...

  2. [TensorFlow] Introduction to TensorFlow Datasets and Estimators

    Datasets and Estimators are two key TensorFlow features you should use: Datasets: The best practice ...

  3. tensorflow 一维卷积 tf.layers.conv1()使用

    在自然语言处理中,主要使用一维的卷积. API tf.layers.conv1d( inputs, filters, kernel_size, strides=1, padding='valid', ...

  4. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

  5. Introduction to TensorFlow

    Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...

  6. tensorflow 学习日志

    Windows安装anaconda 和 TensorFlow anaconda : https://zhuanlan.zhihu.com/p/25198543        anaconda 使用与说 ...

  7. TensorFlow 中文资源全集,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  8. Awesome TensorFlow

    Awesome TensorFlow  A curated list of awesome TensorFlow experiments, libraries, and projects. Inspi ...

  9. TensorFlow 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  10. AI - TensorFlow - 示例03:基本回归

    基本回归 回归(Regression):https://www.tensorflow.org/tutorials/keras/basic_regression 主要步骤:数据部分 获取数据(Get t ...

随机推荐

  1. Hadoop和Spark大数据挖掘与实战

    1.概述 本节将系统讲解大数据分析的完整流程,包括数据采集.预处理.存储管理.分析挖掘与结果可视化等核心环节.与此同时,我们还将对主流数据分析工具进行横向对比,帮助读者根据实际需求选用最合适的工具,提 ...

  2. PC端自动化测试实战教程-3-pywinauto 启动PC端应用程序 - 下篇(详细教程)

    1.简介 经过上一篇的学习.介绍和了解,pywinauto的强大,不言而喻吧!宏哥讲解和分享的是电脑自带和安装的应用程序.有些小伙伴或者童鞋们已经迫不及待地私信宏哥,如果在电脑中这个应用程序已经启用了 ...

  3. windows 隐藏桌面了解此图片

    1. 桌面上有了解此图片图标无法删除 这是因为在windows背景设置中选择了Window聚焦. 如果想关闭可以选择其他选项.如果不想关闭Window聚焦还想隐藏桌面了解此图片图标,可以参考下面设置. ...

  4. 漏洞预警 | Ivanti Connect Secure栈溢出漏洞

    0x00 漏洞编号 CVE-2025-0282 0x01 危险等级 高危 0x02 漏洞概述 Ivanti Connect Secure是一款远程访问和零信任安全解决方案,它提供了SSL VPN功能, ...

  5. Nacos源码—3.Nacos集群高可用分析

    大纲 1.Nacos集群的几个问题 2.单节点对服务进行心跳健康检查和同步检查结果 3.集群新增服务实例时如何同步给其他节点 4.集群节点的健康状态变动时的数据同步 5.集群新增节点时如何同步已有服务 ...

  6. vue中 <el-table-column>回显转成百分比【数字转为百分比】

    一.方案1[不保留小数] 这里直接乘以100然后加入百分号既可 <el-table-column prop="refundRate15" label="15天退款率 ...

  7. 正点原子ALPHA开发板使用4.3寸触摸屏LCD驱动实验显示不正常

    显示问题 裸机开发时,驱动教程的PDF里给了4.3寸LCD屏幕的设置参数.如下图所示: 但是按照这个设置,编写设备树dts文件,下载到开发板里,却出现了显示异常,具体来说就是帧率不对,图和字都是歪斜的 ...

  8. Java Solon v3.3.0 发布(国产优秀应用开发基座)

    Solon 框架! Solon 是新一代,Java 企业级应用开发框架.从零开始构建(No Java-EE),有灵活的接口规范与开放生态.采用商用友好的 Apache 2.0 开源协议,是" ...

  9. uni-app小程序登录后…

    前情 最近新接了一个全新项目,是类似商城的小程序项目,我负责从0开始搭建小程序,我选用的技术栈是uni-app技术栈,其中就有一个用户登录功能,小程序部分页面是需要登录才可以查看的,对于未登录的用户需 ...

  10. 面试题:String,StringBuilder,StringBuffer三者的区别

    摘要:总结Java中的String,StringBuilder,StringBuffer三者的区别和联系,介绍后两者的扩容机制.   Java中的String,StringBuilder,String ...