tensorflow 学习之 cifar_10 模型定义 4000
2017-07-12 21:07
543 查看
# -*- coding: utf-8 -*- import os import tensorflow as tf import new_cifar10_input FLAGS=tf.app.flags.FLAGS #解析命令行传递的参数 #设置模型参数 tf.app.flags.DEFINE_integer('batch_size',128,"""Number of images to process in a batch.""") tf.app.flags.DEFINE_string('data_dir','/tmp/cifar10_data',"""Path to the CIFAR-10 data directory.""") tf.app.flags.DEFINE_boolean('use_fp16',False,"""Train the model using fp16.""") #数据集的全局常量 IMAGE_SIZE =new_cifar10_input.IMAGE_SISE NUM_CLASSES =new_cifar10_input.NUM_CLASSES NUM_EXAMOLES_PER_EPOCH_FOR_TRAIN =new_cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN NUM_EXAMOLES_PER_EPOCH_FOR_EVAL = new_cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL #训练的常量 MOVING_AVERAGE_DEVAY=0.999 #移动平均衰减率 NUM_EPOCHS_PER_DECAY=350.0 #衰减呈阶梯函数,控制衰减周期(阶梯宽度) 每350epoch衰减一次 LEARNING_RATE_DECAY_FACTOR=0.1 #学习率衰减因子 INITIAL_LEARNING_RATE=0.1 #初始化学习率 TOWER_NAME='tower' DATA_URL='http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz' #创建直方图,以及衡量稀疏度的量,在tensorboard展现出来 def _activation_summary(x): tensor_name=re.sub('%s_[0-9]*/'%TOWER_NAME,'',x.op.name) tf.summary.histogram(tensor_name+'/activations',x) tf.summary.scalar(tensor_name+'/sparity',tf.nn.zero_fraction(x)) def _variable_on_cpu(name,shape,initializer): with tf.float16('/cup:0'): # #一个 context manager,用于为新的op指定要使用的硬件 dtype=tf.float16 if FLAGS.use_fp16 else tf.float32 var=tf.get_variable(name,shape,initializer=initializer,dtype=dtype) return var def _variable_with_weight_decay(name,shape,stddev,wd): dtype=tf.float16 if FLAGS.use_fp16 else tf.float32 var=_variable_on_cpu(name,shape,tf.truncated_normal_initializer(stddev=stddev,dtype=dtype)) if wd is not None: weight_decay=tf.multiply(tf.nn.l2_loss(var),wd,name='weight_loss') tf.add_to_collection('losses',weight_decay) return var def distorted_inputs(): if not FLAGS.data_dir: raise ValueError('Please supply a data_dir') data_dir =os.path.join(FLAGS.data_dir,'cifar-10-batches-bin') images,lables=new_cifar10_input.distorted_inputs(data_dir=data_dir,batch_size=FLAGS.batch_size) if FLAGS.use_fp16: images=tf.cast(images,tf.float16) lables=tf.cast(lables,tf.float16) return images,lables def inputs(eval_data): if not FLAGS.data_dir: raise ValueError('Please supply a data_dir') data_dir =os.path.join(FLAGS.data_dir,'cifar-10-batches-bin') images,labels=new_cifar10_input.inputs(eval_data=eval_data,data_dir=data_dir,batch_size=batch_size) if FLAGS.use_fp16: images=tf.cast(images,tf.float16) labels=tf.cast(labels,tf.float16) return images,labels def inference(images): #卷积和池化第一层 with tf.variable_scope('conv1') as scope: kernel=_variable_with_weight_decay('weights',shape=[5,5,3,64],stddev=5e-2,wd=0.0) conv=tf.nn.conv2d(images,kernel,[1,1,1,1],padding='SAME') biases=_variable_on_cpu('biases',[64],tf.constant_initializer(0.0)) pre_activation=tf.nn.bias_add(conv,biases) conv1=tf.nn.relu(pre_activation,name=scope.name) _activation_summary(conv1) pool1=tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool1') norm1=tf.nn.lrn(pool1,4,bias=1.0,alpha=0.001/9.0,beta=0.75,name='norm1') #卷积和池化第二层 with tf.variable_scope('conv2') as scope: kernel=_variable_with_weight_decay('weights',shape=[5,5,64,64],stddev=5e-2,wd=0.0) conv=tf.nn.conv2d(norm1,kernel,[1,1,1,1],padding='SAME') biases=_variable_on_cpu('biases',[64],tf.constant_initializer(0.1)) pre_activation=tf.nn.bias_add(conv,biases) conv2=tf.nn.relu(pre_activation,name=scope.name) _activation_summary(conv2) norm2=tf.nn.lrn(conv2,4,bias=1.0,alpha=0.001/9.0,beta=0.75,name='norm2') pool2=tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool2') #全连接层 with tf.variable_scope('fc1') as scope: reshape=tf.reshape(pool2,[FLAGS.batch_size,-1]) dim=reshape.get_shape()[1].value weights=_variable_with_weight_decay('weights',shape=[dim,384],stddev=0.04,wd=0.004) biases=_variable_on_cpu('biases',[384],tf.constant_initializer(0.1)) fc1=tf.nn.relu(tf.matmul(reshape,weights)+biases,name=scope.name) _activation_summary(fc1) with tf.variable_scope('fc2') as scope: weights=_variable_with_weight_decay('weights',shape=[384,192],stddev=0.04,wd=0.004) biases=_variable_on_cpu('biased',[192],tf.constant_initializer(0.1)) fc2=tf.nn.relu(tf.matmul(fc1,weights)+biases,name=scope.name) _activation_summary(fc2) #进行线性变换输出logistics模型 with tf.variable_scope('sotfmax_linear') as scope: weights=_variable_with_weight_decay('weights',[192,NUM_CLASSES],stddev=1/192.0,wd=0.0) biases=_variable_on_cpu('biases',[NUM_CLASSES],tf.constant_initializer(0.0)) softmax_linear=tf.add(tf.matmul(fc2,weights),biases,name=scope.name) _activation_summary(softmax_linear) return softmax_linear
还有很多地方不理解,慢慢磨吧~
相关文章推荐
- tensorflow 学习之 cifar_10 模型定义(补)
- tensorflow学习笔记----二(CIFAR-10 模型 )
- tensorflow学习之cifar_10模型评估
- 【深度学习】针对CIFAR-10模型所写的进行图片size,图片名,后缀图片格式写的预处理程序
- 深度学习小白——tensorflow(四)CIFAR-10实例
- Tensorflow深度学习之二十二:AlexNet的实现(CIFAR-10数据集)
- Tensorflow学习笔记:CNN篇(5)——CIFAR-10数据集VGG19实现(Keras版)
- TensorFlow学习笔记---CNN分类CIFAR-10数据集3
- TensorFlow深度学习进阶教程:TensorFlow实现CIFAR-10数据集测试的卷积神经网络
- Tensorflow学习笔记:CNN篇(3)——CIFAR-10数据集的CNN实现
- TensorFlow学习--卷积神经网络训练CIFAR-10数据集
- Deep Learning-TensorFlow (5) CNN卷积神经网络_CIFAR-10进阶图像分类模型(下)
- tensorflow 学习笔记10 网络模型的保存与提取
- Tensorflow学习笔记:CNN篇(4)——CIFAR-10数据集LeNet实现(Keras版)
- 【深度学习】笔记4_caffe第二个比较经典的[小图片]识别例子CIFAR_10的运行,网络模型的详解
- Deep Learning-TensorFlow (10) CNN卷积神经网络_ TFLearn 快速搭建深度学习模型
- TensorFlow学习——CIFAR-10(python实现数据可视化)
- TensorFlow学习——CIFAR-10(二)代码实现
- Alexnet网络模型在cifar-10数据集上的实现(基于tensorflow-gpu)
- iOS学习- 10 定义数据模型及构造本地运行时数据 - Todo App