深度学习编程笔记:Tensorflow2.1基础知识---搭建神经网络八股以及小案例实战

利用Tensorflow API tf.keras搭建网络八股(六步法)

六步法:

  1. 导入相关的模块,也就是 import
  2. 加载训练集和测试集,也就是加载train(x_train数据、y_train标签)、test(x_test数据、y_test标签)数据
  3. 前向传播(搭建神经网络结构,逐层描述每层网络),也就是model = tf.keras.models.Sequential
  4. 配置训练时所用的方法(也就是优化器,损失函数,评测指标的选择),也就是model.compile
  5. 进行数据的训练(告诉训练集和测试集的输入特征和标签,batch的值,以及迭代多少次数据集),也即是model.fit
  6. 利用summary()函数打印出网络的结构和参数统计

对上述用到的tf.keras模块中的函数进行进一步的介绍

  1. model = tf.keras.models.Sequential([网络结构]) #描述各层网络
    网络结构举例:
    1. 拉直层:tf.keras.layers.Flatten()
    2. 全连接层:tf.keras.layers.Dense(神经元个数,activation=“激活函数”,kernel_regularizer=“正则化函数”)
      activation可选的字符串:“relu”、“softmax”、“sigmoid”、“tanh”
      kernel_regularizer可选:tf.keras.regularizers.l1()、tf.keras.regularizers.l2()
    3. 卷积层:tf.keras.layers.Conv2D(filters = 卷积核个数,kernel_size = 卷积核尺寸,strides = 卷积步长,padding = “valid”or“same”)
    4. LSTM层:tf.keras.layers.LSTM()
  2. model.compile(optimizer=优化器,loss=损失函数,metrics=[“准确率”])
    1. Optimizer可选:
      i. ‘sgd’ or tf.keras.optimizers.SGD(lr=学习率,momentum=动量参数)
      ii. 'adagrad’or tf.keras.optimizers.Adagrad(lr=学习率)
      iii. 'adadelta’or tf.keras.optimizers.Adadelta(lr=学习率)
      iv. 'adam’or tf.keras.optimizers.Adam(lr=学习率,beta_1=0.9,beta_1=0.999)
    2. loss可选:
      i. ‘mse’ or tf.keras.losses.MeanSquaredError()
      ii. ‘sparse_categorical_crossentropy’ or tf.keras.losses.SparseCategoricalCrossentropy(from_logots=False)
    3. Metrics可选:
      i. ‘accuracy’:y_和y都是数值,如y_=[1] y=[1]
      ii. ‘categorical_accuracy’:y_和y都是独热码(概率分布),如y_=[0,1,0] y=[0.256,0.695,0.048]
      iii. ‘sparse_categorical_accuracy’:y_是数值,y是独热码(概率分布),y_=[1] y=[0.256,0.695,0.048]
  3. model.fit(训练集的输入特征,训练集的标签,batch_size=,epochs=,validation_data=(测试集的输入特征,测试集的标签),validation_split=从训练集划分多少比例给测试集,validation_freq=多少次epoch测试一次)
  4. model.summary() 打印出网络的结构和参数统计

案例

案例1:利用tf.keras实现鸢尾花分类

import tensorflow as tf
from sklearn import datasets
import numpy as np

x_train = datasets.load_iris().data
y_train = datasets.load_iris().target

np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(3,activation = 'softmax',kernel_regularizer = tf.keras.regularizers.l2())
])

model.compile(optimizer = tf.keras.optimizers.SGD(lr = 0.1),
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
             metrics = ['sparse_categorical_accuracy'])

model.fit(x_train,y_train,batch_size = 32,epochs = 500,validation_split = 0.2,validation_freq = 20)

model.summary()

将上述代码封装成class

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as np

x_train = datasets.load_iris().data
y_train = datasets.load_iris().target

np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)

class IrisModel(Model):
    def __init__(self):
        super(IrisModel,self).__init__()
        self.dl = Dense(3,activation = 'sigmoid',kernel_regularizer = tf.keras.regularizers.l2())
    def call(self,x):
        y = self.dl(x)
        return y
    
model = IrisModel()

model.compile(optimizer = tf.keras.optimizers.SGD(lr = 0.1),
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
             metrics = ['sparse_categorical_accuracy'])

model.fit(x_train,y_train,batch_size = 32,epochs = 500,validation_split = 0.2,validation_freq = 20)

model.summary()

案例2:利用tf.keras实现mnist手写数字识别

import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train / 255.0,x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation = 'relu'),
    tf.keras.layers.Dense(10,activation = 'softmax')
])

model.compile(optimizer = 'adam',
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
             metrics = ['sparse_categorical_accuracy'])

model.fit(x_train,y_train,batch_size = 32,epochs = 5,validation_data = (x_test,y_test),validation_freq=1)
model.summary()

将上述代码封装成class

import tensorflow as tf
from tensorflow.keras.layers import Dense,Flatten
from tensorflow.keras import Model

mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train / 255.0,x_test / 255.0

class MnistModel(Model):
    def __init__(self):
        super(MnistModel,self).__init__()
        self.flatten = Flatten()
        self.d1 = Dense(128,activation = 'relu')
        self.d2 = Dense(10,activation = 'softmax')
    def call(self,x):
        x = self.flatten(x)
        x = self.d1(x)
        y = self.d2(x)
        return y
    
model = MnistModel()

model.compile(optimizer = 'adam',
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
             metrics = ['sparse_categorical_accuracy'])

model.fit(x_train,y_train,batch_size = 32,epochs = 5,validation_data = (x_test,y_test),validation_freq=1)
model.summary()

你可能感兴趣的:(深度学习编程笔记,深度学习,神经网络,tensorflow,卷积神经网络,机器学习)