利用TensorFlow实现《神经网络与机器学习》一书中4.7模式分类练习
具体问题是将如下图所示双月牙数据集分类。
使用到的工具:
python3.5 tensorflow1.2.1 numpy matplotlib
1.产生双月环数据集
def produceData(r,w,d,num): r1 = r-w/2 r2 = r+w/2 #上半圆 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) #类别标签为1 #下半圆 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) #类别标签为-1,注意:由于采取双曲正切函数作为激活函数,类别标签不能为0 #合并 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
其中r为月环半径,w为月环宽度,d为上下月环距离(与书中一致)
2.利用TensorFlow搭建神经网络模型
2.1 神经网络层添加
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 利用tensorflow建立神经网络模型
输入层大小:2
隐藏层大小:20
输出层大小:1
激活函数:双曲正切函数
学习率:0.1(与书中略有不同)
(具体的搭建过程可参考莫烦的视频,链接就不附上了自行搜索吧......)
###define placeholder for inputs to network xs = tf.placeholder(tf.float32, [None, 2]) ys = tf.placeholder(tf.float32, [None, 1]) ###添加隐藏层 l1 = add_layer("layer1",xs, 2, 20, activation_function=tf.tanh) ###添加输出层 prediction = add_layer("layer2",l1, 20, 1, activation_function=tf.tanh) ###MSE 均方误差 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1])) ###优化器选取 学习率设置 此处学习率置为0.1 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) ###tensorflow变量初始化,打开会话 init = tf.global_variables_initializer()#tensorflow更新后初始化所有变量不再用tf.initialize_all_variables() sess = tf.Session() sess.run(init)
新闻热点
疑难解答