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

{
"cells": [
{
"cell_type": "markdown",
"id": "d0ce4228",
"metadata": {},
"source": [
"# plot activation functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3c8093a",
"metadata": {},
"outputs": [],
"source": [
"# Importing the required libraries\n",
"import math\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a134fb45",
"metadata": {},
"outputs": [],
"source": [
"# The definition of activation functions mathematically\n",
"# Sigmoid Function\n",
"def sigmoid(x):\n",
" a = []\n",
" for i in x:\n",
" a.append(1/(1+math.exp(-i)))\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "954c32ed",
"metadata": {},
"outputs": [],
"source": [
"# Hyperbolic Tanjant Function\n",
"def tanh(x, derivative=False):\n",
" if (derivative == True):\n",
" return (1 - (x ** 2))\n",
" return np.tanh(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c0e5136",
"metadata": {},
"outputs": [],
"source": [
"# ReLU Function\n",
"def re(x):\n",
" b = []\n",
" for i in x:\n",
" if i<0:\n",
" b.append(0)\n",
" else:\n",
" b.append(i)\n",
" return b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f29cac64",
"metadata": {},
"outputs": [],
"source": [
"# Leaky ReLU Function\n",
"def lr(x):\n",
" b = []\n",
" for i in x:\n",
" if i<0:\n",
" b.append(i/10)\n",
" else:\n",
" b.append(i)\n",
" return b\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1854dc7b",
"metadata": {},
"outputs": [],
"source": [
"# Determining the intervals to be created for the graph\n",
"x = np.arange(-3., 3., 0.1)\n",
"sig = sigmoid(x)\n",
"tanh = tanh(x)\n",
"relu = re(x)\n",
"leaky_relu = lr(x)\n",
"swish = sig*x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c535ccb",
"metadata": {},
"outputs": [],
"source": [
"# Displaying the functions\n",
"line_1, = plt.plot(x,sig, label='Sigmoid')\n",
"line_2, = plt.plot(x,tanh, label='Tanh')\n",
"line_3, = plt.plot(x,relu, label='ReLU')\n",
"line_4, = plt.plot(x,leaky_relu, label='Leaky ReLU')\n",
"line_5, = plt.plot(x,swish, label='Swish')\n",
"plt.legend(handles=[line_1, line_2, line_3, line_4, line_5])\n",
"plt.axhline(y=0, color='k')\n",
"plt.axvline(x=0, color='k')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}