Use tensorflow, tensorboard of

Based tensorflow of a quadratic equation regression prediction:

0. examination tensorflow environment

import tensorflow as tf
hello = tf.constant('hello world')
session = tf.Session()
session.run(hello)

operation result:
Here Insert Picture Description

1. Prepare data

import numpy as np

X = np.linspace(-1, 1, 300)[:, np.newaxis].astype('float32')#变量a为1维的ndarray对象时,a[:, np.newaxis]与a.reshape(-1, 1)相同。-1代表可以随便取。np.linspace(-1, 1, 300)表示-1到1等间距点取300个。
noise = np.random.normal(0, 0.05, X.shape).astype('float32')#np.random.normal方法初始化符合正态分布的点,第1个参数是正态分布的均值,第2个参数是正态分布的方差,第3个参数是返回值的shape,返回值的数据类型为ndarray对象。
y = np.square(X) - 0.5 + noise#np.square方法对X中的每一个值求平方,- 0.5使用了ndarray对象的广播特性,最后加上噪声noise,将计算结果赋值给变量y。

2. build neural networks

import tensorflow as tf 

Weights_1 = tf.Variable(tf.random_normal([1, 10]))#定义Weights,它的形状是连接上一层神经元的个数*连接下一层神经元的个数。
biases_1 = tf.Variable(tf.zeros([1, 10]) + 0.1)#定义biases,它是二维矩阵,行数一直为1,列数为连接下一层神经元的个数,即它的形状为1*连接下一层神经元的个数;
Wx_plus_b_1 = tf.matmul(X, Weights_1) + biases_1#表示wx+b的计算结果
outputs_1 = tf.nn.relu(Wx_plus_b_1)#表示在第1个连接的输出结果,经过激活函数relu得出
#上面4行定义神经网络中的输入层到第1隐层的连接
Weights_2 = tf.Variable(tf.random_normal([10, 1]))
biases_2 = tf.Variable(tf.zeros([1, 1]) + 0.1)
Wx_plus_b_2 = tf.matmul(outputs_1, Weights_2) + biases_2
outputs_2 = Wx_plus_b_2#表示在第2个连接的输出结果,因为此连接的下一层是输出层,所以不需要激活函数
#上面4行定义神经网络中的第1隐层到输出层的连接

loss = tf.reduce_mean(tf.square(y - outputs_2))#定义损失函数,等同于回归预测中的MSE,中文叫做均方误差
optimizer = tf.train.AdamOptimizer(0.1)#调用tf.train库中的AdamOptimizer方法实例化优化器对象
train = optimizer.minimize(loss)#调用优化器的minimize方法定义训练方式,参数为损失函数。方法的返回结果赋值给变量train

3. variable initialization

#对于神经网络模型,重要是其中的W、b这两个参数。开始神经网络模型训练之前,这两个变量需要初始化
init = tf.global_variables_initializer()#tf.global_variables_initializer实例化tensorflow中的Operation对象
session = tf.Session()#调用tf.Session方法实例化会话对象
session.run(init)#调用tf.Session对象的run方法做变量初始化

4. Model Training

#模型训练200次,每运行1次代码session.run(train)则模型训练1次。在训练次数为20的整数倍时,打印训练步数、loss值
for step in range(201):
    session.run(train)
    if step % 20 == 0:
        print(step, 'loss:', session.run(loss))

operation result:

0 loss: 0.040248208
20 loss: 0.011066726
40 loss: 0.004511993
60 loss: 0.0028382281
80 loss: 0.0026794705
100 loss: 0.0026217068
120 loss: 0.0026041507
140 loss: 0.0025921084
160 loss: 0.002581141
180 loss: 0.0025619685
200 loss: 0.0025474937

Here Insert Picture Description
**************************************************************************

Model-based training tensorboard visualization:

0. Data Preparation

In the desktop create a new folder named tensorflowtest, cmd open jupyter notebook, newly named tensorflowTest.ipynb
Here Insert Picture Description

#检测tensorflow环境
import tensorflow as tf
hello = tf.constant('hello world')
session = tf.Session()
session.run(hello)
import tensorflow as tf
import numpy as np

