Feedforward neural networks & `tf.keras`

  • Interlude 1: Handling devices with TensorFlow 用TF管理CPU\GPU
  • Downloading data and some preprocessing 下载和处理数据
  • Interlude 2: Iterators and generators in Python 迭代器和生成器简介
  • Loading the data in TensorFlow with `tf.data` 用TF的tf.data加载数据
  • Defining the model using layers 用layers类定义模型
  • Losses and metrics in `tf.keras` 损失和矩阵
  • Interlude 3: merging multiple lists with `zip` 简介zip的使用
  • The main training loop 主要的训练训练
  • Model training with `compile` and `fit` 还可以用fit训练模型

写在前面:

该代码详细介绍了如何实现前向反馈神经网络,训练一个神经网络的主要代码结构为:

  • 下载和加载数据 → \rightarrow → 用TF的tf.data加载数据 → \rightarrow → 用layers类定义模型 → \rightarrow → 定义损失和矩阵 → \rightarrow → 训练模型

注意:本文使用两种方式训练模型

  • 一种是根据神经网络的算法,计算模型梯度,损失,更新参数,计算准确率
  • 一种是直接用fit计算,简单快捷
#导入TF,然后查看其版本,我用的是 2.3.1 版本
import tensorflow as tf
print(tf.__version__)

Interlude 1: Handling devices with TensorFlow 用TF管理CPU\GPU

#查看可用的设备
# "XLA_CPU" and "XLA_GPU" refers to using the devices with the new XLA accelerator: https://www.tensorflow.org/xla
print(tf.config.list_logical_devices())#我们可以通过检查新创建的张量来检查目前使用的设备
# We can check which device is currently used by inspecting a newly created tensor
x = tf.random.normal((3, 1))
print(x.device)#我们也可以为操作的执行指定一个具体的设备
# We can also specify a certain device placement for an operation:
with tf.device('CPU:0'):x= tf.random.normal((3, 1))print(x.device)

Downloading data and some preprocessing 下载和处理数据

我们用一个简单的从数字特征诊断驱动器的数据集

点击此处获取该数据集的更多信息

下载地址:请点击此处

import pandas as pd
sensorless = pd.read_csv('Sensorless_drive_diagnosis.txt', header=None, sep=' ')#前48列是数字特征,最后一列是类标签
# The first 48 columns are the numerical features, while the last column is the class (1, ..., 11)
sensorless.head(5)#注意:X必须是数字(float32),输出为整型(int64)
# Note all the preprocessing: X must be numeric (float32), while the output are integers (int64).
# We also ensure that the output is shaped as (n, 1), and that the index for the class starts from 0.
X = sensorless.values[:, 0:-1].astype('float32')
y = sensorless.values[:, -1:].astype('int64') - 1print(X.shape)
print(y.shape)#各类的数据很平均
# Classes are perfectly balanced (as per the dataset description, check the link above)
import matplotlib.pyplot as plt
_ = plt.hist(y, bins=11, rwidth=0.9)#train_test_split用于分割数据集,默认比例为25%为测试集,75%为训练集from sklearn import model_selection
X_tr, X_tst, y_tr, y_tst = model_selection.train_test_split(X, y, stratify=y)

Interlude 2: Iterators and generators in Python 迭代器和生成器简介

#迭代器可以在for循环里面使用
# An iterator is any object that can be used inside a for-loop, like range
for i in range(4):_#迭代器可以动态的生成数据
# Iterators can be used to generate data on-the-fly, as it is being consumed by the for-loop.
range(4)#一种简单的方法是用yeild生成器
# To build an iterator, you need to construct a specific class implementing __iter__ and __next (https://wiki.python.org/moin/Iterator).
# A simpler way is to use generators, exploiting the keywork yield, as below.
def gen_custom_numbers():own_list = [3.0, 2.4, -12, 8]for n in own_list:yield n# Note: gen_custom_numbers() builds the iterator, which is then consumed by the for-loop.
for n in gen_custom_numbers():print(n)

Loading the data in TensorFlow with tf.data 用TF的tf.data加载数据

Before going into this section, it is a good idea to check the basic tf.data guide: https://www.tensorflow.org/guide/data.
The tf.data.Dataset allows to easily create iterable from your data.

#加载数据到 tf.data.Dataset对象,这样使用的时候可以将数据和标签一块使用
# Load the data inside a tf.data.Dataset object.
# Note: from_tensor_slices specifies that each row of the matrices is a single element of the dataset.
# Doing tf.data.Dataset.from_tensors((X_tr, y_tr)) would create a dataset with *a single* element (containing the two tensors).
train_dataset = tf.data.Dataset.from_tensor_slices((X_tr, y_tr))#按32个数据为一个batch,打乱数据
# We can create pipelines by concatenating operations on the data. It is important to understand
# that the operations are not run here, but only defined. Execution happens when the iterator is consumed.
train_dataset = train_dataset.shuffle(1000).batch(32)for data in train_dataset:print(data[0].shape)break# We can apply custom functions to our dataset. In this case, we need a simple function
# to only take the input elements for the Normalization layer.
def take_first_element(xb, yb):return xbfrom tensorflow.keras.layers.experimental.preprocessing import Normalization
normalizer = Normalization()
# Note: we are using a tf.data.Dataset here, meaning that the adapt function will work on mini-batches.
normalizer.adapt(train_dataset.map(take_first_element))

