CNN自学入门

it2022-05-05  156

MNIST自学

各个函数的参数含义:

t f . t r u n c a t e d _ n o r m a l ( s h a p e , m e a n = 0.0 , s t d d e v = 1.0 , d t y p e = t f . f l o a t 32 , s e e d = N o n e , n a m e = N o n e ) {tf.truncated\_normal(shape, mean=0.0, stddev=1.0,dtype=tf.float32,seed=None,name=None)} tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None) s h a p e shape shape表示生成张量的维度, m e a n mean mean是均值, s t d d e v stddev stddev是标准差。这个函数产生正态分布,均值和标准差自己设定。产生截断正态分布随机数,取值范围为 [ m e a n − 2 ∗ s t d d e v , m e a n + 2 ∗ s t d d e v ] {[mean -2*stddev,mean+2*stddev]} [mean2stddev,mean+2stddev]

卷积: t f . n n . c o n v 2 d ( i n p u t , f i l t e r , s t r i d e s , p a d d i n g , u s e _ c u d n n _ o n _ g p u = N o n e , n a m e = N o n e ) {tf.nn.conv2d(input, filter, strides, padding, use\_cudnn\_on\_gpu=None, name=None)} tf.nn.conv2d(input,filter,strides,padding,use_cudnn_on_gpu=None,name=None)

i n p u t input input:输入的要做卷积的图片,要求为一个张量, s h a p e shape shape [ b a t c h , i n _ h e i g h t , i n _ w e i g h t , i n _ c h a n n e l ] [batch,in\_height,in\_weight,in\_channel] [batch,in_height,in_weight,in_channel],其中 b a t c h batch batch为图片的数量, i n _ h e i g h t in\_height in_height为图片高度, i n _ w e i g h t in\_weight in_weight为图片宽度, i n _ c h a n n e l in\_channel in_channel为图片的通道数,灰度图该值为1,彩色图为3。 f i l t e r filter filter:卷积核,要求也是一个张量, s h a p e shape shape [ f i l t e r _ h e i g h t , f i l t e r _ w e i g h t , i n _ c h a n n e l , o u t _ c h a n n e l s ] [filter\_height, filter\_weight, in\_channel, out\_channels] [filter_height,filter_weight,in_channel,out_channels],其中 f i l t e r _ h e i g h t filter\_height filter_height为卷积核高度, f i l t e r _ w e i g h t filter\_weight filter_weight为卷积核宽度, i n _ c h a n n e l in\_channel in_channel是图像通道数 ,和 i n p u t input input i n _ c h a n n e l in\_channel in_channel要保持一致, o u t _ c h a n n e l out\_channel out_channel是卷积核数量。 s t r i d e s strides strides:卷积时在图像每一维的步长,这是一个一维的向量, [ 1 , s t r i d e s , s t r i d e s , 1 ] [1,strides,strides,1] [1,strides,strides,1],第一位和最后一位固定必须是1。 p a d d i n g padding padding s t r i n g string string类型,值为 “ S A M E ” “SAME” SAME “ V A L I D ” “VALID” VALID,表示的是卷积的形式,是否考虑边界。 “ S A M E ” “SAME” SAME是考虑边界,不足的时候用0去填充周围, “ V A L I D ” “VALID” VALID则不考虑。 u s e _ c u d n n _ o n _ g p u use\_cudnn\_on\_gpu use_cudnn_on_gpu: b o o l bool bool类型,是否使用 c u d n n cudnn cudnn加速,默认为 t r u e true true

最大池化: t f . n n . m a x _ p o o l ( v a l u e , k s i z e , s t r i d e s , p a d d i n g , n a m e = N o n e ) {tf.nn.max\_pool(value, ksize, strides, padding, name=None)} tf.nn.max_pool(value,ksize,strides,padding,name=None)

v a l u e value value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是 f e a t u r e   m a p feature\ map feature map,依然是[batch, height, width, channels]这样的shape k s i z e ksize ksize:池化窗口的大小,取一个四维向量,一般是 [ 1 , h e i g h t , w i d t h , 1 ] [1, height, width, 1] [1,height,width,1],因为我们不想在 b a t c h batch batch c h a n n e l s channels channels上做池化,所以这两个维度设为了1。 s t r i d e s strides strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是 [ 1 , s t r i d e , s t r i d e , 1 ] [1, stride,stride, 1] [1,stride,stride,1] p a d d i n g padding padding:和卷积类似,可以取 ′ V A L I D ′ 'VALID' VALID 或者 ′ S A M E ′ 'SAME' SAME

