数学之路(3)-机器学习(3)-机器学习算法-神经网络[19]
生活随笔
收集整理的這篇文章主要介紹了
数学之路(3)-机器学习(3)-机器学习算法-神经网络[19]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
| 鋼包使用次數(shù)與容積實測數(shù)據(jù) | ||
| 使用次數(shù)x | 容積y | ? |
| 2 | 106.42 | ? |
| 3 | 108.2 | ? |
| 4 | 109.58 | ? |
| 5 | 109.5 | ? |
| 7 | 110 | ? |
| 8 | 109.93 | ? |
| 10 | 110.49 | ? |
| 11 | 110.59 | ? |
| 14 | 110.6 | ? |
| 15 | 110.9 | ? |
| 16 | 110.7 | ? |
| 18 | 111 | ? |
| 19 | 111.2 | ? |
本博客所有內(nèi)容是原創(chuàng),如果轉(zhuǎn)載請注明來源
http://blog.csdn.net/u010255642
我們用多層感知器對這組數(shù)據(jù)進行訓練,以使用次數(shù)為輸入,以容積為輸出,建立它們之間的關系。 部分python代碼如下: #!/usr/bin/env python #-*- coding: utf-8 -*- #bp ann import numpy as np import matplotlib.pyplot as plt import random import copyisdebug=False#x和d樣本初始化 #x和d樣本初始化 train_x =[] d=[] f = open("cubage.csv") try: f_text = f.read( ) finally: f.close( ) x_text=f_text.split('\n') for line_i in xrange(0,len(x_text)):line=x_text[line_i]if line_i>1 and len(line)>0:train_x.append([])hdata=line.split(',')train_x[line_i-2].append(float(hdata[0]))d.append([float(hdata[1])])myinput=np.array(train_x) mytarget=np.array(d) mymax=np.max(d) tz=(0.1**(len(str(int(mymax)))))*5 myinput=tz*myinput mytarget=tz*mytarget train_x=myinput d=mytarget ...................... def simulate(myx,sigmoid_func,delta_sigfun):'''一個樣本的仿真計算'''print u"仿真計算中" global ann_yiglobal ann_wglobal ann_wj0global ann_y0global hidelevel_countglobal alllevel_countglobal dglobal mylnwwmyd=d[0]myx=np.array(myx)n=len(myx)#清空yi輸出信號數(shù)組 hidelevel=hidelevel_countalllevel=alllevel_countfor i in xrange(0,alllevel):#第一維是層數(shù),從0開始for j in xrange(0,n):#第二維是神經(jīng)元ann_yi[i][j]=0.0ann_yi=np.array(ann_yi)yi=ann_yi#前向計算myy=np.array([])for nowlevel in xrange(0,alllevel):#一層層向前計算#計算誘導局部域my_y=[]myy=yi[nowlevel-1]myw=ann_w[nowlevel-1] if nowlevel==0:#第一層隱藏層my_y=myxyi[nowlevel]=my_y elif nowlevel==(alllevel-1):#線性輸出層my_y=o_func(yi[nowlevel-1,:len(myd)])yi[nowlevel,:len(myd)]=my_y elif nowlevel==(hidelevel-1):#最后一層隱藏輸出層for i in xrange(0,len(myd)):temp_y=sigmoid_func(np.dot(myw[:,i],myy))my_y.append(temp_y) yi[nowlevel,:len(myd)]=my_y else:#中間隱藏層#中間隱藏層需要加上偏置for i in xrange(0,len(myy)):temp_y=sigmoid_func(np.dot(myw[:,i],myy))my_y.append(temp_y)yi[nowlevel]=my_yif isdebug:print "============="print u"***權(quán)值矩陣***" print ann_wprint u"***輸出矩陣***" print yiprint "============="return yi[alllevel-1,:len(myd)]train()delta_sigfun=ann_delta_atanh sigmoid_func=ann_atanhsimd=[] for xn in xrange(0,len(x)):mysimout=simulate(x[xn],sigmoid_func,delta_sigfun)simd.append(mysimout[0])temp_x=[] temp_d=[] i=0 for mysamp in train_x:temp_x.append(mysamp[0])temp_d.append(d[i][0])i+=1simd=np.array(simd) simd/=tz temp_x=np.array(temp_x) temp_x/=tz temp_d=np.array(temp_d) temp_d/=tz temp_y=simdx_max=max(temp_x) x_min=min(temp_x) y_max=max(temp_y) y_min=min(temp_y)plt.subplot(211) plt.xlabel(u"x") plt.xlim(x_min, x_max) plt.ylabel(u"y") plt.ylim(y_min, y_max) plt.title(u"http://blog.csdn.net/myhaspl" ) lp_x1 = temp_x lp_x2 = temp_y lp_d = temp_d plt.plot(lp_x1, lp_x2, 'r-') plt.plot(lp_x1,lp_d,'b*')errx_max=len(err) errx_min=1 erry_max=max(err)+0.1 erry_min=0. plt.subplot(212) plt.xlabel(u"traincount") plt.xlim(errx_min, errx_max) plt.ylabel(u"mse") plt.ylim(erry_min, erry_max)lp_x1 = xrange(1,len(err)+1) lp_x2 = err plt.plot(lp_x1,lp_x2,'g-') plt.show()
擬合的效果如下:
從效果圖上可以看出,通過多層感知器建立的數(shù)據(jù)模型雖然不能直接得出輸入與輸出之間的確切函數(shù)y=f(x),但通過輸入數(shù)據(jù)進入已經(jīng)訓練好的神經(jīng)網(wǎng)絡,仿真輸出仍能達到相同的效果
?
總結(jié)
以上是生活随笔為你收集整理的数学之路(3)-机器学习(3)-机器学习算法-神经网络[19]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: I/O线程
- 下一篇: 《JavaScript高级程序设计(第四