日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
TensorFlow學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)的構(gòu)建

1.建立一個(gè)神經(jīng)網(wǎng)絡(luò)添加層

站在用戶的角度思考問題,與客戶深入溝通,找到類烏齊網(wǎng)站設(shè)計(jì)與類烏齊網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋類烏齊地區(qū)。

輸入值、輸入的大小、輸出的大小和激勵(lì)函數(shù)

學(xué)過神經(jīng)網(wǎng)絡(luò)的人看下面這個(gè)圖就明白了,不懂的去看看我的另一篇博客(http://www.cnblogs.com/wjy-lulu/p/6547542.html) 

 
 
 
  1. def add_layer(inputs , in_size , out_size , activate = None): 
  2.     Weights = tf.Variable(tf.random_normal([in_size,out_size]))#隨機(jī)初始化 
  3.     baises  = tf.Variable(tf.zeros([1,out_size])+0.1)#可以隨機(jī)但是不要初始化為0,都為固定值比隨機(jī)好點(diǎn) 
  4.     y = tf.matmul(inputs, Weights) + baises #matmul:矩陣乘法,multipy:一般是數(shù)量的乘法 
  5.     if activate: 
  6.         y = activate(y) 
  7.     return y  

2.訓(xùn)練一個(gè)二次函數(shù) 

 
 
 
  1. import tensorflow as tf 
  2. import numpy as np 
  3.  
  4. def add_layer(inputs , in_size , out_size , activate = None): 
  5.     Weights = tf.Variable(tf.random_normal([in_size,out_size]))#隨機(jī)初始化 
  6.     baises  = tf.Variable(tf.zeros([1,out_size])+0.1)#可以隨機(jī)但是不要初始化為0,都為固定值比隨機(jī)好點(diǎn) 
  7.     y = tf.matmul(inputs, Weights) + baises #matmul:矩陣乘法,multipy:一般是數(shù)量的乘法 
  8.     if activate: 
  9.         y = activate(y) 
  10.     return y 
  11. if __name__ == '__main__': 
  12.     x_data = np.linspace(-1,1,300,dtype=np.float32)[:,np.newaxis]#創(chuàng)建-1,1的300個(gè)數(shù),此時(shí)為一維矩陣,后面轉(zhuǎn)化為二維矩陣===[1,2,3]-->>[[1,2,3]] 
  13.     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)#噪聲是(1,300)格式,0-0.05大小 
  14.     y_data = np.square(x_data) - 0.5 + noise #帶有噪聲的拋物線 
  15.  
  16.     xs = tf.placeholder(tf.float32,[None,1]) #外界輸入數(shù)據(jù) 
  17.     ys = tf.placeholder(tf.float32,[None,1]) 
  18.  
  19.     l1 = add_layer(xs,1,10,activate=tf.nn.relu) 
  20.     prediction = add_layer(l1,10,1,activate=None) 
  21.  
  22.     loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))#誤差 
  23.     train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#對誤差進(jìn)行梯度優(yōu)化,步伐為0.1 
  24.  
  25.     sess = tf.Session() 
  26.     sess.run( tf.global_variables_initializer()) 
  27.     for i in range(1000): 
  28.         sess.run(train_step, feed_dict={xs: x_data, ys: y_data})#訓(xùn)練 
  29.         if i%50 == 0: 
  30.             print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))#查看誤差  

3.動(dòng)態(tài)顯示訓(xùn)練過程

顯示的步驟程序之中部分進(jìn)行說明,其它說明請看其它博客(http://www.cnblogs.com/wjy-lulu/p/7735987.html) 

 
 
 
  1. import tensorflow as tf 
  2. import numpy as np 
  3. import matplotlib.pyplot as plt 
  4.  
  5. def add_layer(inputs , in_size , out_size , activate = None): 
  6.     Weights = tf.Variable(tf.random_normal([in_size,out_size]))#隨機(jī)初始化 
  7.     baises  = tf.Variable(tf.zeros([1,out_size])+0.1)#可以隨機(jī)但是不要初始化為0,都為固定值比隨機(jī)好點(diǎn) 
  8.     y = tf.matmul(inputs, Weights) + baises #matmul:矩陣乘法,multipy:一般是數(shù)量的乘法 
  9.     if activate: 
  10.         y = activate(y) 
  11.     return y 
  12. if __name__ == '__main__': 
  13.     x_data = np.linspace(-1,1,300,dtype=np.float32)[:,np.newaxis]#創(chuàng)建-1,1的300個(gè)數(shù),此時(shí)為一維矩陣,后面轉(zhuǎn)化為二維矩陣===[1,2,3]-->>[[1,2,3]] 
  14.     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)#噪聲是(1,300)格式,0-0.05大小 
  15.     y_data = np.square(x_data) - 0.5 + noise #帶有噪聲的拋物線 
  16.     fig = plt.figure('show_data')# figure("data")指定圖表名稱 
  17.     ax = fig.add_subplot(111) 
  18.     ax.scatter(x_data,y_data) 
  19.     plt.ion() 
  20.     plt.show() 
  21.     xs = tf.placeholder(tf.float32,[None,1]) #外界輸入數(shù)據(jù) 
  22.     ys = tf.placeholder(tf.float32,[None,1]) 
  23.  
  24.     l1 = add_layer(xs,1,10,activate=tf.nn.relu) 
  25.     prediction = add_layer(l1,10,1,activate=None) 
  26.  
  27.     loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))#誤差 
  28.     train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#對誤差進(jìn)行梯度優(yōu)化,步伐為0.1 
  29.  
  30.     sess = tf.Session() 
  31.     sess.run( tf.global_variables_initializer()) 
  32.     for i in range(1000): 
  33.         sess.run(train_step, feed_dict={xs: x_data, ys: y_data})#訓(xùn)練 
  34.         if i%50 == 0: 
  35.             try: 
  36.                 ax.lines.remove(lines[0]) 
  37.             except Exception: 
  38.                 pass 
  39.             prediction_value = sess.run(prediction, feed_dict={xs: x_data}) 
  40.             lines = ax.plot(x_data,prediction_value,"r",lw = 3) 
  41.             print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))#查看誤差 
  42.             plt.pause(2) 
  43.     while True: 
  44.         plt.pause(0.01)   

