TensorFlow is used to train a simple binary classification neural network model.
Use TensorFlow to implement the 4.7 pattern classification exercise in neural networks and machine learning
The specific problem is to classify the dual-Crescent dataset as shown in.
Tools used:
Python3.5 tensorflow1.2.1 numpy matplotlib
1. Generate a two-month Dataset
Def produceData (r, w, d, num): r1 = r-w/2 r2 = r + w/2 # upper half circle theta1 = np. random. uniform (0, np. pi, num) X_Col1 = np. random. uniform (r1 * np. cos (theta1), r2 * np. cos (theta1), num) [:, np. newaxis] X_Row1 = np. random. uniform (r1 * np. sin (theta1), r2 * np. sin (theta1), num) [:, np. newaxis] Y_label1 = np. ones (num) # category label is 1 # lower half circle theta2 = np. random. uniform (-np. pi, 0, num) X_Col2 = (np. random. uniform (r1 * np. cos (theta2), r2 * np. cos (theta2), num) + r) [:, np. newaxis] X_Row2 = (np. random. uniform (r1 * np. sin (theta2), r2 * np. sin (theta2), num)-d) [:, np. newaxis] Y_label2 =-np. ones (num) # The category label is-1. Note: Because the hyperbolic tangent function is used as the activation function, the category label cannot be 0 # merge X_Col = np. vstack (X_Col1, X_Col2) X_Row = np. vstack (X_Row1, X_Row2) X = np. hstack (X_Col, X_Row) Y_label = np. hstack (Y_label1, Y_label2) Y_label.shape = (num * 2, 1) return X, Y_label
Here, r is the radius of the ring, w is the width of the ring, and d is the distance between the upper and lower rings (consistent with the book)
2. Use TensorFlow to build a Neural Network Model
2.1 Neural Network Layer Addition
def add_layer(layername,inputs, in_size, out_size, activation_function=None): # add one more layer and return the output of this layer with tf.variable_scope(layername,reuse=None): Weights = tf.get_variable("weights",shape=[in_size, out_size], initializer=tf.truncated_normal_initializer(stddev=0.1)) biases = tf.get_variable("biases", shape=[1, out_size], initializer=tf.truncated_normal_initializer(stddev=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.2 using tensorflow to establish a Neural Network Model
Input layer size: 2
Hidden Layer size: 20
Output layer size: 1
Activation function: hyperbolic tangent function
Learning rate: 0.1 (slightly different from the book)
(For details about the construction process, refer to the boring video. The link will not be attached with self-search ......)
### Define placeholder for inputs to network xs = tf. placeholder (tf. float32, [None, 2]) ys = tf. placeholder (tf. float32, [None, 1]) ### Add a hidden layer l1 = add_layer ("layer1", xs, 2, 20, activation_function = tf. tanh) ### Add the output Layer prediction = add_layer ("layer2", l1, 20, 1, activation_function = tf. tanh) ### MSE mean square error loss = tf. performance_mean (tf. performance_sum (tf. square (ys-prediction), reduction_indices = [1]) ### set the optimizer's learning rate to 0.1 train_step = tf. train. gradientDescentOptimizer (0.1 ). minimize (loss) ### tensorflow variable initialization, enable session init = tf. global_variables_initializer () # tf is no longer used to initialize all variables after tensorflow is updated. initialize_all_variables () sess = tf. session () sess. run (init)
2.3 Training Model
### Training 2000 times for I in range (2000): sess. run (train_step, feed_dict = {xs: x_data, ys: y_label })
3. Search for classification decision boundaries using trained network models
3.1 generate two-dimensional random points
def produce_random_data(r,w,d,num): X1 = np.random.uniform(-r-w/2,2*r+w/2, num) X2 = np.random.uniform(-r - w / 2-d, r+w/2, num) X = np.vstack((X1, X2)) return X.transpose()
3.2 use a trained model to collect points near decision boundaries
Input a two-dimensional random point to the network. If the calculated output value is greater than-0.5 and less than 0.5, the point falls near the decision boundary (hyperbolic tangent function)
def collect_boundary_data(v_xs): global prediction X = np.empty([1,2]) X = list() for i in range(len(v_xs)): x_input = v_xs[i] x_input.shape = [1,2] y_pre = sess.run(prediction, feed_dict={xs: x_input}) if abs(y_pre - 0) < 0.5: X.append(v_xs[i]) return np.array(X)
3.3 Use numpy tool to combine decision-making boundary curves near the collected boundary, and use matplotlib. pyplot to draw a picture
### Generate space random data X_NUM = produce_random_data (10, 6,-4, 5000) ### Border Data Sampling X_ B = collect_boundary_data (X_NUM) ### plot data fig = plt. figure () ax = fig. add_subplot (1, 1, 1) ### set the axis name plt. xlabel ('x1') plt. ylabel ('x2 ') ax. scatter (x_data [:, 0], x_data [:, 1], marker = 'X ') ### use the sampled boundary data to fit the optimal z1 = np of the 7-time boundary curve. polyfit (X_ B [:, 0], X_ B [:, 1], 7) p1 = np. poly1d (z1) x = X_ B [:, 0] x. sort () yvals = p1 (x) plt. plot (x, yvals, 'R', label = 'boundray line') plt. legend (loc = 4) # plt. ion () plt. show ()
4. Results
5. Add the source code Github link.
PatternClassification. py file in the https://github.com/Peakulorain/Practices.git
Another note: Use softmax to solve the classification problem... I just use it to do the exercises in the book.
(The beginner's level is limited. If you have any questions, please point out that you are not a beginner)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.