Tensorflow-constructing a neural network

1 Introduction

This time, the code will show how to build a complete neural network, including adding neural layers, calculating errors, training steps, and judging whether it is learning.

2. Construct a neural network

2.1. Import module

import tensorflow as tf
import numpy as np

2.2. Construct a function to add a neural layer

def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))   #    [in_size, out_size]: 输出张量的形状 mean: 正态分布的均值,默认为0,stddev: 正态分布的标准差,默认为1.0
    biases = tf.Variable(tf.zeros([1,out_size])+0.1)
    Wx_plus_b = tf.matmul(inputs,Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

2.3. Constructing data

Build the required data. Here, x_data and y_data are not strictly related to the unary quadratic function, because we added a noise, so that it will look more like the real situation.

x_data = np.linspace(-1,1,300)[:,np.newaxis]    #300行1列
noise = np.random.normal(0,0.05,x_data.shape)      #np.random.normal参数:均值、方差、输出的形状
y_data = np.square(x_data) - 0.5 + noise   #np.square(x):计算数组各元素的平方

Use placeholders to define the input of the neural network we need. tf.placeholder () is a placeholder. None here means that no matter how many inputs there are, because the input has only one feature, so here is 1.

xs = tf.placeholder(tf.float32,[None,1])   #此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值
ys = tf.placeholder(tf.float32,[None,1])    #[None, 1]表示列是1,行不定

Next, we can start to define the neural layer. Usually the neural layer includes the input layer, hidden layer and output layer. The input layer here has only one attribute, so we have only one input; we can assume the hidden layer ourselves, here we assume that the hidden layer has 10 neurons; the structure of the output layer and the input layer is the same, so our output layer There is only one floor. Therefore, we build a neural network with 1 input layer, 10 hidden layers, and 1 output layer.

2.4. Build a network

Next, we begin to define the hidden layer, using the previous add_layer () function, here using the excitation function tf.nn.relu that comes with Tensorflow.

Next, define the output layer. The input at this time is the output of the hidden layer-l1, the input has 10 layers (the output layer of the hidden layer), and the output has 1 layer.

#搭建网络
l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None)

Calculate the error between the predicted value and the true value, and then sum the square of the difference between the two to average.

loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))   

Next, it is a crucial step, how to make machine learning improve its accuracy. The value in tf.train.GradientDescentOptimizer () is usually less than 1, which is 0.1 here, which represents the efficiency of 0.1 to minimize the error loss.

train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)  #对所有步骤中的所有变量使用恒定的学习率

When using a variable, it must be initialized, which is essential

init = tf.global_variables_initializer()

Define Session and use Session to perform init initialization steps. (Note: In tensorflow, only session.run () will perform the operations we defined.)

sess = tf.Session()
sess.run(init)

2.5. Training

Next, let the machine start learning.

For example, here, we let the machine learn 1000 times. The content of machine learning is train_step, use Session to run the data of each training, and gradually improve the prediction accuracy of the neural network. (Note: When the placeholder is used for the operation, the dictionary feed_dict is required to specify the input.)

for i in range(1000):
    sess.run(train_step, feed_dict={xs:x_data,ys:y_data})
    if i%50==0:
        print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))

Insert picture description here

Published 227 original articles · praised 633 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105512017