full_code.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 12 - regularization
  5. """
  6. Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
  7. """
  8. from __future__ import print_function
  9. import theano
  10. from sklearn.datasets import load_boston
  11. import theano.tensor as T
  12. import numpy as np
  13. import matplotlib.pyplot as plt
  14. class Layer(object):
  15. def __init__(self, inputs, in_size, out_size, activation_function=None):
  16. self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))
  17. self.b = theano.shared(np.zeros((out_size, )) + 0.1)
  18. self.Wx_plus_b = T.dot(inputs, self.W) + self.b
  19. self.activation_function = activation_function
  20. if activation_function is None:
  21. self.outputs = self.Wx_plus_b
  22. else:
  23. self.outputs = self.activation_function(self.Wx_plus_b)
  24. def minmax_normalization(data):
  25. xs_max = np.max(data, axis=0)
  26. xs_min = np.min(data, axis=0)
  27. xs = (1 - 0) * (data - xs_min) / (xs_max - xs_min) + 0
  28. return xs
  29. np.random.seed(100)
  30. x_data = load_boston().data
  31. # minmax normalization, rescale the inputs
  32. x_data = minmax_normalization(x_data)
  33. y_data = load_boston().target[:, np.newaxis]
  34. # cross validation, train test data split
  35. x_train, y_train = x_data[:400], y_data[:400]
  36. x_test, y_test = x_data[400:], y_data[400:]
  37. x = T.dmatrix("x")
  38. y = T.dmatrix("y")
  39. l1 = Layer(x, 13, 50, T.tanh)
  40. l2 = Layer(l1.outputs, 50, 1, None)
  41. # the way to compute cost
  42. cost = T.mean(T.square(l2.outputs - y)) # without regularization
  43. # cost = T.mean(T.square(l2.outputs - y)) + 0.1 * ((l1.W ** 2).sum() + (l2.W ** 2).sum()) # with l2 regularization
  44. # cost = T.mean(T.square(l2.outputs - y)) + 0.1 * (abs(l1.W).sum() + abs(l2.W).sum()) # with l1 regularization
  45. gW1, gb1, gW2, gb2 = T.grad(cost, [l1.W, l1.b, l2.W, l2.b])
  46. learning_rate = 0.01
  47. train = theano.function(
  48. inputs=[x, y],
  49. updates=[(l1.W, l1.W - learning_rate * gW1),
  50. (l1.b, l1.b - learning_rate * gb1),
  51. (l2.W, l2.W - learning_rate * gW2),
  52. (l2.b, l2.b - learning_rate * gb2)])
  53. compute_cost = theano.function(inputs=[x, y], outputs=cost)
  54. # record cost
  55. train_err_list = []
  56. test_err_list = []
  57. learning_time = []
  58. for i in range(1000):
  59. train(x_train, y_train)
  60. if i % 10 == 0:
  61. # record cost
  62. train_err_list.append(compute_cost(x_train, y_train))
  63. test_err_list.append(compute_cost(x_test, y_test))
  64. learning_time.append(i)
  65. # plot cost history
  66. plt.plot(learning_time, train_err_list, 'r-')
  67. plt.plot(learning_time, test_err_list, 'b--')
  68. plt.show()