美文网首页我爱编程
DeepLearn_TensorFlow入门练习1-1

DeepLearn_TensorFlow入门练习1-1

作者: 明天会更好_4c14 | 来源:发表于2018-03-18 21:14 被阅读0次

1. Implement Logistic Regression with Mini-batch Gradient Descent using TensorFlow. Train it and evaluate it on the moons dataset .Try adding all the bells and whistles:

A. Define the graph within a logistic_regression() function that can be reused easily. (submit python file)

B. Save checkpoints using a Saver at regular intervals during training, and save the final model at the end of training. (submit both python file and the saved model)

C. Restore the last checkpoint upon startup if training was interrupted. (submit python file)

D. Define the graph using nice scopes so the graph looks good in TensorBoard. (submit python file)

E. Add summaries to visualize the learning curves in TensorBoard. (submit python file)

F. Try tweaking hyperparameters, including the learning rate and the mini- batch size and display the shape of the learning curve. (submit screenshot)


A.使用Tensorlow实现小批量梯度下降(Mini-Batch)的逻辑回归

import tensorflowas tf
from sklearn.datasetsimport make_moons
# trainnumber 训练集的大小
# x_train 输入
# y_train 输出
# weight 权重
# bia 偏量
# itert 迭代次数
# learnrate 学习率
# batchsize 一个mini-batch大小
#传入上述参数返回 weight与bia
def getWegihtBia(trainnumber, x_train, y_train, weight, bia, itert, learnrate, batchsize, sess):
weight = tf.Variable(weight,dtype=tf.float32,name="weight")
bia = tf.Variable(bia,dtype=tf.float32,name="bia")
x_train = tf.cast(x_train,dtype=tf.float32,name="x_train")
y_train = tf.cast(y_train.reshape([trainnumber,1]),dtype=tf.float32,name="y_train")
input = tf.placeholder(dtype=tf.float32,shape=[None,2],name="input")
output = tf.placeholder(dtype=tf.float32,shape=[None,1],name="output")
y_pred = tf.add(tf.matmul(input, weight), bia)#y=w*x+b 预测值 
mse = tf.reduce_mean(tf.square(y_pred - output))#根据误差调整
init = tf.global_variables_initializer()
sess.run(init)
opt = tf.train.GradientDescentOptimizer(learnrate)#学习率
train_step = opt.minimize(mse)
#此处为实现mini-batch
x_templist = []
y_templist = []
for j in range(itert):
        for i in range(trainnumber):
                 if i % batchsize ==0 and i !=0:
                       sess.run(train_step,feed_dict={input: x_templist, output: y_templist})
                        x_templist.clear()
                        y_templist.clear()
                        x_templist.append(x_train.eval(session=sess)[i])
                        y_templist.append(y_train.eval(session=sess)[i])
                else:
                        x_templist.append(x_train.eval(session=sess)[i])
                        y_templist.append(y_train.eval(session=sess)[i])
return weight, bia

def accuracy(testnumber, x_test, y_test, weight, bia):
y_test = y_test.reshape([testnumber,1])
right =0
    for iin range(testnumber):
            y_out = x_test[i][0] * weight[0][0] + x_test[i][1] * weight[1][0] + bia[0][0]
            y_outvalue = sess.run(y_out)
            y_pred =0
            if y_outvalue >=0.5:
                y_pred =1
            if y_pred == y_test[i][0]:
                right = right +1
print("accuracy is " +str(right / testnumber))testnumber =100
#参数
trainnumber =500
batchsize =50
itert =500
learnrate =0.01
x_train, y_train = make_moons(trainnumber)
x_test,  y_test =  make_moons(testnumber)
weight = tf.Variable(tf.random_normal(shape=[2,1]))
bia = tf.Variable(tf.random_normal(shape=[1,1]))
sess = tf.Session()
weight,bia = getWegihtBia(trainnumber, x_train, y_train, weight, bia, itert, learnrate, batchsize, sess)
accuracy(testnumber, x_test, y_test, weight, bia)
sess.close()
#从make_moons中产生的数据集合为
x:[[xx,xx][xx,xx][xx,xx]...]
y:[x,x,x...]因此此处需调整为[[x],[x],[x]..] 即getWegihtBia中的y_train.reshape([trainnumber,1]) (即转换为trainnumber维度,1维的数据集合)


B.保存参数

