Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
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.

148 lines
3.2 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "d0ce4228",
  6. "metadata": {},
  7. "source": [
  8. "# plot activation functions\n"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": null,
  14. "id": "b3c8093a",
  15. "metadata": {},
  16. "outputs": [],
  17. "source": [
  18. "# Importing the required libraries\n",
  19. "import math\n",
  20. "import matplotlib.pyplot as plt\n",
  21. "import numpy as np"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": null,
  27. "id": "a134fb45",
  28. "metadata": {},
  29. "outputs": [],
  30. "source": [
  31. "# The definition of activation functions mathematically\n",
  32. "# Sigmoid Function\n",
  33. "def sigmoid(x):\n",
  34. " a = []\n",
  35. " for i in x:\n",
  36. " a.append(1/(1+math.exp(-i)))\n",
  37. " return a"
  38. ]
  39. },
  40. {
  41. "cell_type": "code",
  42. "execution_count": null,
  43. "id": "954c32ed",
  44. "metadata": {},
  45. "outputs": [],
  46. "source": [
  47. "# Hyperbolic Tanjant Function\n",
  48. "def tanh(x, derivative=False):\n",
  49. " if (derivative == True):\n",
  50. " return (1 - (x ** 2))\n",
  51. " return np.tanh(x)"
  52. ]
  53. },
  54. {
  55. "cell_type": "code",
  56. "execution_count": null,
  57. "id": "2c0e5136",
  58. "metadata": {},
  59. "outputs": [],
  60. "source": [
  61. "# ReLU Function\n",
  62. "def re(x):\n",
  63. " b = []\n",
  64. " for i in x:\n",
  65. " if i<0:\n",
  66. " b.append(0)\n",
  67. " else:\n",
  68. " b.append(i)\n",
  69. " return b"
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": null,
  75. "id": "f29cac64",
  76. "metadata": {},
  77. "outputs": [],
  78. "source": [
  79. "# Leaky ReLU Function\n",
  80. "def lr(x):\n",
  81. " b = []\n",
  82. " for i in x:\n",
  83. " if i<0:\n",
  84. " b.append(i/10)\n",
  85. " else:\n",
  86. " b.append(i)\n",
  87. " return b\n",
  88. " "
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": null,
  94. "id": "1854dc7b",
  95. "metadata": {},
  96. "outputs": [],
  97. "source": [
  98. "# Determining the intervals to be created for the graph\n",
  99. "x = np.arange(-3., 3., 0.1)\n",
  100. "sig = sigmoid(x)\n",
  101. "tanh = tanh(x)\n",
  102. "relu = re(x)\n",
  103. "leaky_relu = lr(x)\n",
  104. "swish = sig*x"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": null,
  110. "id": "9c535ccb",
  111. "metadata": {},
  112. "outputs": [],
  113. "source": [
  114. "# Displaying the functions\n",
  115. "line_1, = plt.plot(x,sig, label='Sigmoid')\n",
  116. "line_2, = plt.plot(x,tanh, label='Tanh')\n",
  117. "line_3, = plt.plot(x,relu, label='ReLU')\n",
  118. "line_4, = plt.plot(x,leaky_relu, label='Leaky ReLU')\n",
  119. "line_5, = plt.plot(x,swish, label='Swish')\n",
  120. "plt.legend(handles=[line_1, line_2, line_3, line_4, line_5])\n",
  121. "plt.axhline(y=0, color='k')\n",
  122. "plt.axvline(x=0, color='k')\n",
  123. "plt.show()"
  124. ]
  125. }
  126. ],
  127. "metadata": {
  128. "kernelspec": {
  129. "display_name": "Python 3 (ipykernel)",
  130. "language": "python",
  131. "name": "python3"
  132. },
  133. "language_info": {
  134. "codemirror_mode": {
  135. "name": "ipython",
  136. "version": 3
  137. },
  138. "file_extension": ".py",
  139. "mimetype": "text/x-python",
  140. "name": "python",
  141. "nbconvert_exporter": "python",
  142. "pygments_lexer": "ipython3",
  143. "version": "3.8.16"
  144. }
  145. },
  146. "nbformat": 4,
  147. "nbformat_minor": 5
  148. }