Beginner deep learning (1) Iterative training and model use of the basic development steps of TensorFlow

This article uses logistic regression to fit two-dimensional data as an example to demonstrate the basic development steps of tensorflow.

Example: Find the law of y≈2x from a set of seemingly chaotic data

Example description:

Suppose there is a set of data sets whose corresponding relationship between x and y is y≈2x.

There are roughly four steps in deep learning:

(1) Prepare data
(2) Build model
(3) Iterative training
(4) Use model

3. Iterative training

(1) Training model

Through the first step of learning deep learning (1) TensorFlow basic development steps: preparing data and building a model (forward + reverse) The second step of building the model, the model can be trained through iteration .

Note: Tasks in tensorflow are carried out through session

# 初始化变量
init = tf.global_variables_initializer() # global_variables_initializer()函数初始化所有变量

# 训练参数
training_epochs = 20 # 设置迭代次数为20次
display_step = 2

# 启动session
with tf.Session() as sess:
    sess.run(init) # 通过sess.进行网络节点的运算
    plotdata = {
    
    "batchsize":[],"loss":[]} # 存放批次值和损失值

    # 向模型输入数据
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={
    
    X: x, Y: y}) # 通过feed机制将真实数据灌到占位符(第二步创建模型时)对应的位置

        # 输出显示训练中的详细信息,每执行一次都会将网络结构的节点打印出来
        if epoch % display_step == 0:
            loss = sess.run(cost, feed_dict={
    
    X: train_X, Y:train_Y})
            print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
            if not (loss == "NA" ):
                plotdata["batchsize"].append(epoch)
                plotdata["loss"].append(loss)

    print (" Finished!")
    print ("cost=", sess.run(cost, feed_dict={
    
    X: train_X, Y: train_Y}), "W=", sess.run(W), "b=", sess.run(b))

The results show that:
Insert picture description here

It can be seen from the above results that cost (the gap between the generated predicted value and the true value) is constantly getting smaller, and W and b (two parameters) are constantly changing.

(2) Visualization of training model

# 初始化变量
init = tf.global_variables_initializer() # global_variables_initializer()函数初始化所有变量

# 训练参数
training_epochs = 20 # 设置迭代次数为20次
display_step = 2

# 启动session
with tf.Session() as sess:
    sess.run(init) # 通过sess.进行网络节点的运算
    plotdata = {
    
    "batchsize":[],"loss":[]} # 存放批次值和损失值

    # 向模型输入数据
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={
    
    X: x, Y: y}) # 通过feed机制将真实数据灌到占位符(第二步创建模型时)对应的位置

        # 输出显示训练中的详细信息,每执行一次都会将网络结构的节点打印出来
        if epoch % display_step == 0:
            loss = sess.run(cost, feed_dict={
    
    X: train_X, Y:train_Y})
            print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
            if not (loss == "NA" ):
                plotdata["batchsize"].append(epoch)
                plotdata["loss"].append(loss)

    print (" Finished!")
    print ("cost=", sess.run(cost, feed_dict={
    
    X: train_X, Y: train_Y}), "W=", sess.run(W), "b=", sess.run(b))

    def moving_average(a,w=10): # 定义moving_average()函数
        if len(a) < w:
            return a[:]
        return [val if idx<w else sum(a[(idx-w):idx])/w for idx,val in enumerate(a)]

    #图形显示
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

    plotdata["avgloss"] = moving_average(plotdata["loss"])
    plt.figure(1)
    plt.subplot(211)
    plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
    plt.xlabel('Minibatch number')
    plt.ylabel('Loss')
    plt.title('Minibatch run vs. Training loss')
    plt.show()

The results show that:
Insert picture description here

  • The oblique line shown in the first picture is the linear equation of x and y formed when the parameters w and b of the model are constant. It is almost a straight line with y=2x.
  • In the second picture, it can be seen that the loss value has been decreasing at the beginning, and it has stabilized around the fifth time.

4. Use the model

After the model is trained, you only need to pass in an input value, and then use sess.run to run each node in the model to use the model.

# 初始化变量
init = tf.global_variables_initializer() # global_variables_initializer()函数初始化所有变量

# 训练参数
training_epochs = 20 # 设置迭代次数为20次
display_step = 2

# 启动session
with tf.Session() as sess:
    sess.run(init) # 通过sess.进行网络节点的运算
    plotdata = {
    
    "batchsize":[],"loss":[]} # 存放批次值和损失值

    # 向模型输入数据
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={
    
    X: x, Y: y}) # 通过feed机制将真实数据灌到占位符(第二步创建模型时)对应的位置

        # 输出显示训练中的详细信息,每执行一次都会将网络结构的节点打印出来
        if epoch % display_step == 0:
            loss = sess.run(cost, feed_dict={
    
    X: train_X, Y:train_Y})
            print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
            if not (loss == "NA" ):
                plotdata["batchsize"].append(epoch)
                plotdata["loss"].append(loss)

    print (" Finished!")
    print ("cost=", sess.run(cost, feed_dict={
    
    X: train_X, Y: train_Y}), "W=", sess.run(W), "b=", sess.run(b))

    def moving_average(a,w=10): # 定义moving_average()函数
        if len(a) < w:
            return a[:]
        return [val if idx<w else sum(a[(idx-w):idx])/w for idx,val in enumerate(a)]

    #图形显示
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

    plotdata["avgloss"] = moving_average(plotdata["loss"])
    plt.figure(1)
    plt.subplot(211)
    plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
    plt.xlabel('Minibatch number')
    plt.ylabel('Loss')
    plt.title('Minibatch run vs. Training loss')
    plt.show()
    
    print ("x=0.2,z=", sess.run(z, feed_dict={
    
    X: 0.2}))

For example, the last line of the above code is: pass in a 0.2 (feed_dict={X: 0.2}), and then use sess.run to run the z node in the model, and then you can view the value it generates.

produces result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109800508