CNN 实现
CNN相比与传统神经网络,主要区别是引入了卷积层和池化层
卷积是使用tf.nn.conv2d, 池化使用tf.nn.max_pool
CNN之keras实现
import numpy as np
np.random.seed() #为了复现
from __future__ import print_function
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten
from keras.optimizers import Adam
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#x标准化到0-1 y使用one-hot
X_train = X_train.reshape(-, ,, )/
X_test = X_test.reshape(-, ,, )/
y_train = np_utils.to_categorical(y_train, num_classes=)
y_test = np_utils.to_categorical(y_test, num_classes=)
keras.layers.convolutional.Conv2D(filters, kernel_size, strides=(1, 1), padding=’valid’, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer=’glorot_uniform’, bias_initializer=’zeros’, kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
#建立模型 使用卷积层
model = Sequential()
#输出的维度 “same”代表保留边界处的卷积结果 “valid”代表只进行有效的卷积,即对边界数据不处理 height & width & channels
model.add(Conv2D(, (, ),padding='same', activation='relu', input_shape=(, , )))
#池化层 pool_size下采样因子 strides步长
model.add(MaxPooling2D(pool_size=(, ),strides=(, ),border_mode='same'))
#断开的神经元的比例
#model.add(Dropout(0.25))
model.add(Conv2D(, (, ), activation='relu'))
model.add(MaxPooling2D(pool_size=(, )))
#Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡
model.add(Flatten())
model.add(Dense(, activation='relu'))
model.add(Dense(, activation='softmax'))
/Users/yuyin/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:5: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding="same", strides=(2, 2), pool_size=(2, 2))`
#定义优化器
adam = Adam(lr=)
#定义loss和评价函数 metrics评价可为cost,accuracy,score
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
#训练模型 epoch训练次数 batch_size 每批处理32个
model.fit(X_train, y_train, epochs=, batch_size=)
#返回测试的指标
loss, accuracy = model.evaluate(X_test, y_test)
print('\n test loss: ', loss)
print('\n test accuracy: ', accuracy)
#预测
y_pre = model.predict(X_test)
#转换成数字-每列概率最大的位置
y_num=[np.argmax(x) for x in y_pre]
Epoch 1/1
60000/60000 [==============================] - 167s - loss: 0.0617 - acc: 0.9812
9952/10000 [============================>.] - ETA: 0s
test loss: 0.0361482483758
test accuracy: 0.9878
CNN之TensorFlow实现
卷积计算输出一点得记得步长
from __future__ import print_function
# Import data
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/', 'Directory for storing data') # 第一次启动会下载文本资料,放在文件夹下
print(FLAGS.data_dir)
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=) # 变量的初始值为截断正太分布
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
"""
tf.nn.conv2d功能:给定4维的input和filter,计算出一个2维的卷积结果
前几个参数分别是input, filter, strides, padding, use_cudnn_on_gpu, ...
input 的格式要求为一个张量,[batch, in_height, in_width, in_channels],批次数,图像高度,图像宽度,通道数
filter 的格式为[filter_height, filter_width, in_channels, out_channels],滤波器高度,宽度,输入通道数,输出通道数
strides 一个长为4的list. 表示每次卷积以后在input中滑动的距离
padding 有SAME和VALID两种选项,表示是否要保留不完全卷积的部分。如果是SAME,则保留
use_cudnn_on_gpu 是否使用cudnn加速。默认是True
"""
return tf.nn.conv2d(x, W, strides=[, , , ], padding='SAME')
def max_pool_2x2(x):
"""
tf.nn.max_pool 进行最大值池化操作,而avg_pool 则进行平均值池化操作
几个参数分别是:value, ksize, strides, padding,
value: 一个4D张量,格式为[batch, height, width, channels],与conv2d中input格式一样
ksize: 长为4的list,表示池化窗口的尺寸
strides: 窗口的滑动值,与conv2d中的一样
padding: 与conv2d中用法一样。
"""
return tf.nn.max_pool(x, ksize=[, , , ],
strides=[, , , ], padding='SAME')
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None, ])
x_image = tf.reshape(x, [-,,,]) #将输入按照 conv2d中input的格式来reshape,reshape
"""
# 第一层
# 卷积核(filter)的尺寸是5*5, 通道数为1,输出通道为32,即feature map 数目为32
# 又因为strides=[1,1,1,1] 所以单个通道的输出尺寸应该跟输入图像一样。即总的卷积输出应该为 28*28*32
# 也就是单个通道输出为28*28,共有32个通道,共有?个批次
# 在池化阶段,ksize=[1,2,2,1] 那么卷积结果经过池化以后的结果,其尺寸应该是 14*14*32
"""
W_conv1 = weight_variable([, , , ]) # 卷积是在每个5*5的patch中算出32个特征,分别是patch大小,输入通道数目,输出通道数目
b_conv1 = bias_variable([])
h_conv1 = tf.nn.elu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
"""
# 第二层
# 卷积核5*5,输入通道为32,输出通道为64。
# 卷积前图像的尺寸为 14*14*32, 卷积后为 14*14*64
# 池化后,输出的图像尺寸为 7*7*64
"""
W_conv2 = weight_variable([, , , ])
b_conv2 = bias_variable([])
h_conv2 = tf.nn.elu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
# 第三层 是个全连接层,输入维数7*7*64, 输出维数为1024
W_fc1 = weight_variable([ * * , ])
b_fc1 = bias_variable([])
h_pool2_flat = tf.reshape(h_pool2, [-, **])
h_fc1 = tf.nn.elu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32) # 这里使用了drop out,即随机安排一些cell输出值为0,可以防止过拟合
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# 第四层,输入1024维,输出10维,也就是具体的0~9分类
W_fc2 = weight_variable([, ])
b_fc2 = bias_variable([])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # 使用softmax作为多分类激活函数
y_ = tf.placeholder(tf.float32, [None, ])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[])) # 损失函数,交叉熵
train_step = tf.train.AdamOptimizer().minimize(cross_entropy) # 使用adam优化
correct_prediction = tf.equal(tf.argmax(y_conv,), tf.argmax(y_,)) # 计算准确度
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer()) # 变量初始化
for i in range():
batch = mnist.train.next_batch()
if i% == :
train_accuracy = accuracy.eval(feed_dict={x:batch[], y_: batch[], keep_prob: })
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[], y_: batch[], keep_prob: })
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: }))
/Users/yuyin/anaconda/lib/python2.7/site-packages/pandas/computation/__init__.py:19: UserWarning: The installed version of numexpr 2.4.4 is not supported in pandas and will be not be used
UserWarning)
/Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/
Extracting /Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/train-images-idx3-ubyte.gz
Extracting /Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/train-labels-idx1-ubyte.gz
Extracting /Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting /Users/yuyin/Downloads/笔记学习/深度学习/TensorFlow实战Google深度学习框架/datasets/MNIST_data/t10k-labels-idx1-ubyte.gz
step 0, training accuracy 0.09
step 100, training accuracy 0.92
step 200, training accuracy 0.94
step 300, training accuracy 0.96
step 400, training accuracy 0.9
step 500, training accuracy 0.96
step 600, training accuracy 0.99
step 700, training accuracy 0.94
step 800, training accuracy 0.98
step 900, training accuracy 0.98
test accuracy 0.9674