Guest User

Untitled

a guest
Mar 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "window_size = 8\n",
  10. "alpha = 0.025 # learning rate\n",
  11. "p = 100 # p = dimensions of document vectors (no. of features)\n",
  12. "m = len(vocab) # number of words in the corpus \n",
  13. "\n",
  14. "D = np.random.rand(p, n) # matrix of document embeddings\n",
  15. "U = scipy.stats.truncnorm.rvs(-2, 2, loc=0, scale=1, size=(m, p)) # matrix of softmax weights\n",
  16. "\n",
  17. "epochs = 1\n",
  18. "for epoch in range(epochs):\n",
  19. " for i in range(n):\n",
  20. " # Feed-forward\n",
  21. " d = np.array(np.zeros(n), ndmin=2).T\n",
  22. " d[i] = 1\n",
  23. " \n",
  24. " e = np.array(np.dot(D, d), ndmin=2)\n",
  25. " k = np.array(np.dot(U, e), ndmin=2)\n",
  26. " t_hat = softmax(k)\n",
  27. " \n",
  28. " doc_words = documents[i]\n",
  29. " middle = randint(window_size, len(doc_words) - window_size - 1)\n",
  30. "\n",
  31. " #window_words = [] \n",
  32. " \n",
  33. " errors_out = (np.array(np.zeros(m), ndmin=2).T)\n",
  34. " errors_middle = (np.array(np.zeros(p), ndmin=2).T)\n",
  35. " \n",
  36. " for c in range(middle - window_size, middle + window_size):\n",
  37. " #window_words.append(doc_words[c])\n",
  38. " \n",
  39. " t = (np.array(np.zeros(len(vocab)), ndmin=2).T)\n",
  40. " t[vocab.index(doc_words[c])] = 1\n",
  41. " errors_out += t_hat - t\n",
  42. " errors_middle += np.dot(U.T, errors_out)\n",
  43. " \n",
  44. " if c == middle:\n",
  45. " print(cross_entropy_loss(t, t_hat))\n",
  46. "\n",
  47. " # Backprogation\n",
  48. " U += - alpha * np.dot(errors_out, e.T)\n",
  49. " D += - alpha * np.dot(errors_middle, d.T)"
  50. ]
  51. }
  52. ],
  53. "metadata": {
  54. "kernelspec": {
  55. "display_name": "Python [default]",
  56. "language": "python",
  57. "name": "python3"
  58. },
  59. "language_info": {
  60. "codemirror_mode": {
  61. "name": "ipython",
  62. "version": 3
  63. },
  64. "file_extension": ".py",
  65. "mimetype": "text/x-python",
  66. "name": "python",
  67. "nbconvert_exporter": "python",
  68. "pygments_lexer": "ipython3",
  69. "version": "3.6.3"
  70. },
  71. "toc": {
  72. "nav_menu": {},
  73. "number_sections": true,
  74. "sideBar": true,
  75. "skip_h1_title": false,
  76. "toc_cell": false,
  77. "toc_position": {},
  78. "toc_section_display": "block",
  79. "toc_window_display": false
  80. },
  81. "varInspector": {
  82. "cols": {
  83. "lenName": 16,
  84. "lenType": 16,
  85. "lenVar": 40
  86. },
  87. "kernels_config": {
  88. "python": {
  89. "delete_cmd_postfix": "",
  90. "delete_cmd_prefix": "del ",
  91. "library": "var_list.py",
  92. "varRefreshCmd": "print(var_dic_list())"
  93. },
  94. "r": {
  95. "delete_cmd_postfix": ") ",
  96. "delete_cmd_prefix": "rm(",
  97. "library": "var_list.r",
  98. "varRefreshCmd": "cat(var_dic_list()) "
  99. }
  100. },
  101. "types_to_exclude": [
  102. "module",
  103. "function",
  104. "builtin_function_or_method",
  105. "instance",
  106. "_Feature"
  107. ],
  108. "window_display": false
  109. }
  110. },
  111. "nbformat": 4,
  112. "nbformat_minor": 2
  113. }
Add Comment
Please, Sign In to add comment