今天就跟大家聊聊有關(guān)使用python怎么構(gòu)建一個(gè)深度神經(jīng)網(wǎng)絡(luò),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)專注于南部網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供南部營銷型網(wǎng)站建設(shè),南部網(wǎng)站制作、南部網(wǎng)頁設(shè)計(jì)、南部網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造南部網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供南部網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。1、云計(jì)算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動(dòng)化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財(cái)分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
1) 正則化項(xiàng)
2) 調(diào)出中間損失函數(shù)的輸出
3) 構(gòu)建了交叉損失函數(shù)
4) 將訓(xùn)練好的網(wǎng)絡(luò)進(jìn)行保存,并調(diào)用用來測試新數(shù)據(jù)
1 數(shù)據(jù)預(yù)處理
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-12 15:11 # @Author : CC # @File : net_load_data.py from numpy import * import numpy as np import cPickle def load_data(): """載入解壓后的數(shù)據(jù),并讀取""" with open('data/mnist_pkl/mnist.pkl','rb') as f: try: train_data,validation_data,test_data = cPickle.load(f) print " the file open sucessfully" # print train_data[0].shape #(50000,784) # print train_data[1].shape #(50000,) return (train_data,validation_data,test_data) except EOFError: print 'the file open error' return None def data_transform(): """將數(shù)據(jù)轉(zhuǎn)化為計(jì)算格式""" t_d,va_d,te_d = load_data() # print t_d[0].shape # (50000,784) # print te_d[0].shape # (10000,784) # print va_d[0].shape # (10000,784) # n1 = [np.reshape(x,784,1) for x in t_d[0]] # 將5萬個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),逐個(gè)排列 n = [np.reshape(x, (784, 1)) for x in t_d[0]] # 將5萬個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),逐個(gè)排列 # print 'n1',n1[0].shape # print 'n',n[0].shape m = [vectors(y) for y in t_d[1]] # 將5萬標(biāo)簽(50000,1)化為(10,50000) train_data = zip(n,m) # 將數(shù)據(jù)與標(biāo)簽打包成元組形式 n = [np.reshape(x, (784, 1)) for x in va_d[0]] # 將5萬個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),排列 validation_data = zip(n,va_d[1]) # 沒有將標(biāo)簽數(shù)據(jù)矢量化 n = [np.reshape(x, (784, 1)) for x in te_d[0]] # 將5萬個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),排列 test_data = zip(n, te_d[1]) # 沒有將標(biāo)簽數(shù)據(jù)矢量化 # print train_data[0][0].shape #(784,) # print "len(train_data[0])",len(train_data[0]) #2 # print "len(train_data[100])",len(train_data[100]) #2 # print "len(train_data[0][0])", len(train_data[0][0]) #784 # print "train_data[0][0].shape", train_data[0][0].shape #(784,1) # print "len(train_data)", len(train_data) #50000 # print train_data[0][1].shape #(10,1) # print test_data[0][1] # 7 return (train_data,validation_data,test_data) def vectors(y): "賦予標(biāo)簽" label = np.zeros((10,1)) label[y] = 1.0 #浮點(diǎn)計(jì)算 return label
2 網(wǎng)絡(luò)定義和訓(xùn)練
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-28 10:18 # @Author : CC # @File : net_network2.py from numpy import * import numpy as np import operator import json # import sys class QuadraticCost(): """定義二次代價(jià)函數(shù)類的方法""" @staticmethod def fn(a,y): cost = 0.5*np.linalg.norm(a-y)**2 return cost @staticmethod def delta(z,a,y): delta = (a-y)*sig_derivate(z) return delta class CrossEntroyCost(): """定義交叉熵函數(shù)類的方法""" @staticmethod def fn(a, y): cost = np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a))) # not a number---0, inf---larger number return cost @staticmethod def delta(z, a, y): delta = (a - y) return delta class Network(object): """定義網(wǎng)絡(luò)結(jié)構(gòu)和方法""" def __init__(self,sizes,cost): self.num_layer = len(sizes) self.sizes = sizes self.cost = cost # print "self.cost.__name__:",self.cost.__name__ # CrossEntropyCost self.default_weight_initializer() def default_weight_initializer(self): """權(quán)值初始化""" self.bias = [np.random.rand(x, 1) for x in self.sizes[1:]] self.weight = [np.random.randn(y, x)/float(np.sqrt(x)) for (x, y) in zip(self.sizes[:-1], self.sizes[1:])] def large_weight_initializer(self): """權(quán)值另一種初始化""" self.bias = [np.random.rand(x, 1) for x in self.sizes[1:]] self.weight = [np.random.randn(y, x) for x, y in zip(self.sizes[:-1], self.sizes[1:])] def forward(self,a): """forward the network""" for w,b in zip(self.weight,self.bias): a=sigmoid(np.dot(w,a)+b) return a def SGD(self,train_data,min_batch_size,epochs,eta,test_data=False, lambd = 0, monitor_train_cost = False, monitor_train_accuracy = False, monitor_test_cost=False, monitor_test_accuracy=False ): """1)Set the train_data,shuffle; 2) loop the epoches, 3) set the min_batches,and rule of update""" if test_data: n_test=len(test_data) n = len(train_data) for i in xrange(epochs): random.shuffle(train_data) min_batches = [train_data[k:k+min_batch_size] for k in xrange(0,n,min_batch_size)] for min_batch in min_batches: # 每次提取一個(gè)批次的樣本 self.update_minbatch_parameter(min_batch,eta,lambd,n) train_cost = [] if monitor_train_cost: cost1 = self.total_cost(train_data,lambd,cont=False) train_cost.append(cost1) print "epoche {0},train_cost: {1}".format(i,cost1) if monitor_train_accuracy: accuracy = self.accuracy(train_data,cont=True) train_cost.append(accuracy) print "epoche {0}/{1},train_accuracy: {2}".format(i,epochs,accuracy) test_cost = [] if monitor_test_cost: cost1 = self.total_cost(test_data,lambd) test_cost.append(cost1) print "epoche {0},test_cost: {1}".format(i,cost1) test_accuracy = [] if monitor_test_accuracy: accuracy = self.accuracy(test_data) test_cost.append(accuracy) print "epoche:{0}/{1},test_accuracy:{2}".format(i,epochs,accuracy) self.save(filename= "net_save") #保存網(wǎng)絡(luò)網(wǎng)絡(luò)參數(shù) def total_cost(self,train_data,lambd,cont=True): cost1 = 0.0 for x,y in train_data: a = self.forward(x) if cont: y = vectors(y) #將測試樣本標(biāo)簽化為矩陣 cost1 += (self.cost).fn(a,y)/len(train_data) cost1 += lambd/len(train_data)*np.sum(np.linalg.norm(weight)**2 for weight in self.weight) #加上權(quán)值項(xiàng) return cost1 def accuracy(self,train_data,cont=False): if cont: output1 = [(np.argmax(self.forward(x)),np.argmax(y)) for (x,y) in train_data] else: output1 = [(np.argmax(self.forward(x)), y) for (x, y) in train_data] return sum(int(out1 == y) for (out1, y) in output1) def update_minbatch_parameter(self,min_batch, eta,lambd,n): """1) determine the weight and bias 2) calculate the the delta 3) update the data """ able_b = [np.zeros(b.shape) for b in self.bias] able_w=[np.zeros(w.shape) for w in self.weight] for x,y in min_batch: #每次只取一個(gè)樣本? deltab,deltaw = self.backprop(x,y) able_b =[a_b+dab for a_b, dab in zip(able_b,deltab)] #實(shí)際上對dw,db做批次累加,最后小批次取平均 able_w = [a_w + daw for a_w, daw in zip(able_w, deltaw)] self.weight = [weight - eta * (dw) / len(min_batch)- eta*(lambd*weight)/n for weight, dw in zip(self.weight,able_w) ] #增加正則化項(xiàng):eta*lambda/m *weight self.bias = [bias - eta * db / len(min_batch) for bias, db in zip(self.bias, able_b)] def backprop(self,x,y): """" 1) clacu the forward value 2) calcu the delta: delta =(y-f(z)); deltak = delta*w(k)*fz(k-1)' 3) clacu the delta in every layer: deltab=delta; deltaw=delta*fz(k-1)""" deltab = [np.zeros(b.shape) for b in self.bias] deltaw = [np.zeros(w.shape) for w in self.weight] zs = [] activate = x activates = [x] for w,b in zip(self.weight,self.bias): z =np.dot(w, activate) +b zs.append(z) activate = sigmoid(z) activates.append(activate) # backprop delta = self.cost.delta(zs[-1],activates[-1],y) #調(diào)用不同代價(jià)函數(shù)的方法求梯度 deltab[-1] = delta deltaw[-1] = np.dot(delta ,activates[-2].transpose()) for i in xrange(2,self.num_layer): z = zs[-i] delta = np.dot(self.weight[-i+1].transpose(),delta)* sig_derivate(z) deltab[-i] = delta deltaw[-i] = np.dot(delta,activates[-i-1].transpose()) return (deltab,deltaw) def save(self,filename): """將訓(xùn)練好的網(wǎng)絡(luò)采用json(java script object notation)將對象保存成字符串保存,用于生產(chǎn)部署 encoder=json.dumps(data) python 原始類型(沒有數(shù)組類型)向 json 類型的轉(zhuǎn)化對照表: python json dict object list/tuple arrary int/long/float number .tolist() 將數(shù)組轉(zhuǎn)化為列表 >>> a = np.array([[1, 2], [3, 4]]) >>> list(a) [array([1, 2]), array([3, 4])] >>> a.tolist() [[1, 2], [3, 4]] """ data = {"sizes": self.sizes,"weight": [weight.tolist() for weight in self.weight], "bias": ([bias.tolist() for bias in self.bias]), "cost": str(self.cost.__name__)} # 保存網(wǎng)絡(luò)訓(xùn)練好的權(quán)值,偏置,交叉熵參數(shù)。 f = open(filename, "w") json.dump(data,f) f.close() def load_net(filename): """采用data=json.load(json.dumps(data))進(jìn)行解碼, decoder = json.load(encoder) 編碼后和解碼后鍵不會(huì)按照原始data的鍵順序排列,但每個(gè)鍵對應(yīng)的值不會(huì)變 載入訓(xùn)練好的網(wǎng)絡(luò)用于測試""" f = open(filename,"r") data = json.load(f) f.close() # print "data[cost]", getattr(sys.modules[__name__], data["cost"])#獲得屬性__main__.CrossEntropyCost # print "data[cost]", data["cost"], data["sizes"] net = Network(data["sizes"], cost=data["cost"]) #網(wǎng)絡(luò)初始化 net.weight = [np.array(w) for w in data["weight"]] #賦予訓(xùn)練好的權(quán)值,并將list--->array net.bias = [np.array(b) for b in data["bias"]] return net def sig_derivate(z): """derivate sigmoid""" return sigmoid(z) * (1-sigmoid(z)) def sigmoid(x): sigm=1.0/(1.0+exp(-x)) return sigm def vectors(y): """賦予標(biāo)簽""" label = np.zeros((10,1)) label[y] = 1.0 #浮點(diǎn)計(jì)算 return label
3) 網(wǎng)絡(luò)測試
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-12 15:24 # @Author : CC # @File : net_test.py import net_load_data # net_load_data.load_data() train_data,validation_data,test_data = net_load_data.data_transform() import net_network2 as net cost = net.QuadraticCost cost = net.CrossEntroyCost lambd = 0 net1 = net.Network([784,50,10],cost) min_batch_size = 30 eta = 3.0 epoches = 2 net1.SGD(train_data,min_batch_size,epoches,eta,test_data, lambd, monitor_train_cost=True, monitor_train_accuracy=True, monitor_test_cost=True, monitor_test_accuracy=True ) print "complete"
4 調(diào)用訓(xùn)練好的網(wǎng)絡(luò)進(jìn)行測試
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-28 17:27 # @Author : CC # @File : forward_test.py import numpy as np # 對訓(xùn)練好的網(wǎng)絡(luò)直接進(jìn)行調(diào)用,并用測試樣本進(jìn)行測試 import net_load_data #導(dǎo)入測試數(shù)據(jù) import net_network2 as net train_data,validation_data,test_data = net_load_data.data_transform() net = net.load_net(filename= "net_save") #導(dǎo)入網(wǎng)絡(luò) output = [(np.argmax(net.forward(x)),y) for (x,y) in test_data] #測試 print sum(int(y1 == y2) for (y1,y2) in output) #輸出最終值
看完上述內(nèi)容,你們對使用python怎么構(gòu)建一個(gè)深度神經(jīng)網(wǎng)絡(luò)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:使用python怎么構(gòu)建一個(gè)深度神經(jīng)網(wǎng)絡(luò)-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article30/dchhpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、ChatGPT、微信公眾號(hào)、品牌網(wǎng)站制作、網(wǎng)站制作、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容