Tensorflow create a neural network

 
A neural network system that consists of many layers, an input layer for receiving information, processing the input information of the intermediate layer, the output layer is the computer's perception of the input information.

https://www.jianshu.com/p/e112012a4b2d

 The basic process to build neural networks

Neural layer function defined added

1. Training data
2. Define node is ready to receive data
3. Define Neural Layer: hidden layer and the prediction layer
4. Define loss expression
5. Select optimizer makes minimal loss

Then all the variables are initialized by sess.run optimizer, 1000 learning iterations:


import tensorflow as tf
import numpy as np

def add_layer(inputs,in_size,out_size,activation_fuction = None):
    
    Weight = tf.Variable(tf.random.normal([in_size,out_size]))
    biases  = tf.Variable(tf.zeros([1,out_size])+0.1)    
    wx = tf.matmul(inputs,Weight)+biases
    
    if activation_fuction is None:
        output = wx
    else :
        output = activation_fuction(wx)
    return output 



x_data = np.linspace (-1,1,300 ) [:, np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data)-0.5+noise

xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
hidden = add_layer(xs,1,10,activation_fuction =tf.nn.relu)
prediction = add_layer(hidden,10,1,activation_fuction = None)

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

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for I in Range (1000 ):
 #    # train_step Training and loss are defined by a placeholder operation, the incoming feed parameters are used here 
        sess.run (Train, feed_dict = {XS: x_data, YS: y_data})
         IF I == 50% 0:
             Print (sess.run (Loss, feed_dict = {XS: x_data, YS: y_data}))
    

 



Guess you like

Origin www.cnblogs.com/gaona666/p/12632897.html