Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "%pylab inline\n",
  10. "\n",
  11. "\n",
  12. "lw=6\n",
  13. "\n",
  14. "colors = {'FW': '#66c2a5','AdaFW': '#fc8d62', 'AdaPFW' : '#8da0cb', 'PFW':'#e78ac3',\n",
  15. " 'AFW' : '#a6d854', 'AdaAFW' : '#ffd92f', 'LSFW': '#756bb1', 'MP': '#998ec3', 'AdaptiveMP' : '#f1a340'}\n",
  16. "\n",
  17. "plt.rcParams['figure.figsize'] = (3 * 10.0, 1 * 8.0)\n",
  18. "plt.rcParams['font.size'] = 35\n",
  19. "\n",
  20. "\n",
  21. "import matplotlib as mpl\n",
  22. "import matplotlib.pyplot as plt\n",
  23. "import matplotlib.font_manager as font_manager\n",
  24. "\n",
  25. "font_dirs = ['/home/fabian/Dropbox/fonts/Open_Sans/', ]\n",
  26. "font_files = font_manager.findSystemFonts(fontpaths=font_dirs)\n",
  27. "font_list = font_manager.createFontList(font_files)\n",
  28. "font_manager.fontManager.ttflist.extend(font_list)\n",
  29. "\n",
  30. "path = '/home/fabian/Dropbox/fonts/Open_Sans/OpenSans-Light.ttf'\n",
  31. "prop = font_manager.FontProperties(fname=path)\n",
  32. "\n",
  33. "matplotlib.rc('font', family='sans-serif') \n",
  34. "# matplotlib.rc('text', usetex='false') \n",
  35. "matplotlib.rcParams['font.family'] = 'Open Sans'\n",
  36. "matplotlib.rcParams['font.weight'] = 'light'\n",
  37. "matplotlib.rcParams.update({'font.size': 35})"
  38. ]
  39. },
  40. {
  41. "cell_type": "code",
  42. "execution_count": null,
  43. "metadata": {
  44. "scrolled": true
  45. },
  46. "outputs": [],
  47. "source": [
  48. "plt.rcParams['figure.figsize'] = (2 * 10.0, 1 * 8.0)\n",
  49. "plt.rcParams['font.size'] = 25\n",
  50. "\n",
  51. "\n",
  52. "matplotlib.rcParams['xtick.direction'] = 'out'\n",
  53. "matplotlib.rcParams['ytick.direction'] = 'out'\n",
  54. "\n",
  55. "np.random.seed(0)\n",
  56. "n_samples = 100\n",
  57. "A = np.random.rand(n_samples, 2)\n",
  58. "A[:, 0] *= 4\n",
  59. "b = np.sign(np.random.randn(100))\n",
  60. "alpha = 2./n_samples\n",
  61. "\n",
  62. "from sklearn.linear_model import logistic\n",
  63. "from scipy import optimize\n",
  64. "\n",
  65. "def f_obj(x):\n",
  66. " return logistic._logistic_loss(x, A, b, alpha)\n",
  67. "\n",
  68. "delta = 0.025\n",
  69. "x = np.arange(-3.0, 2.0, delta)\n",
  70. "y = np.arange(-3.0, 2.0, delta)\n",
  71. "X, Y = np.meshgrid(x, y)\n",
  72. "\n",
  73. "Z = np.zeros_like(X)\n",
  74. "for i in range(X.shape[0]):\n",
  75. " for j in range(X.shape[1]):\n",
  76. " xx = np.array((X[i, j], Y[i, j]))\n",
  77. " Z[i, j] = f_obj(xx)\n",
  78. "\n",
  79. "sol = optimize.minimize(f_obj, (0, 0))\n",
  80. "plot_x = []\n",
  81. "plot_y = []\n",
  82. "plot_x2 = []\n",
  83. "plot_y2 = []\n",
  84. "xt = np.array((-3, -3))\n",
  85. "L = 0.25 * linalg.linalg.norm(A) ** 2 + alpha\n",
  86. "L_adaptive = 0.01 * L\n",
  87. "step_size = 1 / L\n",
  88. "xt2 = xt.copy()\n",
  89. "for i in range(25):\n",
  90. " f, axarr = plt.subplots(1, 2, sharex=True)\n",
  91. " CS = axarr[0].contour(X, Y, Z, 20)\n",
  92. " #plt.clabel(CS, inline=1, fontsize=10)\n",
  93. "\n",
  94. " plot_x.append(xt[0])\n",
  95. " plot_y.append(xt[1])\n",
  96. " axarr[0].plot(plot_x, plot_y, marker='^', markersize=10, lw=2)\n",
  97. " f.suptitle('Theoretical (left) vs Adaptive (right) Step Size, iteration %s' % i)\n",
  98. "\n",
  99. " xt = xt - step_size * logistic._logistic_loss_and_grad(xt, A, b, alpha)[1]\n",
  100. " axarr[0].set_xticks(())\n",
  101. " axarr[1].set_xticks(())\n",
  102. " axarr[0].set_yticks(())\n",
  103. " axarr[1].set_yticks(())\n",
  104. "\n",
  105. " # axarr[0].scatter(*sol.x, s=100)\n",
  106. " \n",
  107. " \n",
  108. " ## second plot\n",
  109. " CS = axarr[1].contour(X, Y, Z, 20)\n",
  110. " plot_x2.append(xt2[0])\n",
  111. " plot_y2.append(xt2[1])\n",
  112. " axarr[1].plot(plot_x2, plot_y2, marker='^', markersize=10, lw=2) \n",
  113. "\n",
  114. " L_adaptive *= 0.1\n",
  115. " f_grad = logistic._logistic_loss_and_grad(xt2, A, b, 0)[1]\n",
  116. " x_next = xt2 - (1 / L_adaptive) * f_grad\n",
  117. "\n",
  118. " while True:\n",
  119. " if f_obj(x_next) <= f_obj(xt2) + f_grad.dot(x_next - xt2) + (L_adaptive/2.) * (np.linalg.norm(x_next - xt2) **2):\n",
  120. " xt2 = xt2 - (1 / L_adaptive) * f_grad\n",
  121. " break\n",
  122. " else:\n",
  123. " L_adaptive *= 1.1\n",
  124. " x_next = xt2 - (1 / L_adaptive) * f_grad\n",
  125. " \n",
  126. " plt.savefig('lr_adaptive_%02d.png' % i)\n",
  127. " plt.show()\n",
  128. " "
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": null,
  134. "metadata": {},
  135. "outputs": [],
  136. "source": []
  137. }
  138. ],
  139. "metadata": {
  140. "kernelspec": {
  141. "display_name": "Python 3",
  142. "language": "python",
  143. "name": "python3"
  144. },
  145. "language_info": {
  146. "codemirror_mode": {
  147. "name": "ipython",
  148. "version": 3
  149. },
  150. "file_extension": ".py",
  151. "mimetype": "text/x-python",
  152. "name": "python",
  153. "nbconvert_exporter": "python",
  154. "pygments_lexer": "ipython3",
  155. "version": "3.6.4"
  156. }
  157. },
  158. "nbformat": 4,
  159. "nbformat_minor": 2
  160. }
Add Comment
Please, Sign In to add comment