TensorFlow 入门实操课程-Day2

3、卷积神经元网络CNN--提高计算机视觉精度
卷积比全连接花费时间长,但精确度更高,loss更少
MaxPooling:卷积后处理,增强图像特征,减少数据,1/4
Conv2D:两维卷积层,64个过滤器,3x3像素/个

model = keras.Sequential([
    #卷积
    keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)),
    keras.layers.MaxPooling2D(2*2),#2x2=4个像素
    keras.layers.Conv2D(64,(3,3),activation='relu'),
    keras.layers.MaxPooling2D(2*2),
    #原有
    keras.layers.Flatten(),
    keras.layers.Dense(128,activation='relu'),
    keras.layers.Dense(10,activation='softmax')
])
完整:
import tensorflow as tf
from tensorflow import keras
#加载数据集 fashion_mnist
= keras.datasets.fashion_mnist (train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()
#数据转换为[0,1]之间的数
train_images=train_images/255
#构建模型
model = keras.Sequential([
    #卷积
    keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)),
    keras.layers.MaxPooling2D(2,2),#2x2=4个像素
    keras.layers.Conv2D(64,(3,3),activation='relu'),
    keras.layers.MaxPooling2D(2,2),
    #原有
    keras.layers.Flatten(),
    keras.layers.Dense(128,activation=tf.nn.relu),
    keras.layers.Dense(10,activation=tf.nn.softmax)
])
#训练模型
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images.reshape(-1,28,28,1),train_labels,epochs=5)
#查看模型结构
model.summary()

模型结构如下

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 64)        640       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 64)        0        
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        36928     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0         
_________________________________________________________________
dense (Dense)                (None, 128)               204928    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================

解释

#卷积
第一层Conv2D:输入28*28,过滤器3*3,所以26*26个像素尺寸,64个过滤器,1张图-->64张图
(3*3+1)*64=640,过滤器+一个bias
第二层MaxPooling:26÷2=13,26÷2=13,64个过滤器
第三层Conv2D:输入13*13,过虑器3*3,所以11*11个像素尺寸
(3*3*64+1)*64=36928
第四层MaxPooling:11÷2=5,11÷2=5,64个过滤器
#原有
第五层Flatten:展开,5*5*64=1600
第六层Dense:自定义
第七层Dense:自定义

查看每层结构

import matplotlib.pyplot as plt
layer_outputs=[layer.output for layer in model.layers]
activation_model=tf.keras.models.Model(inputs=model.input,outputs=layer_outputs)
pred = activation_model.predict(test_images[0].reshape(1,28,28,1))
pred[0].shape#输出第一层:(1, 26, 26, 64)
#查看图片
plt.imshow(pred[0][0,:,:,0])#取第一个过滤器
原文地址:https://www.cnblogs.com/DLYQY/p/14429201.html