Tensorflow2.0入门教程4:顺序式模型和函数式模型搭建神经网络

tf.kears

Keras 是一个广为流行的高级神经网络 API,简单、快速而不失灵活性,现已得到 TensorFlow 的官方内置和全面支持。

两个重要概念:模型(Model)和层(Layer)

层将各种计算流程和变量进行了封装(例如基本的全连接层,CNN 的卷积层、池化层等),而模型则将各种层进行组织和连接,并封装成一个整体,描述了如何将输入数据通过各种层以及运算而得到输出。

import tensorflow as tf

一、神经网络层(Layer)API: tf.keras.layers

层将各种计算流程和变量进行了封装,包含了神经网络层全连接层,CNN的卷积层、池化层等。

1.Dense

全连接层

Arguments:

  • units: Positive integer, dimensionality of the output space.
  • activation: Activation function to use. If you don’t specify anything, no activation is applied (ie. “linear” activation: a(x) = x).
  • use_bias: Boolean, whether the layer uses a bias vector.
  • kernel_initializer: Initializer for the kernel weights matrix.
  • bias_initializer: Initializer for the bias vector.
  • kernel_regularizer: Regularizer function applied to the kernel weights matrix.
  • bias_regularizer: Regularizer function applied to the bias vector.
  • activity_regularizer: Regularizer function applied to the output of the layer (its “activation”)…
  • kernel_constraint: Constraint function applied to the kernel weights matrix.
  • bias_constraint: Constraint function applied to the bias vector.

Input shape:

  • N-D tensor with shape: (batch_size, …, input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim).

Output shape:

  • N-D tensor with shape: (batch_size, …, units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, units).

activation激活函数

  • sigmoid
  • softmax
  • relu
tf.keras.activations.sigmoid(x)
tf.keras.activations.softmax(x)
tf.keras.activations.relu(x)
tf.keras.layers.Dense(units=100,input_shape(2,), activation=tf.nn.relu)
tf.keras.layers.Dense(units=100, activation=tf.nn.relu)
tf.keras.layers.Dense(units=100, activation="sigmoid")
2.Conv1D、Conv2D

卷积层

tf.keras.layers.Conv2D()
tf.keras.layers.Conv1D()
3.RNN、LSTM、GRU

循环神经网络层

tf.keras.layers.SimpleRNN()
tf.keras.layers.LSTM()
tf.keras.layers.GRU()

二、搭建模型(Model)方法

  • 顺序式模型:通过Sequential类API;
  • 函数式模型:通过Model类API;

一般步骤:

  • 1.构建Sequential类Model对象
  • 2.堆叠神经网络层(Layer)
  • 3.Model类compile方法定义训练参数(优化方法、损失函数、评估方法)
  • 4.Model类fit方法训练模型
  • 5.Model类evaluate评估模型
  • 6.Model类predict模型预测
1 顺序式模型:通过Sequential类建立模型

在这里插入图片描述

顺序式模型的编程特点:

  • Layer提供input与output属性;
  • Sequential类通过Layer的input与output属性来维护层之间的关系,构建网络模型;
  • 第一个Layer必须是InputLayer或者Input函数构建的张量;

将神经网络层按按特定顺序叠加起来

  • add方法构建模型
  • 提供一个层列表构建模型
model = tf.keras.Sequential()
1.1 add方法构建模型
model.add(tf.keras.layers.Dense(100,input_shape=(2,),activation="relu"))
model.add(tf.keras.layers.Dense(10,activation="softmax"))

查看模型结构

