Tensorflow实用模板

总的方法论:使用张量表示数据,计算图(只搭建网络不运算)搭建神经网络,会话执行计算图,优化线上的权重(参数),得到模型。

1.前向传播 xx_forward.py

def get_weight(shape,regularizer):
    w = tf.Variable(tf.random_normal(shape,mean,stddev,seed,dtype))
    if regularizer != None:#如果需要对w进行正则化
        tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(w))
    return w
def get_bias(shape):
    b = tf.Variable(tf.constant(...))
    return b
def forward(x,train,regularizer):
    x = tf.placeholder(dtype,shape)
    y_= tf.placeholder(dtype,shape)
    w1 =
    b1 =
    y1 =
    ......
    y =
    if train:#如果选择对全连接层使用dropout来避免过拟合
        y = tf.nn.dropout(y,0.5)#0.5:丢弃概率
    return y

2.反向传播 xx_backward.py

def backward(mnist):
    x = tf.placeholder()
    y_= tf.placeholder()
    y = xx_forward.forward(x,train=True,regularizer)
    
    #loss
    #1.mse
    loss = tf.reduce_mean(tf.square(y-y_))+tf.add_n(tf.get_collection('losses'))
    #2.entropy
    cme = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
    loss = tf.reduce_mean(cme)+tf.add_n(tf.get_collection('losses'))
    #3.自定义loss

    #采用滑动平均的学习率的优化
    global_step = tf.Variable(0,trainable=False)
    learning_rate = tf.train.exponential_decay(0.001,global_step,间隔数,
                                               0.99,staircase=True)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)

    #使用滑动平均模型
    ema = tf.train.ExponentialMovingAverage(MOVING_RATE_DECAY,global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step,ema_op]):
        train_op = tf.no_op=(name='train')

    #保存当前神经网络
    saver = tf.train.Saver()

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        #读取已经保存的模型,实现断点续训
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess,ckpt.model_checkpoint_path)#加载最新的模型

        for i in range(50000):
            start =
            end = 
            _,loss_value,step = sess.run([train_op,loss_value,global_step],feed_dict={})
            if i%1000 == 0:
                print(......) 
                #每隔1000轮保存1次
                saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),
                                              global_step=global_step))

 

3.测试 xx_test.py

def test(mnist):
    #复现计算图
    with tf.Graph().as_default() as g:
        x = tf.placeholder()
        y_= tf.placeholder()
        y = xx_forward().forward(x,train=False,regularizer=None)

        #实例化课还原滑动平均的saver
        ema = tf.train.ExponentialMovingAverage(xx_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.varaibles_to_restore()
        saver = tf.train.Saver(ema_store)
   
        #定义评价指标
        #1.分类准确率
        #2.psnr
        #.......

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(xx_backward.MODEL_SAVE_PATH)
            if ckpt and ckpy.model_checkpoint_path:
                saver.restore(sess,ckpy.model_checkpoint_path)
                global_step = ckpy.model_checkpoint_path.split('/')[-1].split('-')[-1]
                评价指标 = sess.run(评价指标,feed_dict={})
                print()
            else:
                print()


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值