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

还有很多地方不理解,慢慢磨吧~