tensorflow2学习笔记

第三章神经网络八股

3.1搭建神经网络sequential(上层输出就是下层输入的顺序网络结构)

使用tensorflow的API:tf.keras搭建网络六步法:
第一步:import相关模块。
第二步:train和test数据集。
第三步:model = tf.keras.models.Sequential()。
第四步:model.compile()配置训练方法,选择优化器,损失函数和评价指标。
第五步:model.fit()执行训练过程,告知训练集的输入和标签,batch大小和epoch迭代次数。
第六步:model.summary()打印网络结构,统计参数。
下面仔细介绍这些函数的使用方法:
model = tf.keras.models.Sequential ([ 网络结构 ]) #描述各层网络
拉直层: tf.keras.layers.Flatten( )
全连接层: tf.keras.layers.Dense(神经元个数, activation= "激活函数“ ,
kernel_regularizer=哪种正则化)
activation(字符串给出)可选: relu、 softmax、 sigmoid 、tanh
kernel_regularizer可选: tf.keras.regularizers.l1()、tf.keras.regularizers.l2()
卷积层: tf.keras.layers.Conv2D(filters = 卷积核个数, kernel_size = 卷积核尺寸,
strides = 卷积步长, padding = " valid" or "same")
LSTM层: tf.keras.layers.LSTM()

model.compile(optimizer = 优化器, loss = 损失函数
Optimizer可选:
metrics = [“准确率”] )
‘sgd’ or tf.keras.optimizers.SGD (lr=学习率,momentum=动量参数)
‘adagrad’ or tf.keras.optimizers.Adagrad (lr=学习率)
‘adadelta’ or tf.keras.optimizers.Adadelta (lr=学习率)
‘adam’ or tf.keras.optimizers.Adam (lr=学习率, beta_1=0.9, beta_2=0.999)
loss可选:
‘mse’ or tf.keras.losses.MeanSquaredError()
‘sparse_categorical_crossentropy’ or tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False
Metrics可选:
‘accuracy’:y_和y都是数值,如y_=[1] y=[1]
‘categorical_accuracy’ :y_和y都是独热码(概率分布),如y_=[0,1,0] y=[0.256,0.695,0.048]
‘sparse_categorical_accuracy’ :y_是数值,y是独热码(概率分布),如y_=[1] y=[0.256,0.695,0.048]

model.fit (训练集的输入特征, 训练集的标签, batch_size= , epochs= ,
validation_data=(测试集的输入特征,测试集的标签), validation_split=从训练集划分多少比例给测试集, validation_freq = 多少次epoch测试一次)

下面给出具体的代码实现:

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(optimezer=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()     # 第六步

3.2搭建神经网络class(跳连的非顺序网络结构)

除了第三步其余与3.1相同。
class MyModel(Model) model = MyModel

class MyModel(Model):
def init(self):
super(MyModel, self).init()
定义网络结构块
def call(self, x):
调用网络结构块,实现前向传播
return y
model = MyModel()
下面给出代码实现:

import tensorflow as tf
from sklearn import datasets
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
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.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())

    def call(self, x):
        y = self.d1(x)
        return y
    # 第三步


model = IrisModel()

model.compile(optimezer=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()  # 第六步

3.3mnist数据集

下面给出基于mnist数据集的sequential和class实现方法:
sequential:

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   # 因为原数据为1-255,进行归一化处理

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  # 因为原数据为1-255,进行归一化处理


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()

3.4使用fashion数据集进行练习

import tensorflow as tf

fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion.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()
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model

fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.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()

你可能感兴趣的:(tensorflow2学习笔记)