model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 100)               300       
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1010      
=================================================================
Total params: 1,310
Trainable params: 1,310
Non-trainable params: 0
_________________________________________________________________
1.2 通过向 tf.keras.Sequential() 提供一个层的列表,Keras将它们自动首尾相连,就能快速地建立一个 tf.keras.Model 模型并返回,形成模型呢。
model = tf.keras.Sequential([
    tf.keras.layers.Dense(100,input_shape=(2,),activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              (None, 100)               300       
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1010      
=================================================================
Total params: 1,310
Trainable params: 1,310
Non-trainable params: 0
_________________________________________________________________
2 函数式模型:通过Model类API

在这里插入图片描述

顺序式模型这种层叠结构并不能表示任意的神经网络结构。为此,Keras 提供了 Functional API,帮助我们建立更为复杂的模型,例如多输入 / 输出或存在参数共享的模型。其使用方法是将层作为可调用的对象并返回张量,并将输入向量和输出向量提供给 tf.keras.Model 的 inputs 和 outputs 参数,示例如下:

inputs = tf.keras.Input(shape=(2,))
x = tf.keras.layers.Dense(units=100, activation=tf.nn.relu)(inputs)
x = tf.keras.layers.Dense(units=10)(x)
outputs = tf.keras.layers.Softmax()(x)
model = tf.keras.Model(inputs=inputs, outputs=x)
model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 2)]               0         
_________________________________________________________________
dense_6 (Dense)              (None, 100)               300       
_________________________________________________________________
dense_7 (Dense)              (None, 10)                1010      
=================================================================
Total params: 1,310
Trainable params: 1,310
Non-trainable params: 0
_________________________________________________________________

三、使用 Keras Model 的 compile、fit 、evaluate、predict 方法训练、评估模型、模型预测

3.1 当模型建立完成后,通过tf.keras.Model的compile方法配置训练过程:

tf.keras.Model.compile 接受 3 个重要的参数:

  • oplimizer :优化器,可从 tf.keras.optimizers 中选择;
  • loss :损失函数,可从 tf.keras.losses 中选择;
  • metrics :评估指标,可从 tf.keras.metrics 中选择。

categorical_crossentropy和sparse_categorical_crossentropy的区别:

  • categorical_crossentropy : one-hot编码:[0, 0, 1], [1, 0, 0], [0, 1, 0]
  • sparse_categorical_crossentropy:数字编码:2, 0, 1
model.compile(
        optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
        loss=tf.keras.losses.sparse_categorical_crossentropy,
#          loss = tf.keras.losses.SparseCategoricalCrossentropy(),
        metrics=[tf.keras.metrics.sparse_categorical_accuracy]
    )

或者

model.compile(optimizer = "adam",
              loss = "sparse_categorical_crossentropy",
              metrics=["sparse_categorical_accuracy"])
3.2 接下来,可以使用 tf.keras.Model 的 fit 方法训练模型:

tf.keras.Model.fit 接受 6 个重要的参数:

  • x :训练数据;
  • y :目标数据(数据标签);
  • epochs :将训练数据迭代多少遍;
  • batch_size :批次的大小;
  • validation_data :验证数据,可用于在训练过程中监控模型的性能。
  • verbose:日志显示,0为不在标准输出流输出日志信息,1为输出进度条记录,2为每个epoch输出一行记录
model.fit(x=train_data,y=train_label, epochs=num_epochs, batch_size=batch_size)
3.3 使用 tf.keras.Model.evaluate 评估训练效果,提供测试数据及标签即可:
model.evaluate(x=test_data, y=test_label,epochs=num_epochs)
3.4 使用 tf.keras.Model.predict 评估训练效果,提供预测数据即可:
model.predict(x=pred_data)

示例:TensorFlow 下的线性回归

x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
y = tf.constant([5.0,6.0,7.0,8.0,9.0,10.0])
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________
# 定义训练参数
model.compile(
    optimizer=tf.keras.optimizers.SGD(learning_rate=0.05),     # 指定优化器
#     loss=tf.keras.losses.MSE,   # 指定损失函数
    loss = 'mse'
)

查看模型变量值

model.variables  # 模型所有的变量
# model.layers[0].variables  模型第一层变量值
[<tf.Variable 'dense_1/kernel:0' shape=(1, 1) dtype=float32, numpy=array([[1.4437822]], dtype=float32)>,
 <tf.Variable 'dense_1/bias:0' shape=(1,) dtype=float32, numpy=array([0.], dtype=float32)>]
model.fit(x,y, epochs=10,verbose=2)
Train on 6 samples
Epoch 1/10
6/6 - 0s - loss: 6.5611
Epoch 2/10
6/6 - 0s - loss: 4.1146
Epoch 3/10
6/6 - 0s - loss: 3.1746
Epoch 4/10
6/6 - 0s - loss: 2.7765
Epoch 5/10
6/6 - 0s - loss: 2.5747
Epoch 6/10
6/6 - 0s - loss: 2.4452
Epoch 7/10
6/6 - 0s - loss: 2.3438
Epoch 8/10
6/6 - 0s - loss: 2.2544
Epoch 9/10
6/6 - 0s - loss: 2.1712
Epoch 10/10
6/6 - 0s - loss: 2.0921

<tensorflow.python.keras.callbacks.History at 0x2740bfe9358>
model.evaluate(x=x, y=y,verbose=0)
2.0162193775177
model.predict(x)
array([[ 2.5158374],
       [ 4.269158 ],
       [ 6.0224786],
       [ 7.7757993],
       [ 9.52912  ],
       [11.282441 ]], dtype=float32)
  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值