Dropout(一般用在全连接层) t f . n n . d r o p o u t ( x , k e e p _ p r o b , n o i s e _ s h a p e = N o n e , s e e d = N o n e , n a m e = N o n e ) {tf.nn.dropout(x,keep\_prob,noise\_shape=None, seed=None, name=None)} tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None)

x x x:指输入,输入 t e n s o r tensor tensor k e e p p r o b keep_prob keepprob f l o a t float float类型,每个元素被保留下来的概率,设置神经元被选中的概率,在初始化时 k e e p _ p r o b keep\_prob keep_prob是一个占位符。 n o i s e _ s h a p e noise\_shape noise_shape:一个1维的 i n t 32 int32 int32张量,表示随机生成“保留/丢弃”标志的 s h a p e shape shape。在默认情况下,每个元素独立安排保留或者丢弃。如果已经指定 n o i s e _ s h a p e noise\_shape noise_shape,则 x x x的形状必须为可广播的。

代码部分:全部来自于Tensorflow中文社区(http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html)

源代码1(爬虫):

# -*- coding: utf-8 -*- """ Created on Tue Jul 16 09:09:01 2019 @author: Administrator """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import gzip import os import tensorflow.python.platform import numpy from six.moves import urllib from six.moves import xrange # pylint: disable=redefined-builtin import tensorflow as tf SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/' def maybe_download(filename, work_directory): """Download the data from Yann's website, unless it's already here.""" if not os.path.exists(work_directory): os.mkdir(work_directory) filepath = os.path.join(work_directory, filename) if not os.path.exists(filepath): filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath) statinfo = os.stat(filepath) print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') return filepath def _read32(bytestream): dt = numpy.dtype(numpy.uint32).newbyteorder('>') return numpy.frombuffer(bytestream.read(4), dtype=dt)[0] def extract_images(filename): """Extract the images into a 4D uint8 numpy array [index, y, x, depth].""" print('Extracting', filename) with gzip.open(filename) as bytestream: magic = _read32(bytestream) if magic != 2051: raise ValueError('Invalid magic number %d in MNIST image file: %s' %(magic, filename)) num_images = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(rows * cols * num_images) data = numpy.frombuffer(buf, dtype=numpy.uint8) data = data.reshape(num_images, rows, cols, 1) return data def dense_to_one_hot(labels_dense, num_classes=10): """Convert class labels from scalars to one-hot vectors.""" num_labels = labels_dense.shape[0] index_offset = numpy.arange(num_labels) * num_classes labels_one_hot = numpy.zeros((num_labels, num_classes)) labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1 return labels_one_hot def extract_labels(filename, one_hot=False): """Extract the labels into a 1D uint8 numpy array [index].""" print('Extracting', filename) with gzip.open(filename) as bytestream: magic = _read32(bytestream) if magic != 2049: raise ValueError('Invalid magic number %d in MNIST label file: %s' %(magic, filename)) num_items = _read32(bytestream) buf = bytestream.read(num_items) labels = numpy.frombuffer(buf, dtype=numpy.uint8) if one_hot: return dense_to_one_hot(labels) return labels class DataSet(object): def __init__(self, images, labels, fake_data=False, one_hot=False,dtype=tf.float32): '''Construct a DataSet. one_hot arg is used only if fake_data is true. `dtype` can be either `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into `[0, 1]`. ''' dtype = tf.as_dtype(dtype).base_dtype if dtype not in (tf.uint8, tf.float32): raise TypeError('Invalid image dtype %r, expected uint8 or float32' %dtype) if fake_data: self._num_examples = 10000 self.one_hot = one_hot else: assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape,labels.shape)) self._num_examples = images.shape[0] # Convert shape from [num examples, rows, columns, depth] # to [num examples, rows*columns] (assuming depth == 1) assert images.shape[3] == 1 images = images.reshape(images.shape[0],images.shape[1] * images.shape[2]) if dtype == tf.float32: # Convert from [0, 255] -> [0.0, 1.0]. images = images.astype(numpy.float32) images = numpy.multiply(images, 1.0 / 255.0) self._images = images self._labels = labels self._epochs_completed = 0 self._index_in_epoch = 0 @property def images(self): return self._images @property def labels(self): return self._labels @property def num_examples(self): return self._num_examples @property def epochs_completed(self): return self._epochs_completed def next_batch(self, batch_size, fake_data=False): """Return the next `batch_size` examples from this data set.""" if fake_data: fake_image = [1] * 784 if self.one_hot: fake_label = [1] + [0] * 9 else: fake_label = 0 return [fake_image for _ in xrange(batch_size)], [fake_label for _ in xrange(batch_size)] start = self._index_in_epoch self._index_in_epoch += batch_size if self._index_in_epoch > self._num_examples: # Finished epoch self._epochs_completed += 1 # Shuffle the data perm = numpy.arange(self._num_examples) numpy.random.shuffle(perm) self._images = self._images[perm] self._labels = self._labels[perm] # Start next epoch start = 0 self._index_in_epoch = batch_size assert batch_size <= self._num_examples end = self._index_in_epoch return self._images[start:end], self._labels[start:end] def read_data_sets(train_dir, fake_data=False, one_hot=False, dtype=tf.float32): class DataSets(object): pass data_sets = DataSets() if fake_data: def fake(): return DataSet([], [], fake_data=True, one_hot=one_hot, dtype=dtype) data_sets.train = fake() data_sets.validation = fake() data_sets.test = fake() return data_sets TRAIN_IMAGES = 'train-images-idx3-ubyte.gz' TRAIN_LABELS = 'train-labels-idx1-ubyte.gz' TEST_IMAGES = 't10k-images-idx3-ubyte.gz' TEST_LABELS = 't10k-labels-idx1-ubyte.gz' VALIDATION_SIZE = 5000 local_file = maybe_download(TRAIN_IMAGES, train_dir) train_images = extract_images(local_file) local_file = maybe_download(TRAIN_LABELS, train_dir) train_labels = extract_labels(local_file, one_hot=one_hot) local_file = maybe_download(TEST_IMAGES, train_dir) test_images = extract_images(local_file) local_file = maybe_download(TEST_LABELS, train_dir) test_labels = extract_labels(local_file, one_hot=one_hot) validation_images = train_images[:VALIDATION_SIZE] validation_labels = train_labels[:VALIDATION_SIZE] train_images = train_images[VALIDATION_SIZE:] train_labels = train_labels[VALIDATION_SIZE:] data_sets.train = DataSet(train_images, train_labels, dtype=dtype) data_sets.validation = DataSet(validation_images, validation_labels,dtype=dtype) data_sets.test = DataSet(test_images, test_labels, dtype=dtype) return data_sets