Defining the model using layers 用layers类定义模型

from tensorflow.keras import layers#单个的全连接层相当于一个线性层: w * x + b
# A single Dense layer is equivalent to a linear layer (w@x + b), possibly with an activation function.
model = layers.Dense(11)#当我们第一次运行“层”时,可以创建个内部变量
# When we run the layer for the first time, we create the internal variables.
print(model(data[0]).shape)#Sequential 可以让我们建立一个连续的模型,即把连续的层堆叠到一起
# There are multiple ways of building models from layers. Sequential is the easiest one.
from tensorflow.keras import Sequential#在模型的开头可以加入 layers.Input((48, )),便于确定实际输入的数据shape和你想要的shape一致
# For TF 2.2.0, you can add layers.Input((48,)) at the beginning to ensure that the shapes are correctly computed.
# We are including the preprocessing layer as part of the model architecture.
model = Sequential(layers=[normalizer,# layers.Dense(50, activation='relu'),layers.Dense(50, activation='relu'),layers.Dense(11, activation='softmax')
])print(model(data[0]).shape)#可以显示所有参数,包括不训练的参数
# These are *all* the parameters, including parameters that are not trained.
model.count_params()#可以显示整个模型结构和各种参数
# The Normalization has a series of internal variables that are not trained.
model.summary()print(len(model.trainable_variables))

Losses and metrics in tf.keras 损失和矩阵

from tensorflow.keras import losses
#损失的两种实现方法:#方法1,用函数的版本
# Approach 1: functional version (note the sparse version, because our targets are defined as indexes and not as one-hot vectors).
y_pred = model(data[0])
tf.reduce_mean(losses.sparse_categorical_crossentropy(data[1], y_pred))#方法2,用面向对象的版本
# Approach 2: object-oriented version.
cross_entropy = losses.SparseCategoricalCrossentropy()
cross_entropy(data[1], y_pred)#优化器
from tensorflow.keras import optimizers
sgd = optimizers.SGD(learning_rate=1e-3)

Interlude 3: merging multiple lists with zip 简介zip的使用

a = [1.0, 2.0, 3.0]
b = ['e', 'b', 'f']
for el in zip(a, b):print(el)

The main training loop 主要的训练训练

其实这种方法,感觉不常用,但是因为他需要自己编写对模型训练的每个过程,所以对于理解神经网络的内部原理会非常有好处!

#计算模型的斜率,损失,用sgd更新参数
# The tf.function will compile the function at the first execution, in order to considerably
# speed-up training: https://www.tensorflow.org/api_docs/python/tf/function.
# Carefully read the guide, as the compiled code has a number of important limitations.
# For example: try to return ce.numpy() instead of ce, with and without compilation.
# Can you understand why the former is not working?
@tf.function
def train_step(batch):xb, yb = batchwith tf.GradientTape() as tape:# Get the predictions of the modely_predicted = model(xb)# Compute the average loss of the predictionsce = cross_entropy(yb, y_predicted)# Get the gradients of the parametersgrads = tape.gradient(ce, model.trainable_variables)# Update the parameters using gradient descentsgd.apply_gradients(zip(grads, model.trainable_variables))return ce#加载测试数据,方法同上面训练集的操作一样,把数据和标签放一起
# Load the test part of the dataset. In practice, we would use a separate validation
# set here. Note that we are not shuffling the dataset, as this is not needed.
test_dataset = tf.data.Dataset.from_tensor_slices((X_tst, y_tst)).batch(32)print(y_pred[0])#将各类的概率转化为具体的预测类别
# To go from probabilities to classes, we take the argmax of the predictions.
tf.argmax(y_pred, axis=1)#计算准确率,很复杂!哈哈哈
# Computing the accuracy is strangely complex!
print(tf.reduce_mean(tf.cast(tf.argmax(y_pred, axis=1) == data[1][:, 0], tf.float32)))from tensorflow.keras import metrics#定义准确率矩阵
# Using metrics is generally simpler. Metrics are built to process multiple batches,
# hence the update_state function.
acc = metrics.SparseCategoricalAccuracy()
acc.update_state(data[1], y_pred)
print(acc.result())ce_history = []
for epoch in range(10):# Compute the accuracy for this epochacc = metrics.SparseCategoricalAccuracy()for batch in test_dataset:xb, yb = batchy_pred = model(xb)acc.update_state(yb, y_pred)print(f'Accuracy at epoch {epoch} is {acc.result().numpy()}')# Perform one epoch of trainingfor batch in train_dataset:ce = train_step(batch)ce_history.append(ce.numpy())import matplotlib.pyplot as plt
plt.plot(ce_history)
plt.plot(pd.Series(ce_history).ewm(halflife=15).mean(), 'r') # A smoothed version of the curve is easier to interpret.

