题目标题

Dropout的实现

难度:中级

深度学习
参考解析
  1. import numpy as np
  2. 2
  3. 3 p = 0.5 # 神经元激活概率
  4. 4
  5. 5 def train_step(X):
  6. 6 """ X contains the data """
  7. 7
  8. 8 # 三层神经网络前向传播为例
  9. 9 H1 = np.maximum(0, np.dot(W1, X) + b1)
  10. 10 U1 = np.random.rand(*H1.shape) < p # first dropout mask
  11. 11 H1 *= U1 # drop!
  12. 12 H2 = np.maximum(0, np.dot(W2, H1) + b2)
  13. 13 U2 = np.random.rand(*H2.shape) < p # second dropout mask
  14. 14 H2 *= U2 # drop!
  15. 15 out = np.dot(W3, H2) + b3
  16. 16
  17. 17
  18. 18 def predict(X):
  19. 19 # ensembled forward pass
  20. 20 H1 = np.maximum(0, np.dot(W1, X) + b1) * p # NOTE: scale the activations
  21. 21 H2 = np.maximum(0, np.dot(W2, H1) + b2) * p # NOTE: scale the activations
  22. 22 out = np.dot(W3, H2) + b3