@hanxiaoyang 2016-07-22T16:39:10.000000Z 字数 7807 阅读 2288

# 斯坦福CS231n学习笔记_(9)_串一串神经网络之动手实现小例子

CS231n

## 2.样本数据的产生

N = 100 # 每个类中的样本点D = 2 # 维度K = 3 # 类别个数X = np.zeros((N*K,D)) # 样本inputy = np.zeros(N*K, dtype='uint8') # 类别标签for j in xrange(K):  ix = range(N*j,N*(j+1))  r = np.linspace(0.0,1,N) # radius  t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta  X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]  y[ix] = j# 可视化一下我们的样本点plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)

## 3.使用Softmax线性分类器

### 3.1 初始化参数

#随机初始化参数import numpy as np#D=2表示维度，K=3表示类别数W = 0.01 * np.random.randn(D,K)b = np.zeros((1,K))

### 3.2 计算得分

#使用得分函数计算得分scores = np.dot(X, W) + b

### 3.3 计算损失

# 用指数函数还原exp_scores = np.exp(scores)# 归一化probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)

#计算log概率和互熵损失corect_logprobs = -np.log(probs[range(num_examples),y])data_loss = np.sum(corect_logprobs)/num_examples#加上正则化项reg_loss = 0.5*reg*np.sum(W*W)loss = data_loss + reg_loss

### 3.4 计算梯度与梯度回传

dscores = probsdscores[range(num_examples),y] -= 1dscores /= num_examples

dW = np.dot(X.T, dscores)db = np.sum(dscores, axis=0, keepdims=True)#得记着正则化梯度哈dW += reg*W

### 3.5 参数迭代与更新

#参数迭代更新W += -step_size * dWb += -step_size * db

### 3.6 大杂合：训练SoftMax分类器

#代码部分组一起，训练线性分类器#随机初始化参数W = 0.01 * np.random.randn(D,K)b = np.zeros((1,K))#需要自己敲定的步长和正则化系数step_size = 1e-0reg = 1e-3 #正则化系数#梯度下降迭代循环num_examples = X.shape[0]for i in xrange(200):  # 计算类别得分, 结果矩阵为[N x K]  scores = np.dot(X, W) + b   # 计算类别概率  exp_scores = np.exp(scores)  probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K]  # 计算损失loss(包括互熵损失和正则化部分)  corect_logprobs = -np.log(probs[range(num_examples),y])  data_loss = np.sum(corect_logprobs)/num_examples  reg_loss = 0.5*reg*np.sum(W*W)  loss = data_loss + reg_loss  if i % 10 == 0:    print "iteration %d: loss %f" % (i, loss)  # 计算得分上的梯度  dscores = probs  dscores[range(num_examples),y] -= 1  dscores /= num_examples  # 计算和回传梯度  dW = np.dot(X.T, dscores)  db = np.sum(dscores, axis=0, keepdims=True)  dW += reg*W # 正则化梯度  #参数更新  W += -step_size * dW  b += -step_size * db

iteration 0: loss 1.096956iteration 10: loss 0.917265iteration 20: loss 0.851503iteration 30: loss 0.822336iteration 40: loss 0.807586iteration 50: loss 0.799448iteration 60: loss 0.794681iteration 70: loss 0.791764iteration 80: loss 0.789920iteration 90: loss 0.788726iteration 100: loss 0.787938iteration 110: loss 0.787409iteration 120: loss 0.787049iteration 130: loss 0.786803iteration 140: loss 0.786633iteration 150: loss 0.786514iteration 160: loss 0.786431iteration 170: loss 0.786373iteration 180: loss 0.786331iteration 190: loss 0.786302

190次循环之后，结果大致收敛了。我们评估一下准确度：

#评估准确度scores = np.dot(X, W) + bpredicted_class = np.argmax(scores, axis=1)print 'training accuracy: %.2f' % (np.mean(predicted_class == y))

## 4.使用神经网络分类

# 初始化参数h = 100 # 隐层大小(神经元个数)W = 0.01 * np.random.randn(D,h)b = np.zeros((1,h))W2 = 0.01 * np.random.randn(h,K)b2 = np.zeros((1,K))

#2层神经网络的前向计算hidden_layer = np.maximum(0, np.dot(X, W) + b) # 用的 ReLU单元scores = np.dot(hidden_layer, W2) + b2

# 梯度回传与反向传播# 对W2和b2的第一次计算dW2 = np.dot(hidden_layer.T, dscores)db2 = np.sum(dscores, axis=0, keepdims=True)

dhidden = np.dot(dscores, W2.T)

#梯度回传经过ReLUdhidden[hidden_layer <= 0] = 0

dW = np.dot(X.T, dhidden)db = np.sum(dhidden, axis=0, keepdims=True)

# 随机初始化参数h = 100 # 隐层大小W = 0.01 * np.random.randn(D,h)b = np.zeros((1,h))W2 = 0.01 * np.random.randn(h,K)b2 = np.zeros((1,K))# 手动敲定的几个参数step_size = 1e-0reg = 1e-3 # 正则化参数# 梯度迭代与循环num_examples = X.shape[0]for i in xrange(10000):  hidden_layer = np.maximum(0, np.dot(X, W) + b) #使用的ReLU神经元  scores = np.dot(hidden_layer, W2) + b2  # 计算类别概率  exp_scores = np.exp(scores)  probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K]  # 计算互熵损失与正则化项  corect_logprobs = -np.log(probs[range(num_examples),y])  data_loss = np.sum(corect_logprobs)/num_examples  reg_loss = 0.5*reg*np.sum(W*W) + 0.5*reg*np.sum(W2*W2)  loss = data_loss + reg_loss  if i % 1000 == 0:    print "iteration %d: loss %f" % (i, loss)  # 计算梯度  dscores = probs  dscores[range(num_examples),y] -= 1  dscores /= num_examples  # 梯度回传  dW2 = np.dot(hidden_layer.T, dscores)  db2 = np.sum(dscores, axis=0, keepdims=True)  dhidden = np.dot(dscores, W2.T)  dhidden[hidden_layer <= 0] = 0  # 拿到最后W,b上的梯度  dW = np.dot(X.T, dhidden)  db = np.sum(dhidden, axis=0, keepdims=True)  # 加上正则化梯度部分  dW2 += reg * W2  dW += reg * W  # 参数迭代与更新  W += -step_size * dW  b += -step_size * db  W2 += -step_size * dW2  b2 += -step_size * db2

iteration 0: loss 1.098744iteration 1000: loss 0.294946iteration 2000: loss 0.259301iteration 3000: loss 0.248310iteration 4000: loss 0.246170iteration 5000: loss 0.245649iteration 6000: loss 0.245491iteration 7000: loss 0.245400iteration 8000: loss 0.245335iteration 9000: loss 0.245292

#计算分类准确度hidden_layer = np.maximum(0, np.dot(X, W) + b)scores = np.dot(hidden_layer, W2) + b2predicted_class = np.argmax(scores, axis=1)print 'training accuracy: %.2f' % (np.mean(predicted_class == y))

cs231n 神经网络小例子

• 私有
• 公开
• 删除