implement of deep neural network --- python
来源:互联网 发布:淘宝刷手p图软件哪个好 编辑:程序博客网 时间:2024/05/21 11:33
import numpy as npimport randomdef sigmoid(z): return 1.0/(1.0 + np.exp(-z))def sigmoid_prime(z): return sigmoid(z)*(1.0-sigmoid(z))class Net(object): def __init__(self,sizes): self.layer_num = len(sizes) self.sizes = sizes self.bias = [ np.random.randn(y,1) for y in sizes[1:] ] self.weights = [ np.random.randn(y,x) for x,y in zip(sizes[:-1],sizes[1:]) ] def feedward(self,a): a = np.array([a]).transpose() print a for b,w in zip(self.bias, self.weights): a = sigmoid( np.dot(w,a) + b ) print w.shape, a.shape return a def SDG(self, training_data, epochs, mini_batch_size, eta): n = len(training_data) for j in xrange(epochs): random.shuffle(training_data) mini_batchs = [ training_data[k:k+mini_batch_size] for k in xrange(0,n,mini_batch_size) ] for mini_batch in mini_batchs: self.update_mini_batch(mini_batch, eta) if j%100 ==0: print 'epoch{0} complete..'.format(j) def update_mini_batch(self, mini_batch, eta): nabla_b = [ np.zeros(b.shape) for b in self.bias ] nabla_w = [ np.zeros(w.shape) for w in self.weights ] for x,y in mini_batch: delta_b, delta_w = self.backprop(x,y) nabla_b = [ nb+dnb for nb, dnb in zip(nabla_b, delta_b) ] nabla_w = [ nw+bnw for nw, bnw in zip(nabla_w, delta_w) ] self.weights = [ w-(eta/len(mini_batch))*nw for w,nw in zip(self.weights, nabla_w) ] self.bias = [ b-(eta/len(mini_batch))*nb for b,nb in zip(self.bias, nabla_b) ] def backprop(self,x,y): nabla_b = [ np.zeros(b.shape) for b in self.bias ] nabla_w = [ np.zeros(w.shape) for w in self.weights ] # feedward activation = np.array([x]).transpose() #print activation activations = [activation] zs = [] for b,w in zip(self.bias, self.weights): z = np.dot(w, activation)+b zs.append(z) activation = sigmoid(z) activations.append(activation) # backward delta = self.cost_derivate(activations[-1],y) * sigmoid_prime(zs[-1]) nabla_b[-1] = delta nabla_w[-1] = np.dot(delta, activations[-2].transpose()) for l in xrange(2, self.layer_num): z = zs[-l] sp = sigmoid_prime(z) delta = np.dot( self.weights[-l+1].transpose(), delta) * sp nabla_b[-l] = delta nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) return (nabla_b, nabla_w) def cost_derivate(self, output_activations, y): return (output_activations-y)xx = Net([2,3,3,1])traindata = [([1,1],3),([1,0],2),([0,0],0), ([0,1],1),([1,1],3),([1,0],2),([0,0],0), ([0,1],1)]xx.SDG(traindata, 100, 4, 0.5)
--------------------analysis----------------------
--------------------reference------------------------
http://neuralnetworksanddeeplearning.com/
0 0
- implement of deep neural network --- python
- The home of my deep neural network
- Deep Neural Network
- Deep Neural Network
- Deep Neural Network
- Bag-of-Words Based Deep Neural Network for Image Retrieval
- Neural Network and Deep Learning
- Deep learning与Neural Network
- Building your Deep Neural Network
- Deep-Learning NotePad2 : Deep Neural network
- neural networks deep learning Deep Neural Network Application Homework
- Look into "A Neural Network in 11 lines of Python"
- 【Deep Learning】Review of Stereo Matching by Training a Convolutional Neural Network to Compare Image
- neural network and deep learning (1)
- neural network and deep learning (2)
- neural network and deep learing(笔记一)
- neural network and deep learning(笔记二)
- ImageNet classification with deep convolutional neural network
- Ionic 常见疑难杂症
- 安卓图片自适应
- 我用Python写自动刷代理服务IP的经过
- leatcode 6. ZigZag Conversion
- lightoj - 1193 - Dice (II) dp / 背包
- implement of deep neural network --- python
- Problem O
- Filter过滤器
- leetcode4:Median of Two Sorted Arrays
- 《第一行代码2》
- java中子类与父类的同名变量覆盖问题
- 【Spark】Spark基本概念
- Android——AS的Refactor下拉菜单Rename的作用
- [leetcode] 24. Swap Nodes in Pairs