Model training with compile and fit 还可以用fit训练模型

fit可说是tf.keras最简单且常用的训练方法了

#编译模型,只有编译后的模型才可以训练
# Compile writes all the previous training code for us!
model.compile(optimizer=sgd,loss=cross_entropy,metrics=[acc])model.fit(train_dataset, epochs=5, validation_data=test_dataset)

[NN]前向神经网络的tf.keras详细实现教学相关推荐

  1. 深度学习框架 TensorFlow:张量、自动求导机制、tf.keras模块(Model、layers、losses、optimizer、metrics)、多层感知机(即多层全连接神经网络 MLP)

    日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) 安装 TensorFlow2.CUDA10.cuDNN7.6. ...

  2. 使用估算器、tf.keras 和 tf.data 进行多 GPU 训练

    文 / Zalando Research 研究科学家 Kashif Rasul 来源 | TensorFlow 公众号 与大多数 AI 研究部门一样,Zalando Research 也意识到了对创意 ...

  3. 机器学习(七)——tf.keras搭建神经网络固定模式

    一.总纲(一般按照下面6步搭建) import--导入相关模块 train,test--指定训练集与测试集 model = tf.keras.models.Sequential--在Sequentia ...

  4. 深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-多层感知器(神经网络)与激活函数概述-04

    多层感知器 计算输入特征得加权和,然后使用一个函数激活(或传递函数)计算输出. 单个神经元 多个神经元 单层神经元缺陷 多层感知器 多层感知器 激活函数 relu:曲线如下图,假如过来的函数是x当x小 ...

  5. tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验

    这里写目录标题 场景:dropout和Dropout区别 问题描述: 结论: 深层次原因:dropout是底层API,Dropout是高层API 场景:dropout和Dropout区别 全网搜索tf ...

  6. 深度学习-函数-tf.nn.embedding_lookup 与tf.keras.layers.Embedding

    embedding函数用法 1. one_hot编码 1.1. 简单对比 1.2.优势分析: 1.3. 缺点分析: 1.4. 延伸思考 2. embedding的用途 2.1 embedding有两个 ...

  7. TensorFlow 学习(七) — 常用函数 api、tf.nn、tf.keras

    0. 四则运算 平方:tf.square(),开方:tf.sqrt() tf.add().tf.sub().tf.mul().tf.div().tf.mod().tf.abs().tf.neg() 1 ...

  8. tensorflow预定义经典卷积神经网络和数据集tf.keras.applications

    自己开发了一个股票软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html 1.1  tensorflow预定义经 ...

  9. Matlab:基于Matlab实现人工智能算法应用的简介(BP神经网络算法NN、SOFM神经网络)、案例应用(基于Matlab的GUI的方式创建/训练/预测神经网络)之详细攻略

    Matlab:基于Matlab实现人工智能算法应用的简介(BP神经网络算法NN.SOFM神经网络).案例应用(基于Matlab的GUI的方式创建/训练/预测神经网络)之详细攻略 目录

最新文章

  1. 计算机图画大赛作品六年级,小学学生电脑绘画比赛活动方案
  2. Arduino学习笔记35
  3. TF.js 识别图片中的物件
  4. 数据中心备受关注的三大方面
  5. vue 生产word_nodejs(officegen)+vue(axios)在客户端导出word文档
  6. Fliptile (二进制压缩)
  7. PowerShell实现批量收集SCVMM中虚拟机IP-续
  8. linux的基础知识——守护进程
  9. Python 爬取百度网盘所有热门分享文件
  10. 计算机应用专业毕业设计模板,计算机应用毕业论文模板范文
  11. Matlab学习笔记 kron函数
  12. Spring Transaction : TransactionInterceptor
  13. chrome浏览器怎么把整个网页截图保存
  14. 针式打印机windows打印乱码(并口模式与DOS打印测试)
  15. 求解多目标优化问题的邻域采样和代理辅助进化算法
  16. node.js - 收藏集
  17. 关于学计算机趣味段子,搞笑段子:路上一个女孩突然朝我走来问:你是不是学计算机的?...
  18. AVI音视频封装格式学习(五)——h265与PCM合成AVI文件
  19. C语言的语句与程序的基本结构
  20. linux磁盘分区方法 重新分区方法 新建分区方法

热门文章

  1. 4 书写规则
  2. 前端需要知道的CSS函数大全
  3. php引用复制,php引用和拷贝的区别
  4. JAVA计算机毕业设计房屋租赁管理系统Mybatis+系统+数据库+调试部署
  5. Python微信机器人之Python _ithcat 持续登陆错误 KeyError: ‘User’
  6. 目前最值得入手的蓝牙耳机有哪些?四款高性价比蓝牙耳机推荐
  7. 1190 -- 找x
  8. D - Petya and Array(树状数组,二分)
  9. 使用一键hidpi脚本二级logo变大,手动定制,支持Monterey
  10. linux 备份信息查看器,linux 全新的备份神器 Duplicity