batch_size = 100
X_data = np.linspace(-1, 1, 300).reshape(-1, 1).astype('float32')
noise = np.random.normal(0, 0.1, X_data.shape).astype('float32')
y_data = np.square(X_data) - 0.5 + noise
with tf.name_scope('inputs'):#scope中文叫做适用范围,每定义一个tf.name_scope,在tensorboard界面的graph中会有一个节点
    X_holder = tf.placeholder(tf.float32, name='input_X')#tf.placeholder方法的第1个参数是tensorflow中的数据类型;第2个关键字参数name的数据类型是字符串,是在tensorboard界面中的显示名
    y_holder = tf.placeholder(tf.float32, name='input_y')

1. build a neural network

AddConnect defined function, the role of adding a layer connections.
Since the neural network has a total of three layers: the input layer, hidden layer and output layer, it is necessary to add a function to call the secondary addConnect layer 2 connection.
addConnect first parameter is a function of the input matrix, the second parameter is the number of columns of the input matrix, the third parameter is the number of columns of the output matrix, the four parameters are used to define the connection layers, the fifth parameter is activation function.
Finally, line 6 defines loss function, the optimizer, the training process.

def addConnect(inputs, in_size, out_size, n_connect, activation_function=None):
   connect_name = 'connect%s' %n_connect
   with tf.name_scope(connect_name):
       with tf.name_scope('Weights'):
           Weights = tf.Variable(tf.random_normal([in_size, out_size]) ,name='W')
           tf.summary.histogram(connect_name + '/Weights', Weights)
       with tf.name_scope('biases'):
           biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
           tf.summary.histogram(connect_name + '/biases', biases)
       with tf.name_scope('Wx_plus_b'):
           Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
       if activation_function is None:
           return Wx_plus_b
       else:
           return activation_function(Wx_plus_b)
       
connect_1 = addConnect(X_holder, 1, 10, 1, tf.nn.relu)
predict_y = addConnect(connect_1, 10, 1, 2)
with tf.name_scope('loss'):
   loss = tf.reduce_mean(tf.square(y_holder - predict_y))
   tf.summary.scalar('loss', loss)
with tf.name_scope('train'):
   optimizer = tf.train.AdamOptimizer(0.1)
   train = optimizer.minimize(loss)

2. Variable Initialization

init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)

3. Model Training

import datetime #导入datetime库获取当前时间,从而给文件夹命名
import random

nowTime = datetime.datetime.now()
timestamp = nowTime.strftime('%m%d%H%M%S')
write = tf.summary.FileWriter('logs'+timestamp, session.graph)
#上面三行给新建的日志文件夹命名
merge_all = tf.summary.merge_all()#将绘制标量曲线图SCALARS、变量曲线图DISTRIBUTIONS、变量分布直方图HISTOGRAMS的任务合并交给变量merge_all

for i in range(201):
    select_index = random.sample(range(len(X_data)), k=batch_size)#使用random.sample方法选取数量为batch_size的样本来训练
    select_X = X_data[select_index]
    select_y = y_data[select_index]
    #在200次训练迭代中,第10、11、12行代码选取数量为batch_size的样本来训练
    session.run(train, feed_dict={X_holder:select_X, y_holder:select_y})#每运行1次,即神经网络训练1次
    merged = session.run(merge_all, feed_dict={X_holder:select_X, y_holder:select_y})#获得每次训练后loss、Weights、biases值;
    write.add_summary(merged, i)#把loss、Weights、biases值写入日志文件中

Generating a log file, the log file into the folder and then point cmd, type: tensorboard --logdir ./
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
this time in the browser or enter 2localhost:6006

Visualization of model training 4.tensorboard

Graph below shows the change in the loss:
Here Insert Picture Description
neural network architecture diagram below shows:
Here Insert Picture Description
change graph below shows the weights and offset biases Weights: The
Here Insert Picture Description
histogram below shows the weights and offset biases of Weights:
Here Insert Picture Description
binding matplotlib visual comparison overtaken :Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43435675/article/details/88636622