tensorflow 学习之 cifar_10 模型定义
来源:互联网 发布:mac终端建立文件夹 编辑:程序博客网 时间:2024/06/05 15:34
# -*- coding: utf-8 -*-import osimport tensorflow as tfimport new_cifar10_inputFLAGS=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_SISENUM_CLASSES =new_cifar10_input.NUM_CLASSESNUM_EXAMOLES_PER_EPOCH_FOR_TRAIN =new_cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAINNUM_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 vardef _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 vardef 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,lablesdef 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,labelsdef 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
还有很多地方不理解,慢慢磨吧~
阅读全文
0 0
- tensorflow 学习之 cifar_10 模型定义
- tensorflow 学习之 cifar_10 模型定义(补)
- tensorflow学习之cifar_10模型评估
- tensorflow cifar_10 代码阅读与理解
- 深度学习模型之tensorflow应用
- tensorflow学习:定义变量
- thinkphp学习之模型数据表名定义
- Tensorflow深度学习之九:滑动平均模型
- TensorFlow优化模型之学习率的设置
- TensorFlow定义一个线性分类模型
- TensorFlow学习记录-- 8.TensorFlow之如何构建漂亮的模型
- GMF学习之二tianPan例子之工具模型定义
- GMF学习之三tianpan例子之图形模型定义
- 学习TensorFlow,线性回归模型
- TensorFlow学习笔记:编程模型
- TensorFlow之保存/恢复模型
- 【深度学习】笔记4_caffe第二个比较经典的[小图片]识别例子CIFAR_10的运行,网络模型的详解
- tensorflow之保存模型与加载模型
- JSON字符串转换为Map (fastjson)
- 知识图谱与知识栈
- Holding Bin-Laden Captive!(母函数)
- 算法竞赛入门经典(第2版)笔记--第2章
- 赛码网刷题之研究生考试
- tensorflow 学习之 cifar_10 模型定义
- 【题解】Round Numbers
- 线程小总结
- python mysql数据库的操作
- 编写strcpy函数
- c语言基础算法(一)---排序
- AFNetworking数据请求(深度封装)
- MySql-Set字段类型
- 稳定