TensorFlow-GPU linear regression visualization code, and summary of the problem

Visually display the training process of the TensorFlow model through TensorBoard, display the change of the training loss value with the number of iterations, and the internal structure of the neural network to better understand the neural network.

One, create a map

   By adding a scalar data and a histogram data to the log file, and then displaying it through TensorBoard, the first step is added to the summary, and the second step is written to the file.

 The generated value of the model is added to the histogram data (the histogram name is z), and the loss function is added to the scalar data (the scalar name is loss_function).

The following code is to create a summary_writer after starting the session, run the summary value in the iteration, and save it in the file

   code show as below:

# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time    : 2019/5/16 9:47
# @Author  : xhh
# @Desc    :  线性回归的TensorBoard
# @File    : tensor_tensorBoard.py
# @Software: PyCharm

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

plotdata = {"batchsize":[], "loss":[]}

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

#  模拟数据
train_X = np.linspace(-1, 1, 100)
train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.3  # 加入了噪声

# 图形展示
plt.plot(train_X,train_Y,'ro',label="original data") # label数据标签
plt.legend()
plt.show()

tf.reset_default_graph()  # 重置会话

# 创建模型
# 占位符
X = tf.placeholder("float")
Y = tf.placeholder("float")
# 模型参数
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")

# 前向结构
z = tf.multiply(X, W) +b
tf.summary.histogram('z',z)  #将预测值以直方图显示

# 反向优化
cost = tf.reduce_mean(tf.square(Y-z))
tf.summary.scalar('loss_function', cost)  #将损失以标量显示
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# 初始化变量
init = tf.global_variables_initializer()
# 参数设置
training_epochs = 20
display_step = 2
saver = tf.train.Saver() # 模型生成,并保存
savedir = "log/"

# 启动session
with tf.Session() as sess:
    sess.run(init)


    merged_summary_op = tf.summary.merge_all()  # 合并所以summary
    # 创建summary_writer,用于写文件
    summary_writer = tf.summary.FileWriter('log/mnist_with_summaries', sess.graph)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X,train_Y):
            sess.run(optimizer, feed_dict={X:x, Y:y})

            # 生成summary
            summary_str = sess.run(merged_summary_op, feed_dict={X:x, Y:y})
            summary_writer.add_summary(summary_str, epoch) # 将summary写入文件

        # 显示训练中的详细信息
        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!")
        saver.save(sess, savedir+"linermodel.cpkt")
        print("cost=",sess.run(cost, feed_dict={X:train_X, Y:train_Y}),"W=", sess.run(W),"b=",sess.run(b))

        # 图形显示
        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 the final running results, the changes in the fitting linear model during the modeling process and the changes in its loss are posted below:

The three line graphs in the figure below are the changes in the loss function when fitting the model, which are the changes in the 2, 8, and 19 times respectively. The scatter plot is the final fitted model.

 

The following figure is the additional visualization file after running:

Enter the folder, enter cmd, activate your python environment with TensorFlow-gpu version

Then enter:

The address needs to be changed. The port at the back is changed to 8080, and my default 6006 cannot be accessed.

tensorboard --logdir F:\code\tensor_test\log\mnist_with_summaries --port=8080

The results after the final run are as follows:

Then open the Google browser (preferably Google), enter http://localhost:8080 to visit and you can see:

The loss value changes with the number of iterations:

The internal structure of the neural network:

Click SCALARS, and you will see the loss_function created before. After you click it, you can see the change of the loss value with the number of iterations. As shown above.

2. Problems that may arise

(1) Such problems may occur in cmd:

The above is not accessible because there is no corresponding TensorFlow-gpu python.

(2) Problems with TensorFlow-GPU and python environment

Mine is Python==3.6.2 in the figure below, tensorflow-gpu==1.13.1

The python environment is not compatible with the tensorflow-gpu version, so the following problems occur, it is best to change the python environment to python=3.6.7, and install TensorFlow-gpu=1.13.1, and then run successfully

(3) Access issues

When using 127.0.0.1:6006, access is denied when accessing, as follows:

It is because the default ip address of the machine is localhost, so it can't be accessed. It can be accessed by changing to localhost.

It may appear that port 6006 cannot be accessed. At this time, you need to specify IP=8080 on the back, and then the access is OK.

 

You can pay attention to the official account of my friend and me~~~ Here are some python technical information that my friend and I update from time to time! ! You can also leave a message to discuss technical issues. I hope you can support and pay attention to it. Thank you~~

 

 

 

 

 

 

       

  

Guess you like

Origin blog.csdn.net/weixin_39121325/article/details/90257354