数据
①张量
分为:零阶(数,s = 1);一阶(向量, s=[1,1,1,2]);二阶(矩阵,s=[[1,1,1],[1,2,3]]);三阶,...;n阶(); 几阶张量就有几个[ ]; 索引方式与c++相同;
②数据类型(列举几个)
tf.int 32/64 tf.float 32/64 tf.bool :True/False tf.string :"dsf"
③张量创建:
tf.constant(张量内容,dtpye=数据类型(可选))
import tensortflow as tf
a=tf.constant([1,2],dtype=tf.int64)
print(a) #打印a的内容,形状,数据类型
print(a.dtype)#打印a的数据类型
print(a.shape)#打印a的形状 (2,) --- ,的数量说明阶数,2说明了数据的大小
#将numpy的数据类型转换为Tensorflow数据类型
#tf.convert_to_tensor(数据名,dtype=数据类型(可选))
import tensorflow as tf
import numpy as np
a = np.arange(0,5)#[0,1,2,3,4]
b = tf.convert_to_tensor(a,dtype = tf.int64)
#创建全为0的张量:tf.zeros(维度)
#创建全为1的张量:tf.ones(维度)
#创建全为指定值的张量: tf.fill(维度,指定值)
#维度:一维:直接数字;多维:用[a,b,c,d,...]指定
a=tf.zeros(4) #[0,0,0,0]
b= tf.ones([2,3])#[[1,1,1],[1,1,1]]
c = tf.fill([2,2],3)#[[3,3],[3,3]]
#创建符合正态分布的随机数,默认均值为0,标准差为1:
#tf.random.normal(维度,mean= 均值,stddev=标准差)
#生成截断式正态分布的随机数(即随机数的范围(μ-2σ,μ+2σ)):#tf.random.truncated_normal(维度,mean=均值,stddev=标准差)
#生成均匀分布随机数:tf.random.uniform(维度,minval = 最小值,maxval = 最大值)
f = tf.random.normal([2,2])
常用函数
理解axis 对于二维张量或数组,可以使用axis为0,1来控制执行维度,axis=0,代表跨行,axis=1,代表跨列,若不指定axis则计算所有元素
a = tf.constant([[1,2,4],[2,3,2],dtype=tf.int64)
#强制转换数据类型:tf.cast(张量名,dtype = 数据类型)
b = tf.cast(a,dtype=tf.int32)
#计算张量维度上的最小值:tf.reduce_min(张量名)
print(tf.reduce_min(a));#计算所有元素的最小值1
#计算张量维度上的最大值:tf.reduce_max(张量名)
print(tf.reduce_max(a,axis=1))#[4,3]
#平均值计算
tf.reduce_mean(a)
#维度上的逻辑与
#tf.reduce_all(a)
#维度上的乘
#tf.reduce_prod(a)
#维度上求和
tf.reduce_sum(a)
#tf.Variable(初始值) 将变量标记为“可训练的”,被记录的变量会在反向传播中记录梯度信息,神经网络训练中,常用该函数标记待训练参数
w = tf.Variable(tf.random.noraml([2,2],mean=0,stddev=1))
#常见数学运算
#四则运算,前提是张量v1,v2维度相同
tf.add(v1,v2)#对应元素加
tf.subtract(v1,v2)#对应元素减
tf.multiply(v1,v2)#对应元素乘
tf.divide(v1,v2)#对应元素除
tf.square(v1)#平方
tf.pow(v2,n)#次方
tf.sqrt(v1)#开方
tf.matmul(v1,v2)#矩阵乘#相应维度满足条件
#tf.data.Dataset.from_tensor_slices((输入特征,标签)) 切分传入张量的第一维度,生成输入特征/标签对,构建数据集
#data = tf.data.Dataset.from_tensor_slices((输入特征,标签))---适用于Numpy,Tensor格式读入数据
#tf.GradientTape()
#with 结构记录计算过程,gradient求出张量梯度:
with tf.GradientTape() as tape:#with下面是若干计算过程
w = tf.Variable(tf.constant(3.0))
loss = tf.pow(w,2)
grad = tape.gradient(loss,w)#gradient(func,求导变量)
print(grad)#6.0
#enumerate(列表名) ----python内建函数,可以遍历每个元素(如列表,元祖,字符串),组合为:索引,元素,常用于for循环
seq = ['one','two','three']
for i,element in enumerate(seq):
print(i,element)
# 0 one
# 1 two
# 2 three
#tf.one_hot() --- 独热编码(one-hot encoding):在分类问题中,常使用独热码做标签,标记类别:1表示是,0表示非
#如现在有三种标签,对应独热码为(1,0,0),(0,1,0),(0,0,1)
classes=3#记录几种标签,或者说几分类
labels = tf.constant([1,0,2])
output = tf.one_hot(labels,classes)
print(output)
#[[0,1,0],[1,0,0],[0,0,1]]
#tf.nn.softmax(),经过softmax函数会符合概率分布
y = tf.constant([1.01,2.01,-0.66])
y_pro = tf.nn.softmax(y)
print(y_pro)
#[0.2560,0.6958,0.0482]
#assign_sub 即实现-=操作,调用assign_sub前,要先用Variable定义待更新参数为可训练
w = tf.Variable(4)
w.assign_sub(2)
print(w)
#2
#tf.argmax(张量名,axis = 操作轴)---可以返回张量指定维度最大值的索引;
import numpy as np
test = np.array([[1,2,3],[2,3,4],[5,3,2],[7,8,1]])
tf.argmax(test,axis=0)#[3,3,1]
tf.argmax(test,axis=1)#[2,2,0,1]
#tf.where(条件语句,真返回A,假返回B)
a = tf.constant([1,2,3,2,1])
b = tf.constant([0,1,2,3,4])
c = tf.where(tf.greater(a,b),a,b)#若a>b,则返回a对应位置元素,反之,返回b对应位置元素。
print(c) #[1,2,3,3,4]
#np.random.RandomState.rand(维度) ---返回一个[0,1)之间的随机数
import numpy as np
rdm = np.random.RandomState(seed =1) #seed=常数,则每次生成随机数相同
a = rdm.rand()#返回一个随机标量
b = rdm.rand(2,3)#返回维度为2行3列的随机数矩阵
#np.vstack(数组1,数组2) ---将两个数组按垂直方向叠加
import numpy as np
a =np.array([1,2,3])
b = np.array([4,5,6])
c = np.vstack((a,b))
print(c)
#[[1 2 3],[4 5 6]]
#np.mgrid[起始值:结束值:步长,起始值:结束值:步长,...] --其中[起始值,结束值)
#x.ravel() ----将x变成一维数组
#np.c_[数组1,数组2,...] ---使得返回值的间隔点配对
import numpy as np
x,y = np.mgrid[1:4:1,2:5:1]
grid = np.c_[x.ravel(),y.ravel()]
print(x)
print(y)
print(x.ravel())
print(y.ravel())
print(grid)
#输出如下:
[[1 1 1][2 2 2][3 3 3]]
[[2 3 4][2 3 4][2 3 4]]
[1 1 1 2 2 2 3 3 3]
[2 3 4 2 3 4 2 3 4]
[[1 2][1 3][1 4][2 2][2 3][2 4][3 2][3 3][3 4]]
x,y ,z= np.mgrid[1:4:1,2:4:1,1:3:1]
grid = np.c_[x.ravel(),y.ravel(),z.ravel()]
print(x.ravel())
print(y.ravel())
print(z.ravel())
print(grid)
[1 1 1 1 2 2 2 2 3 3 3 3]
[2 2 3 3 2 2 3 3 2 2 3 3]
[1 2 1 2 1 2 1 2 1 2 1 2]
[[1 2 1]
[1 2 2]
[1 3 1]
[1 3 2]
[2 2 1]
[2 2 2]
[2 3 1]
[2 3 2]
[3 2 1]
[3 2 2]
[3 3 1]
[3 3 2]]
激活函数
对于初学者的建议: 首选relu激活函数; 学习率设置较小值; 输入特征标准化,即让输入特征满足以0为均值,以1为标准差的正态分布; 初始参数中心化,即让随机生成的参数满足以0为均值,(2/当前层的输入特征个数)^0.5^为标准差的正态分布;
损失函数(loss):预测值与真实答案的差距
神经网络的目标:最小化loss;常见的有均方误差---loss_mse = tf.reduce_mean(tf.suqare(y_-y)),也可以自定义误差,交叉熵损失函数CE(Cross Entropy):表征两个概率发布之间的距离:H(y_,y) = -∑{y_ * lny}; 代码中使用:tf.losses.categorical_crossentropy(y_,y)
#在程序中使用交叉熵损失函数时,一般先使用softmax处理输出,在拿处理后的输出计算交叉熵损失函数,tf给出了tf.NN.softmax_cross_entropy_with_logits(y_,y) 将两个过程合二为一。
缓解过拟合---正则化
------对应class2:p29 正则化在损失函数中引入模型复杂度指标,利用给w加权值,弱化来训练数据的噪声(一般不正则化b)
# loss = loss(y_,y) + REGULARIZER*loss(w)---前半部分和以往损失函数定义一样,REGULARIZER是一个超参数,loss(w) 一般有两种定义:
# L1正则化:loss(w) = ∑|w| ----大概率会使很多参数为零,因此该方法可以通过稀疏参数,即减少参数的数量,降低复杂度
#L2正则化:loss(w) = ∑|w*w| ----会使很多参数接近零,但不为零,该方法可以通过减小参数值的大小降低复杂度。
#
神经网络参数优化器
#待优化参数w,损失函数loss,学习率lr,每次迭代一个batch,t表示当前迭代的总次数:
#计算t时刻损失函数关于当前参数的梯度gt =▽loss
#计算当前时刻的一阶动量mt,二阶动量Vt
#计算当前时刻的下降梯度:ηt= lr · mt/√Vt
#计算下一时刻的参数:wt+1 = w~ - ηt
#其中:一阶动量:与梯度相关的函数,二阶动量:与梯度平方相关的函数
SGD(无 momentum)
mt = gt ; Vt =1;
wt+1 = wt- lr · gt
SGDM(含 momentum) ; β 接近于1,一般取0.9
mt = β · mt-1+ (1-β) · gt ;Vt = 1
ηt= lr · mt/√Vt = lr · mt
wt+1 = wt - ηt
Adagrad,在SGD基础上增加二阶动量
mt = gt ; Vt = ∑i=1-t{gi · gi}
ηt= lr · mt/√Vt
wt+1 = wt - ηt
RMSProp,在SGD基础上增加二阶动量
mt = gt , Vt = β · Vt-1+ (1-β) · gt· gt
ηt= lr · mt/√Vt
wt+1 = wt - ηt
Adam ,同时结合SGDM的一阶动量和RMSProp的二阶动量
mt = β1 · mt-1+ (1-β1) · gt , Vt = β2 · Vt-1+ (1-β2) · gt· gt
m' = mt/(1-β1^t^); V' = Vt-1/(1-β2^t^)
ηt= lr · m'/√V'
wt+1 = wt - ηt
tf.keras 搭建网络
#六步
#1,import
#2,train,test
#3,model = tf.keras.models.Sequential
#4,model.compile
#5,model.fit
#6,model.summary
#Sequential介绍
#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.regulaziers.l2()
#卷积层:tf.keras.layers.Conv2D(filters = 卷积核个数,kernel_size = 卷积核尺寸,strides=卷积步长,padding = "valid"/"same")
#LSTM层:tf.keras.layers.LSTM()
#model.compile(Optimizer = 优化器,loss = 损失函数,Metrics = ["准确率"])
#Optimizer可选:"sgd"/tf.keras.optimizers.SGD(lr=学习率,momentum = 动量参数)
#"adagrad"/tf.keras.optimizers.Adagrad(lr=learning rate);
#"adadelta"/tf.keras....
#"adam"/...
#loss可选:
#"mse" / tf.keras.losses.MeanSquaredError()
#"spaese_categorical_crossentropy" / tf.keras.losses.SparesCategoricalCrossentropy(from_logits=False)
#from_logits的设置,若输出符合概率发布,则是false,若输出不符合概率分布,则是true
#Metrics可选:
#"accuracy" ----输出和期望输出都是数值
#"categorical_accuracy" ----都是独热编码(概率分别)
#"sparse_categorical_accuracy" ----期望输出是数值,输出是独热编码(概率发布)
#model.fit(训练集输入特征,训练集的标签,batch_size = ,epochs= ,validation_data=(测试集的输入特征,标签),validation_split=从训练集划分多少比例给测试集,validation_freq = 多少次epoch测试一次)
#validation_data 和 validation_split选择一个
#model.summary() 可以打印网络结构
#
#
#六步法---可以搭建非顺序结构的网络----class
#import
#train,test
#class MyModel(Model) / model = MyModel
#model.compile
#model.fit
#model.summary
#class 定义:
class MyModel(Model):
def __init__(self):
super(MyModel,self).__init__()
self.d1 = Dense(3)
#定义网络结构
def call(self,x):
#调用网络结构块,实现前向传播
y = self.d1(x)
return y
model = MyModel()
#example 鸢尾花分类
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.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(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()
自制数据集
import tensorflow as tf
from PIL import Image
import numpy as np
import os
train_path = './mnist_image_label/mnist_train_jpg_60000/'
train_txt = './mnist_image_label/mnist_train_jpg_60000.txt'
x_train_savepath = './mnist_image_label/mnist_x_train.npy'
y_train_savepath = './mnist_image_label/mnist_y_train.npy'
test_path = './mnist_image_label/mnist_test_jpg_10000/'
test_txt = './mnist_image_label/mnist_test_jpg_10000.txt'
x_test_savepath = './mnist_image_label/mnist_x_test.npy'
y_test_savepath = './mnist_image_label/mnist_y_test.npy'
#创建数据集的过程:
def generateds(path, txt):
f = open(txt, 'r') # 以只读形式打开txt文件
contents = f.readlines() # 读取文件中所有行
f.close() # 关闭txt文件
x, y_ = [], [] # 建立空列表
for content in contents: # 逐行取出
value = content.split() # 以空格分开,图片路径为value[0] , 标签为value[1] , 存入列表
img_path = path + value[0] # 拼出图片路径和文件名
img = Image.open(img_path) # 读入图片
img = np.array(img.convert('L')) # 图片变为8位宽灰度值的np.array格式
img = img / 255. # 数据归一化 (实现预处理)
x.append(img) # 归一化后的数据,贴到列表x
y_.append(value[1]) # 标签贴到列表y_
print('loading : ' + content) # 打印状态提示
x = np.array(x) # 变为np.array格式
y_ = np.array(y_) # 变为np.array格式
y_ = y_.astype(np.int64) # 变为64位整型
return x, y_ # 返回输入特征x,返回标签y_
#首先判断数据集是否已经生成,若生成就,直接加载,只有第一次需要制作数据集
if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath) and os.path.exists(
x_test_savepath) and os.path.exists(y_test_savepath):
print('-------------Load Datasets-----------------')
x_train_save = np.load(x_train_savepath)
y_train = np.load(y_train_savepath)
x_test_save = np.load(x_test_savepath)
y_test = np.load(y_test_savepath)
x_train = np.reshape(x_train_save, (len(x_train_save), 28, 28))
x_test = np.reshape(x_test_save, (len(x_test_save), 28, 28))
else:
print('-------------Generate Datasets-----------------')
x_train, y_train = generateds(train_path, train_txt)
x_test, y_test = generateds(test_path, test_txt)
print('-------------Save Datasets-----------------')
x_train_save = np.reshape(x_train, (len(x_train), -1))
x_test_save = np.reshape(x_test, (len(x_test), -1))
np.save(x_train_savepath, x_train_save)
np.save(y_train_savepath, y_train)
np.save(x_test_savepath, x_test_save)
np.save(y_test_savepath, y_test)
#神经网络过程
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()
读取和保存模型
#读取模型:
#load_weights(路径文件名)
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path +'.index'):
model.load_weights(checkpoint_save_path)
#保存模型:
#cp_callback = tf.keras.callback.ModelCheckpoint(filepath = 路径文件名,save_weights_only = True/False,save_best_only = True/False)
#history = model.fit(callbacks =[...,...,...cp_callback])
#example
import tensorflow as tf
import os
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'])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
model.summary()
参数提取
#model.trainable_variables //返回模型中可训练参数
#设置print输出格式
np.set_printoptions(threshould = np.inf)#表示超过threshould的省略显示,现在使用np.inf表示无限大,即不省略表示
print(model.trainable_variables)
file = open('./weights.txt','w')
for v in model.trainable_variables:
file.write(str(v.name)+'\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy())+'\n')
file.close()
#example
import tensorflow as tf
import os
import numpy as np
np.set_printoptions(threshold=np.inf)
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'])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
model.summary()
print(model.trainable_variables)
file = open('./weights.txt', 'w')
for v in model.trainable_variables:
file.write(str(v.name) + '\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy()) + '\n')
file.close()
acc曲线和loss曲线
history = model.fit(训练集数据,训练集标签,batch_size=,epochs=,
validation_spilt=用作测试数据的比例,validation_data=测试集,validation_freq=测试频率)
history:
#训练集loss:loss
#测试集loss:val_loss
#训练集准确率:sparse_categorical_accuracy
#测试集准确率:val_sparse_categorical_accuracy
#example
import tensorflow as tf
import os
import numpy as np
from matplotlib import pyplot as plt
np.set_printoptions(threshold=np.inf)
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'])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
model.summary()
print(model.trainable_variables)
file = open('./weights.txt', 'w')
for v in model.trainable_variables:
file.write(str(v.name) + '\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy()) + '\n')
file.close()
############################################### show ###############################################
# 显示训练集和验证集的acc和loss曲线
acc = history.history['sparse_categorical_accuracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
给图识物
#复现模型
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),...])
#加载参数
model.load_weights(model_save_path)
#预测结果
result = model.predict(x_predict)
卷积神经网络
tf.keras.layers.Conv2D([
filters = 卷积核个数
kernel_size = 卷积核尺寸,正方形写核长整数或(核高h,核宽w)
strides = 滑动步长,横纵相等写buchang整数或(纵向步长h,横向步长w)默认为1
padding = "same" or "valid" # 使用全零填充是same,不使用是valid(默认)
activation = "relu" or "sigmoid" or "tanh" or "softmax"等#若有BN(批标准化)此处不写
input_shape = (高,宽,通道数) #输入特征图维度,可省略
])
model = tf.keras.models.Sequential([
Conv2D(6,5,padding='valid',activation='sigmoid'),
MaxPool2D(2,2),
Conv2D(6,(5,5,),padding='valid',activation='sigmoid'),
MaxPool2D(2,(2,2)),
Conv2D(filters=6,kernel_sizes=(5,5),padding = 'valid',activation='sigmoid')
MaxPool2D(pool_size=(2,2),strdes=2),
Flatten(),
Dense(10,activation='softmax')
])
批标准化(BN) batch normalization
标准化:使数据符合0均值,1为标准差的正态分布, 批标准化:对一小批数据(batch),做标准化处理 BN层位于卷积层和激活层之间。
model = tf.keras.models.Sequential([
Conv2D(filters=6,kernel_size = (5,5),padding = 'same'),#卷积层
BatchNormalization(),#BN层
Activation('relu'),#激活层
MaxPool2D(pool_size=(2,2),strides=2,padding='same'),#池化层
Dropout(0.2),#dropout层
])
池化操作:
tf.keras.layers.MaxPool2D(
pool_size=池化核尺寸,#正方形写核长,或(核高h,核宽w)
strides = 池化步长,#步长整数或(纵向步长h,横向步长w),默认为pool_size
padding ='valid'or 'same' #使用全零填充或者不使用,默认不使用
)
舍弃 为了缓解神经网络过拟合,在神经网络训练时,将一部分神经元按一定比例从神经网络中暂时舍弃,在神经网络使用时,被舍弃的神经元恢复连接。
#tf.keras.layers.Dropout(舍弃概率)
Dropout(0.2)
卷积神经网络: 借助卷积核提起特征后,送入全连接网络 卷积神经网络的主要模块: 卷积---批标准化---激活---池化---全连接 卷积就是特征提取器,CBAPD