Various activation functions, including Sigmoid, Tanh, ReLU, and their variants, are implemented in Python for visualization. Plotting these functions helps grasp their properties, such as output ranges, saturation points, and non-linearity, which are significant considerations when designing neural network layers.We'll use NumPy for the mathematical computations and Plotly for creating an interactive visualization.First, let's import the necessary library:import numpy as npNow, we define the activation functions based on their mathematical formulas:# Sigmoid function def sigmoid(x): """Computes the Sigmoid activation.""" return 1 / (1 + np.exp(-x)) # Hyperbolic Tangent (Tanh) function def tanh(x): """Computes the Tanh activation.""" return np.tanh(x) # Rectified Linear Unit (ReLU) function def relu(x): """Computes the ReLU activation.""" return np.maximum(0, x) # Leaky Rectified Linear Unit (Leaky ReLU) function def leaky_relu(x, alpha=0.01): """Computes the Leaky ReLU activation.""" return np.maximum(alpha * x, x)Next, we'll generate a range of input values, typically centered around zero, to see how the functions behave across different inputs.# Generate input values from -5 to 5 x = np.linspace(-5, 5, 100) # Calculate the output of each activation function y_sigmoid = sigmoid(x) y_tanh = tanh(x) y_relu = relu(x) y_leaky_relu = leaky_relu(x)With the input values and corresponding outputs for each function calculated, we can now plot them. This visualization allows for a direct comparison of their shapes and characteristics.{"layout": {"title": "Common Activation Functions", "xaxis": {"title": "Input (x)", "range": [-5, 5]}, "yaxis": {"title": "Output (f(x))", "range": [-1.2, 5.2]}, "legend": {"yanchor": "top", "y": 0.99, "xanchor": "left", "x": 0.01}}, "data": [{"x": [-5.0, -4.8989898989899, -4.7979797979798, -4.6969696969697, -4.5959595959596, -4.49494949494949, -4.39393939393939, -4.29292929292929, -4.19191919191919, -4.09090909090909, -3.98989898989899, -3.88888888888889, -3.78787878787879, -3.68686868686869, -3.58585858585859, -3.48484848484848, -3.38383838383838, -3.28282828282828, -3.18181818181818, -3.08080808080808, -2.97979797979798, -2.87878787878788, -2.77777777777778, -2.67676767676768, -2.57575757575758, -2.47474747474747, -2.37373737373737, -2.27272727272727, -2.17171717171717, -2.07070707070707, -1.96969696969697, -1.86868686868687, -1.76767676767677, -1.66666666666667, -1.56565656565657, -1.46464646464646, -1.36363636363636, -1.26262626262626, -1.16161616161616, -1.06060606060606, -0.95959595959596, -0.858585858585859, -0.757575757575758, -0.656565656565657, -0.555555555555556, -0.454545454545455, -0.353535353535354, -0.252525252525253, -0.151515151515152, -0.0505050505050506, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "y": [0.0066928509242848554, 0.007427296596162802, 0.00823184164157509, 0.009110511944006452, 0.010067986038709233, 0.011109000335700086, 0.01223899565574705, 0.01346386354635331, 0.014790038049901978, 0.016224733555328356, 0.017775938804536994, 0.019452135889424346, 0.021262359976184127, 0.023216311391966647, 0.025324380106374224, 0.02759778795423167, 0.03004850309087786, 0.03268915935857348, 0.03553308743330582, 0.03859439288313326, 0.04188792470703271, 0.04542930085315312, 0.04923483216668821, 0.05332157120214384, 0.05770718180866582, 0.06240994164514449, 0.0674489883096106, 0.07284418089158138, 0.07861619897338093, 0.08478644869243331, 0.09137712591550857, 0.09841005435418234, 0.10590713506949917, 0.11388978448847952, 0.12237841022474855, 0.1313919144274849, 0.14094726874805645, 0.1510598954471698, 0.16174285048186943, 0.17300679163846728, 0.18485883299374685, 0.1973028687557058, 0.2103387699972423, 0.22396245851193602, 0.23816570972004897, 0.2529362115617361, 0.2682573956786535, 0.2841081254847379, 0.3004625714181754, 0.3172894543071179, 0.3345525617160957, 0.3522114448387209, 0.3702218616270206, 0.38853566113113953, 0.4070998221381913, 0.42586010950063785, 0.4447612484129619, 0.4637470192349667, 0.4827611953041876, 0.5017476091468007, 0.5206502822714088, 0.5394134755091329, 0.557981789009338, 0.5763001448098351, 0.5943140977106828, 0.6119699708181997, 0.6292152753814828, 0.6460000035550091, 0.6622758095924049, 0.6779964853908108, 0.6931184352267596, 0.7076000369211896, 0.7214019290630809, 0.7344871841218571, 0.7468213429698903, 0.7583733876223323, 0.7691157232622959, 0.7790241514569933, 0.7880780053491697, 0.7962601855625196, 0.8035581101875107, 0.809963411463599, 0.8154708050805544, 0.8200781219746567, 0.8237861580749766, 0.8265986419750937, 0.8285220802478164, 0.8295655659696596, 0.8297402227551024, 0.8310584187508953, 0.8324997556551518, 0.8340634416605199, 0.8357486963392174, 0.8375547519695847, 0.8394808505074474, 0.8415262527924011, 0.8436899931183626, 0.8459710875369423, 0.8483684851927281, 0.8508810742597614], "mode": "lines", "name": "Sigmoid", "line": {"color": "#228be6"}}, {"x": [-5.0, -4.8989898989899, -4.7979797979798, -4.6969696969697, -4.5959595959596, -4.49494949494949, -4.39393939393939, -4.29292929292929, -4.19191919191919, -4.09090909090909, -3.98989898989899, -3.88888888888889, -3.78787878787879, -3.68686868686869, -3.58585858585859, -3.48484848484848, -3.38383838383838, -3.28282828282828, -3.18181818181818, -3.08080808080808, -2.97979797979798, -2.87878787878788, -2.77777777777778, -2.67676767676768, -2.57575757575758, -2.47474747474747, -2.37373737373737, -2.27272727272727, -2.17171717171717, -2.07070707070707, -1.96969696969697, -1.86868686868687, -1.76767676767677, -1.66666666666667, -1.56565656565657, -1.46464646464646, -1.36363636363636, -1.26262626262626, -1.16161616161616, -1.06060606060606, -0.95959595959596, -0.858585858585859, -0.757575757575758, -0.656565656565657, -0.555555555555556, -0.454545454545455, -0.353535353535354, -0.252525252525253, -0.151515151515152, -0.0505050505050506, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "y": [-0.9999329299736164, -0.9998903835928646, -0.9998368284197945, -0.9997701824299947, -0.9996880853317159, -0.9995878176680464, -0.9994662582786871, -0.9993198744779009, -0.9991447403907448, -0.9989365188843639, -0.9986899620779635, -0.9983990924519736, -0.9980571947337355, -0.9976565227399263, -0.9971881528110206, -0.9966419282195696, -0.9960067407358797, -0.995270408413386, -0.9944199001338579, -0.9934409198896334, -0.9923180091181773, -0.991034877587548, -0.9895737644686104, -0.9879156903511366, -0.9860399830751213, -0.9839243199606464, -0.9815447030705881, -0.9788748236593997, -0.9758858960776383, -0.972547007530344, -0.9688247783199816, -0.9646837963578047, -0.9600869085856103, -0.9549951692017709, -0.9493677351552942, -0.9431618887458153, -0.9363319553372438, -0.9288291177959895, -0.9206014849289418, -0.9115942904191444, -0.901749437232192, -0.8909997075527075, -0.8792717862301594, -0.8664861077826558, -0.8525586871432803, -0.8373994238710967, -0.8209131441790326, -0.8029988212190979, -0.783552870368087, -0.7624683792832477, -0.7396364448049972, -0.7149481541162517, -0.6883007293378976, -0.6595997863093232, -0.6287621291388584, -0.5957122363487219, -0.5603848302346251, -0.5227245672227323, -0.4826900457304833, -0.4402570606494751, -0.3954197754777513, -0.3481999798279867, -0.29864987128073135, -0.24685325319217953, -0.1929238715584816, -0.1370054230383524, -0.07926144414266668, -0.019874469764031464, 0.04103841575595254, 0.10329988632427549, 0.1667387553905028, 0.2311890281149147, 0.29649089879188846, 0.3624899719648835, 0.4289937483675518, 0.4958008404991073, 0.5626927932062549, 0.6294432887020643, 0.6958180380420087, 0.7615809888248401, 0.8264963785801218, 0.8803373893985893, 0.9230812680648813, 0.9547711811699836, 0.9756056102036573, 0.9860399830751213, 0.991034877587548, 0.9934409198896334, 0.9944199001338579, 0.995270408413386, 0.9960067407358797, 0.9966419282195696, 0.9971881528110206, 0.9976565227399263, 0.9980571947337355, 0.9983990924519736, 0.9986899620779635, 0.9989365188843639, 0.9991447403907448, 0.9993198744779009, 0.9994662582786871, 0.9995878176680464, 0.9996880853317159, 0.9997701824299947, 0.9998368284197945, 0.9998903835928646, 0.9999329299736164], "mode": "lines", "name": "Tanh", "line": {"color": "#ae3ec9"}}, {"x": [-5.0, -4.8989898989899, -4.7979797979798, -4.6969696969697, -4.5959595959596, -4.49494949494949, -4.39393939393939, -4.29292929292929, -4.19191919191919, -4.09090909090909, -3.98989898989899, -3.88888888888889, -3.78787878787879, -3.68686868686869, -3.58585858585859, -3.48484848484848, -3.38383838383838, -3.28282828282828, -3.18181818181818, -3.08080808080808, -2.97979797979798, -2.87878787878788, -2.77777777777778, -2.67676767676768, -2.57575757575758, -2.47474747474747, -2.37373737373737, -2.27272727272727, -2.17171717171717, -2.07070707070707, -1.96969696969697, -1.86868686868687, -1.76767676767677, -1.66666666666667, -1.56565656565657, -1.46464646464646, -1.36363636363636, -1.26262626262626, -1.16161616161616, -1.06060606060606, -0.95959595959596, -0.858585858585859, -0.757575757575758, -0.656565656565657, -0.555555555555556, -0.454545454545455, -0.353535353535354, -0.252525252525253, -0.151515151515152, -0.0505050505050506, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "y": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "mode": "lines", "name": "ReLU", "line": {"color": "#12b886"}}, {"x": [-5.0, -4.8989898989899, -4.7979797979798, -4.6969696969697, -4.5959595959596, -4.49494949494949, -4.39393939393939, -4.29292929292929, -4.19191919191919, -4.09090909090909, -3.98989898989899, -3.88888888888889, -3.78787878787879, -3.68686868686869, -3.58585858585859, -3.48484848484848, -3.38383838383838, -3.28282828282828, -3.18181818181818, -3.08080808080808, -2.97979797979798, -2.87878787878788, -2.77777777777778, -2.67676767676768, -2.57575757575758, -2.47474747474747, -2.37373737373737, -2.27272727272727, -2.17171717171717, -2.07070707070707, -1.96969696969697, -1.86868686868687, -1.76767676767677, -1.66666666666667, -1.56565656565657, -1.46464646464646, -1.36363636363636, -1.26262626262626, -1.16161616161616, -1.06060606060606, -0.95959595959596, -0.858585858585859, -0.757575757575758, -0.656565656565657, -0.555555555555556, -0.454545454545455, -0.353535353535354, -0.252525252525253, -0.151515151515152, -0.0505050505050506, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "y": [-0.05, -0.048989898989899, -0.047979797979798, -0.046969696969697, -0.045959595959596, -0.0449494949494949, -0.0439393939393939, -0.0429292929292929, -0.0419191919191919, -0.0409090909090909, -0.0398989898989899, -0.0388888888888889, -0.0378787878787879, -0.0368686868686869, -0.0358585858585859, -0.0348484848484848, -0.0338383838383838, -0.0328282828282828, -0.0318181818181818, -0.0308080808080808, -0.0297979797979798, -0.0287878787878788, -0.0277777777777778, -0.0267676767676768, -0.0257575757575758, -0.0247474747474747, -0.0237373737373737, -0.0227272727272727, -0.0217171717171717, -0.0207070707070707, -0.0196969696969697, -0.0186868686868687, -0.0176767676767677, -0.0166666666666667, -0.0156565656565657, -0.0146464646464646, -0.0136363636363636, -0.0126262626262626, -0.0116161616161616, -0.0106060606060606, -0.0095959595959596, -0.00858585858585859, -0.00757575757575758, -0.00656565656565657, -0.00555555555555556, -0.00454545454545455, -0.00353535353535354, -0.00252525252525253, -0.00151515151515152, -0.000505050505050506, 0.0505050505050506, 0.151515151515152, 0.252525252525253, 0.353535353535354, 0.454545454545455, 0.555555555555556, 0.656565656565657, 0.757575757575758, 0.858585858585859, 0.95959595959596, 1.06060606060606, 1.16161616161616, 1.26262626262626, 1.36363636363636, 1.46464646464646, 1.56565656565657, 1.66666666666667, 1.76767676767677, 1.86868686868687, 1.96969696969697, 2.07070707070707, 2.17171717171717, 2.27272727272727, 2.37373737373737, 2.47474747474747, 2.57575757575758, 2.67676767676768, 2.77777777777778, 2.87878787878788, 2.97979797979798, 3.08080808080808, 3.18181818181818, 3.28282828282828, 3.38383838383838, 3.48484848484848, 3.58585858585859, 3.68686868686869, 3.78787878787879, 3.88888888888889, 3.98989898989899, 4.09090909090909, 4.19191919191919, 4.29292929292929, 4.39393939393939, 4.49494949494949, 4.5959595959596, 4.6969696969697, 4.7979797979798, 4.8989898989899, 5.0], "mode": "lines", "name": "Leaky ReLU (a=0.01)", "line": {"color": "#f76707", "dash": "dash"}}]}Comparison of Sigmoid, Tanh, ReLU, and Leaky ReLU activation functions. Note the different output ranges: Sigmoid (0 to 1), Tanh (-1 to 1), ReLU (0 to infinity), and Leaky ReLU (-infinity to infinity, with a small slope for negative inputs).Observing the plot, we can clearly see the characteristics discussed earlier:Sigmoid and Tanh are S-shaped and saturate (flatten out) for large positive or negative inputs. Tanh is zero-centered, which is often beneficial during training compared to Sigmoid.ReLU is linear for positive inputs and zero for negative inputs. This simplicity makes it computationally efficient but can lead to "dying ReLUs" where neurons get stuck outputting zero.Leaky ReLU addresses the dying ReLU problem by allowing a small, non-zero gradient for negative inputs, visible as a slight negative slope in the plot.This practical implementation helps connect the mathematical definitions of activation functions to their actual behavior. Understanding these differences is fundamental when deciding which activation function to use in the hidden layers and output layer of your neural network, depending on the task (e.g., binary classification often uses Sigmoid in the output, multi-class uses Softmax, regression uses linear, and hidden layers frequently use ReLU or its variants).