国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用TensorFlow訓練簡單的二分類神經網絡模型的方法

2020-02-22 23:20:54
字體:
來源:轉載
供稿:網友

利用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建立神經網絡模型

輸入層大?。?

隱藏層大小:20

輸出層大?。?

激活函數:雙曲正切函數

學習率: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)             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 两当县| 黑山县| 安化县| 长葛市| 屏东市| 灌南县| 襄城县| 丰宁| 平原县| 泰州市| 禄丰县| 云和县| 醴陵市| 思茅市| 华阴市| 喜德县| 西乌珠穆沁旗| 阿拉善右旗| 绵阳市| 庆云县| 苏尼特左旗| 沁水县| 五大连池市| 锡林郭勒盟| 高雄市| 新龙县| 佳木斯市| 临猗县| 万宁市| 汾西县| 天长市| 秭归县| 锡林郭勒盟| 舟曲县| 吉木乃县| 门源| 苍梧县| 龙游县| 沙雅县| 上饶市| 海兴县|