SciTech-BigDataAIML-Tensorflow-Introduction to modules, layers, and models
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
modelsare made oflayers. Layersare 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, andmethodsthatuse that state. - Note:
tf.Module is the base class forbothtf.keras.layers.Layerandtf.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 onlymodulesor onlyKeras layers.
However, the methods shown below for inspecting variables are the same in either case. - By subclassing
tf.Module, anytf.Variableortf.Moduleinstances 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的更多相关文章
- 吴恩达课后习题第二课第三周:TensorFlow Introduction
目录 第二课第三周:TensorFlow Introduction Introduction to TensorFlow 1 - Packages 1.1 - Checking TensorFlow ...
- [TensorFlow] Introduction to TensorFlow Datasets and Estimators
Datasets and Estimators are two key TensorFlow features you should use: Datasets: The best practice ...
- tensorflow 一维卷积 tf.layers.conv1()使用
在自然语言处理中,主要使用一维的卷积. API tf.layers.conv1d( inputs, filters, kernel_size, strides=1, padding='valid', ...
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
- Introduction to TensorFlow
Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...
- tensorflow 学习日志
Windows安装anaconda 和 TensorFlow anaconda : https://zhuanlan.zhihu.com/p/25198543 anaconda 使用与说 ...
- TensorFlow 中文资源全集,官方网站,安装教程,入门教程,实战项目,学习路径。
Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...
- Awesome TensorFlow
Awesome TensorFlow A curated list of awesome TensorFlow experiments, libraries, and projects. Inspi ...
- TensorFlow 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。
Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...
- AI - TensorFlow - 示例03:基本回归
基本回归 回归(Regression):https://www.tensorflow.org/tutorials/keras/basic_regression 主要步骤:数据部分 获取数据(Get t ...
随机推荐
- 21.7K star!全流程研发项目管理神器,开源免费不限商用!
嗨,大家好,我是小华同学,关注我们获得"最新.最全.最优质"开源项目和高效工作学习方法 「禅道开源版」作为国内首个开源项目管理软件,已为100万+团队提供专业研发管理解决方案.从需 ...
- 『Plotly实战指南』--交互功能进阶篇
在数据可视化的世界中,交互性是提升用户体验和数据探索效率的关键.从简单的悬停提示到复杂的动态数据更新,交互功能让静态图表变得生动起来. 本文将介绍Plotly的高级交互功能,包括点击事件处理.动态数据 ...
- Sentinel——流控规则
目录 流控规则 QPS 设置流控规则 api设置流控规则 资源实体指定流控规则 并发线程数 Sentinel 隔离方案 流控模式-关联 流控模式-链路 控制效果 快速失败 Warm Up 排队等待 三 ...
- 典型相关分析 CCA
最近有小伙伴在问我一个数据分析的问题, 做毕设, 实证分析. 不知道改如何处理数据. 看了下设计的量表大致是这样的, 都是 5级的里克特量表, 大致分为两波, X, Y. 小伙伴认为就只有两个变量, ...
- MongoDB从入门到实战之Windows快速安装MongoDB
前言 本章节的主要内容是在 Windows 系统下快速安装 MongoDB 并使用 Navicat 工具快速连接. MongoDB从入门到实战之MongoDB简介 MongoDB从入门到实战之Mong ...
- RPC的通信流程
核心原理:能否画张图解释下RPC的通信流程? RPC的全称是Remote Procedure Call,即远程过程调用.RPC帮助我们屏蔽网络编程细节,实现调用远程方法跟调用本地(同一个项目中的方法) ...
- Razor路由相关
1.赋予参数 @page "{id}" @model RazorTest.Pages.TestBModel @{ } <p>TestBId is @Model.Test ...
- codeup之日期类
Description 编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作. Input 输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日 ...
- IPO——LeetCode⑫
//原题链接https://leetcode.com/problems/ipo/ 题目描述 Suppose LeetCode will start its IPO soon. In order to ...
- 计算机组成原理 L02 指令集体系结构(ISA)复习-1
计算机组成原理 L02 指令集体系结构(ISA)复习-1 复习-1 用作例题/课后题整理 复习-2 用作理论知识点整理 如何使用大常数 由于大数值范围:[-2^31, 2^31-1],而I型指令常数字 ...