4.TensorBoard整體結(jié)構(gòu)化顯示

A.利用with tf.name_scope("name")創(chuàng)建大結(jié)構(gòu)、利用函數(shù)的name="name"去創(chuàng)建小結(jié)構(gòu):tf.placeholder(tf.float32,[None,1],name="x_data")

B.利用writer = tf.summary.FileWriter("G:/test/",graph=sess.graph)創(chuàng)建一個(gè)graph文件

C.利用TessorBoard去執(zhí)行這個(gè)文件

這里得注意--->>>首先到你存放文件的上一個(gè)目錄--->>然后再去運(yùn)行這個(gè)文件

tensorboard  --logdir=test

(被屏蔽的GIF動(dòng)圖,具體安裝操作歡迎戳“原文鏈接”哈!)

5.TensorBoard局部結(jié)構(gòu)化顯示  

A. tf.summary.histogram(layer_name+"Weight",Weights):直方圖顯示

     

B.  tf.summary.scalar("Loss",loss):折線圖顯示,loss的走向決定你的網(wǎng)絡(luò)訓(xùn)練的好壞,至關(guān)重要一點(diǎn)

C.初始化與運(yùn)行設(shè)定的圖表 

 
 
 
  1. merge = tf.summary.merge_all()#合并圖表2 writer = tf.summary.FileWriter("G:/test/",graph=sess.graph)#寫進(jìn)文件3 result = sess.run(merge,feed_dict={xs:x_data,ys:y_data})#運(yùn)行打包的圖表merge4 writer.add_summary(result,i)#寫入文件,并且單步長50  

 完整代碼及顯示效果: 

 
 
 
  1. import tensorflow as tf 
  2. import numpy as np 
  3. import matplotlib.pyplot as plt 
  4.  
  5. def add_layer(inputs , in_size , out_size , n_layer = 1 , activate = None): 
  6.     layer_name = "layer" + str(n_layer) 
  7.     with tf.name_scope(layer_name): 
  8.         with tf.name_scope("Weights"): 
  9.             Weights = tf.Variable(tf.random_normal([in_size,out_size]),name="W")#隨機(jī)初始化 
  10.             tf.summary.histogram(layer_name+"Weight",Weights) 
  11.         with tf.name_scope("Baises"): 
  12.             baises  = tf.Variable(tf.zeros([1,out_size])+0.1,name="B")#可以隨機(jī)但是不要初始化為0,都為固定值比隨機(jī)好點(diǎn) 
  13.             tf.summary.histogram(layer_name+"Baises",baises) 
  14.         y = tf.matmul(inputs, Weights) + baises #matmul:矩陣乘法,multipy:一般是數(shù)量的乘法 
  15.         if activate: 
  16.             y = activate(y) 
  17.         tf.summary.histogram(layer_name+"y_sum",y) 
  18.         return y 
  19. if __name__ == '__main__': 
  20.     x_data = np.linspace(-1,1,300,dtype=np.float32)[:,np.newaxis]#創(chuàng)建-1,1的300個(gè)數(shù),此時(shí)為一維矩陣,后面轉(zhuǎn)化為二維矩陣===[1,2,3]-->>[[1,2,3]] 
  21.     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)#噪聲是(1,300)格式,0-0.05大小 
  22.     y_data = np.square(x_data) - 0.5 + noise #帶有噪聲的拋物線 
  23.     fig = plt.figure('show_data')# figure("data")指定圖表名稱 
  24.     ax = fig.add_subplot(111) 
  25.     ax.scatter(x_data,y_data) 
  26.     plt.ion() 
  27.     plt.show() 
  28.     with tf.name_scope("inputs"): 
  29.         xs = tf.placeholder(tf.float32,[None,1],name="x_data") #外界輸入數(shù)據(jù) 
  30.         ys = tf.placeholder(tf.float32,[None,1],name="y_data") 
  31.     l1 = add_layer(xs,1,10,n_layer=1,activate=tf.nn.relu) 
  32.     prediction = add_layer(l1,10,1,n_layer=2,activate=None) 
  33.     with tf.name_scope("loss"): 
  34.         loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))#誤差 
  35.         tf.summary.scalar("Loss",loss) 
  36.     with tf.name_scope("train_step"): 
  37.         train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#對誤差進(jìn)行梯度優(yōu)化,步伐為0.1 
  38.  
  39.     sess = tf.Session() 
  40.     merge = tf.summary.merge_all()#合并 
  41.     writer = tf.summary.FileWriter("G:/test/",graph=sess.graph) 
  42.     sess.run( tf.global_variables_initializer()) 
  43.     for i in range(1000): 
  44.         sess.run(train_step, feed_dict={xs: x_data, ys: y_data})#訓(xùn)練 
  45.         if i%100 == 0: 
  46.             result = sess.run(merge,feed_dict={xs:x_data,ys:y_data})#運(yùn)行打包的圖表merge 
  47.             writer.add_summary(result,i)#寫入文件,并且單步長50 
     

主要參考莫凡大大:https://morvanzhou.github.io/

可視化出現(xiàn)問題了,參考這位大神:http://blog.csdn.net/fengying2016/article/details/54289931


當(dāng)前名稱:TensorFlow學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)的構(gòu)建
文章地址:http://www.dlmjj.cn/article/dpihooi.html