源代码2(用CNN对MNIST数据集进行分析):

# -*- coding: utf-8 -*- """ Created on Wed Jul 17 15:56:44 2019 @author: Administrator """ import tensorflow as tf import input_data #读取数据 mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #用占位符申明变量 x = tf.placeholder("float", [None, 784]) y_ = tf.placeholder("float", [None,10]) #构建会话 sess=tf.InteractiveSession() #用于生成初始权重矩阵W,即filter def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) #设置偏置值b def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) #卷积函数 def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') #最大池化函数 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME') #filter大小为[5,5],图片通道为1,filter数量为32 W_conv1 = weight_variable([5, 5, 1, 32]) #设置偏置值 b_conv1 = bias_variable([32]) #将图片向量变为shape[28,28]的矩阵,第一个变量为-1表示根据实际维度计算出这个shape,实则为图片数量,1表示图片通道 x_image = tf.reshape(x, [-1,28,28,1]) #进行第一次卷积,我们把x_image和权值向量W_conv1进行卷积,加上偏置项,然后应用ReLU激活函数,最后进行max pooling。 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) #进行第二次卷积,因为上次卷积应用了32个filter,因此图片通道变为32,此次使用64个filter(经验) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) #因为通过两次卷积池化操作后,图片大小变为[7,7],此层为密集连接层,图片通道为64,此时考虑将图片再一维化,即变为shape(1,7*7*64)的向量,考虑全连接1024个神经元,故如此设置W_fc1与b_fc1 W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) #考虑减少过拟合,因此使用dropout来关闭部分神经元 keep_prob = tf.placeholder("float") h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) #输出层,添加一个softmax层 W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) #使用交叉熵作为loss函数 cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) #采用ADAM优化器来做梯度最速下降 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) #会话初始化 sess.run(tf.initialize_all_variables()) #迭代20000次,每100次迭代,输出一次迭代误差结果 for i in range(20000): batch = mnist.train.next_batch(50) if i0 == 0: train_accuracy = accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0}) print("step %d, training accuracy %g"%(i, train_accuracy)) train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5}) #输出最终测试结果 print("test accuracy %g"
转载请注明原文地址: https://win8.8miu.com/read-12401.html

最新回复(0)