#训练途中或者训练完成保存模型,会有对应4个文件:
#checkpoint
#Logistic Regression_Moon.ckpt.data-00000-of-00001
#Logistic Regression_Moon.ckpt.index
#Logistic Regression_Moon.ckpt.meta
for j in range(itert):
    saver.save(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")
    for i in range(trainnumber): 
        if i % batchsize ==0 and i !=0:
            sess.run(train_step,feed_dict={input: x_templist, output: y_templist})
            saver.save(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")
            x_templist.clear()
            y_templist.clear()
            x_templist.append(x_train.eval(session=sess)[i])
            y_templist.append(y_train.eval(session=sess)[i])
        else:
            x_templist.append(x_train.eval(session=sess)[i])
            y_templist.append(y_train.eval(session=sess)[i])
    saver.save(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")


C.中断的过程中恢复训练

#此处可恢复上次中断的数据,想要恢复的数据需要tf.Variable来设置
#其中值得注意的是多次赋值如:
# A = tf. tf.Variable(8,dtype=tf.int32,name="A")
# A = tf. tf.Variable(9,dtype=tf.int32,name="A")
# 执行上述两次再保存模型获取 根据name=A获取的数据为8,
# 若想获取A = 9 需要根据name=A_1 此处有困惑
weight = tf.Variable(weight,dtype=tf.float32,name="weight")
bia = tf.Variable(bia,dtype=tf.float32,name="bia")
#...此处设置参数
saver = tf.train.Saver()
sess = tf.Session()
init_op = tf.initialize_all_variables()
sess.run(init_op)
saver.restore(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")
weight,bia = getWegihtBia(trainnumber, x_train, y_train, weight, bia, itert, learnrate, batchsize, sess)


D.TensorBoard运用scopes使图更好看
E.TensorBoard画学习曲线
F.更改参数(learn rate,mini-batch Size..)

TensorBoard 图1 TensorBoard 图2

通过运用tf.name_scope:这个函数可以是图分割更易于理解
with tf.name_scope("Trian")as Trian:
                weight, bia = getWegihtBia(trainnumber, x_train, y_train, weight, bia,                                         itert, learnrate, batchsize, sess)

画MSE曲线:

mini-batch MSE 图3

          mini-batch梯度下降 训练结果会震荡如图3,mini-batch可以解决资料量巨大时候batch 梯度下降无法读取所有资料的问题。
def getWegihtBia(trainnumber, x_train, y_train, weight, bia, itert, learnrate, batchsize, sess):
with tf.name_scope("WeightBia")as WeightBia:
        weight = tf.Variable(weight,dtype=tf.float32,name="weight")
        bia = tf.Variable(bia,dtype=tf.float32,name="bia")
with tf.name_scope("TrainSet")as TrainSet:
        x_train = tf.Variable(x_train,dtype=tf.float32,name="x_train")
        y_train = tf.Variable(y_train.reshape([trainnumber,1]),dtype=tf.float32,name="y_train")
with tf.name_scope("MSE")as MSE:
        input = tf.placeholder(dtype=tf.float32,shape=[None,2],name="input")
        output = tf.placeholder(dtype=tf.float32,shape=[None,1],name="output")
        y_pred = tf.add(tf.matmul(input, weight), bia)
        mse = tf.reduce_mean(tf.square(y_pred - output))
        opt = tf.train.GradientDescentOptimizer(learnrate)
        train_step = opt.minimize(mse)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
sess.run(init)
mse_summary = tf.summary.scalar('MSE', mse)#记录的mse
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")#获取时间
root_logdir = os.getcwd() +"/tf_logs"#设置路径
logdir ="{}/run-{}/".format(root_logdir, now)#目录以上述路径+时间

file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
x_templist = []
y_templist = []
index =0
for j in range(itert):
     for i in range(trainnumber):
            if i % batchsize ==0 and i !=0:
                    summary_str = mse_summary.eval(session=sess,feed_dict={input: x_templist, output: y_templist})#计算mse
                    file_writer.add_summary(summary_str, index)#记录mse
  
                    index +=1
                     sess.run(train_step,feed_dict={input: x_templist, output: y_templist})

                    save_path = saver.save(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")
                    x_templist.clear()
                    y_templist.clear()
                    x_templist.append(x_train.eval(session=sess)[i])
                    y_templist.append(y_train.eval(session=sess)[i])
        else:
                x_templist.append(x_train.eval(session=sess)[i])
                y_templist.append(y_train.eval(session=sess)[i])
                file_writer.close()
                save_path = saver.save(sess, os.getcwd() +"/tmp/Logistic Regression_Moon.ckpt")
return weight, bia

运行会在当前目录产生tf_logs/run-20180318124429/events.out.tfevents.1521377069.DESKTOP-9MSI5BD类似的文件,控制台,cd E:\Anaconda3\Scripts目录下,再输入
tensorboard --logdir E:\JetBrains\PycharmProjects\DeepLearnHW1\tf_logs\
其中后者为上述文件所在目录 ,再输入所显示网址即可浏览

TensorBoard开启图4

相关文章

网友评论

    本文标题:DeepLearn_TensorFlow入门练习1-1

    本文链接:https://www.haomeiwen.com/subject/ookyqftx.html