Ejemplos de Machine Learning para el uso y aplicación de regresiones
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0 KiB

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. ###############################
  4. #Datos originales
  5. ###############################
  6. X = 2 * np.random.rand(100, 1)
  7. y = 4 + 3 * X + np.random.randn(100,1)
  8. ###############################
  9. eta = 0.1
  10. n_iterations = 1000
  11. m = 100
  12. X_b = np.c_[np.ones((100, 1)), X] # add x0 = 1 to each instance
  13. theta = np.random.randn(2,1)
  14. X_new = np.array([[0], [2]])
  15. X_new_b = np.c_[np.ones((2, 1)), X_new] # add x0 = 1 to each instance
  16. for iteration in range(n_iterations):
  17. gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
  18. theta = theta - eta * gradients
  19. theta_path_bgd = []
  20. def plot_gradient_descent(theta, eta, theta_path=None):
  21. m = len(X_b)
  22. plt.plot(X, y, "b.")
  23. n_iterations = 1000
  24. for iteration in range(n_iterations):
  25. if iteration < 10:
  26. y_predict = X_new_b.dot(theta)
  27. style = "b-" if iteration > 0 else "r--"
  28. plt.plot(X_new, y_predict, style)
  29. gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
  30. theta = theta - eta * gradients
  31. if theta_path is not None:
  32. theta_path.append(theta)
  33. plt.xlabel("$x_1$", fontsize=18)
  34. plt.axis([0, 2, 0, 15])
  35. plt.title(r"$\eta = {}$".format(eta), fontsize=16)
  36. np.random.seed(42)
  37. theta = np.random.randn(2,1) # random initialization
  38. plt.figure(figsize=(10,4))
  39. plt.subplot(131); plot_gradient_descent(theta, eta=0.02)
  40. plt.ylabel("$y$", rotation=0, fontsize=18)
  41. plt.subplot(132); plot_gradient_descent(theta, eta=0.1, theta_path=theta_path_bgd)
  42. plt.subplot(133); plot_gradient_descent(theta, eta=0.5)
  43. plt.show()
  44. theta_path_sgd = []
  45. m = len(X_b)
  46. np.random.seed(42)
  47. n_epochs = 50
  48. t0, t1 = 5, 50 # learning schedule hyperparameters
  49. def learning_schedule(t):
  50. return t0 / (t + t1)
  51. theta = np.random.randn(2,1) # random initialization
  52. for epoch in range(n_epochs):
  53. for i in range(m):
  54. if epoch == 0 and i < 20: # not shown in the book
  55. y_predict = X_new_b.dot(theta) # not shown
  56. style = "b-" if i > 0 else "r--" # not shown
  57. plt.plot(X_new, y_predict, style) # not shown
  58. random_index = np.random.randint(m)
  59. xi = X_b[random_index:random_index+1]
  60. yi = y[random_index:random_index+1]
  61. gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
  62. eta = learning_schedule(epoch * m + i)
  63. theta = theta - eta * gradients
  64. theta_path_sgd.append(theta) # not shown
  65. plt.plot(X, y, "b.") # not shown
  66. plt.xlabel("$x_1$", fontsize=18) # not shown
  67. plt.ylabel("$y$", rotation=0, fontsize=18) # not shown
  68. plt.axis([0, 2, 0, 15]) # not shown
  69. plt.show() # not shown
  70. theta_path_mgd = []
  71. n_iterations = 50
  72. minibatch_size = 20
  73. np.random.seed(42)
  74. theta = np.random.randn(2,1) # random initialization
  75. t0, t1 = 200, 1000
  76. def learning_schedule(t):
  77. return t0 / (t + t1)
  78. t = 0
  79. for epoch in range(n_iterations):
  80. shuffled_indices = np.random.permutation(m)
  81. X_b_shuffled = X_b[shuffled_indices]
  82. y_shuffled = y[shuffled_indices]
  83. for i in range(0, m, minibatch_size):
  84. t += 1
  85. xi = X_b_shuffled[i:i+minibatch_size]
  86. yi = y_shuffled[i:i+minibatch_size]
  87. gradients = 2/minibatch_size * xi.T.dot(xi.dot(theta) - yi)
  88. eta = learning_schedule(t)
  89. theta = theta - eta * gradients
  90. theta_path_mgd.append(theta)
  91. theta_path_bgd = np.array(theta_path_bgd)
  92. theta_path_sgd = np.array(theta_path_sgd)
  93. theta_path_mgd = np.array(theta_path_mgd)
  94. plt.figure(figsize=(7,4))
  95. plt.plot(theta_path_sgd[:, 0], theta_path_sgd[:, 1], "r-s", linewidth=1, label="Stochastic")
  96. plt.plot(theta_path_mgd[:, 0], theta_path_mgd[:, 1], "g-+", linewidth=2, label="Mini-batch")
  97. plt.plot(theta_path_bgd[:, 0], theta_path_bgd[:, 1], "b-o", linewidth=3, label="Batch")
  98. plt.legend(loc="upper left", fontsize=16)
  99. plt.xlabel(r"$\theta_0$", fontsize=20)
  100. plt.ylabel(r"$\theta_1$ ", fontsize=20, rotation=0)
  101. plt.axis([2.5, 4.5, 2.3, 3.9])
  102. plt.show()