Showing posts with label Maths. Show all posts
Showing posts with label Maths. Show all posts

Sunday, March 12, 2023

Understanding the SIGMOID Activation Function

 

SIGMOID Activation Function

When we are building a neural network almost always the function we want to model is not linear in nature. To deal with this we need to use an activation function which introduces non-linearity into the neural network. A common activation function uses the SIGMOID function, define by the equation below.


For a given value of x the SIGMOID function will produce an output between 0 and 1. We can demonstrate this with the following R code.

library(ggplot2)

 

# Define sigmoid function

sigmoid <- function(x) {

  1 / (1 + exp(-x))

}

# Generate data

x <- seq(-10, 10, length.out = 100)

y <- sigmoid(x)

 

# Create data frame

df <- data.frame(x = x, y = y)

 

# Plot sigmoid function

ggplot(df, aes(x, y)) + geom_line() + labs(title = "Sigmoid Function", x = "x", y = "y")

Adding a weight to the SIGMOID

By adding a weight W to the sigmoid equation we can vary the gradient of the slope between 0 and 1, as shown but the R code below.


library(ggplot2)

 

x <- seq(-10, 10, length.out = 1000)

 

# Define sigmoid function

sigmoid <- function(x) {

  1 / (1 + exp(-x))

}

 

# Generate sigmoid curves for different constants

constants <- c(0.5, 1, 1.5, 2)

curves <- lapply(constants, function(c) sigmoid(c * x))

 

# Plot the curves

ggplot() +

  geom_line(aes(x, curves[[1]], color = "0.5")) +

  geom_line(aes(x, curves[[2]], color = "1")) +

  geom_line(aes(x, curves[[3]], color = "1.5")) +

  geom_line(aes(x, curves[[4]], color = "2")) +

  ggtitle("Sigmoid Function with Varying Slopes") +

  xlab("x") +

  ylab("y") +

  scale_color_manual(values = c("0.5" = "blue", "1" = "red", "1.5" = "green", "2" = "purple")) +

  theme_bw()



Bias connection

We can add another input to the activation function called bias input. Bias input is always one multiplied by a weight b. The purpose of the bias input is to move the sigmoid function either to the left or to the right as by the R code shown below.

sigmoid <- function(x, bias) {

  1 / (1 + exp(-x + bias))

}

 

# Set up plot

plot(NULL, xlim = c(-10, 10), ylim = c(0, 1), xlab = "x", ylab = "y")

 

# Plot sigmoid curves with different biases

curve(sigmoid(x, bias = -3), add = TRUE, col = "blue", lwd = 2)

curve(sigmoid(x, bias = 0), add = TRUE, col = "red", lwd = 2)

curve(sigmoid(x, bias = 3), add = TRUE, col = "green", lwd = 2)

 

# Add legend

legend("topleft", legend = c("-3", "0", "3"), col = c("blue", "red", "green"), lwd = 2)

Logistic Regression

The Sigmoid function can be used to calculate logistic regression. This kind of regression is used to calculate the probability of a binary outcome or decision based on the input.






Wednesday, March 1, 2023

What is ‘e’

Just like pie, ‘e’ is widespread in mathematics and shows up in many different places. In this post I want to explore a little about what ‘e’ is and look at how you might draw a graphical representation of it using the R programming language.

Try this.

Before we begin exploring ‘e’ try this simple exercise using a calculator:
1. Enter a memorable 7-digit number on your calculator.
2. Take the reciprocal of that number, by pressing the 1/x button.
3. Add 1 to your answer.
4. Now raise the number to the power of the original 7-digit number.

Does your answer begin 2.718? If the answer is yes then you have found ‘e’!

What is ‘e’

$$e=lim_{n\to\infty}(1+\frac{1}{n})^{n}$$ We define ‘e’ to be the number that $(1+\frac{1}{n})^{n}$ is getting closer and closer to as ‘n’ gets larger and larger. $$ e=2.718281828459045... $$ If we replace 1/n with x/n we get the following equation: $$e^{n}=lim_{n\to\infty}(1+\frac{x}{n})^{n}$$ This formula $e^{n} $ has lots of interesting properties and applications.

R code to Plot ‘e’

With the simple programme below, we can show you what the growth of $e^{n}$ looks like.

x <- seq(-5, 5, by = 0.1)
y <- exp(x)
plot(x, y, type = "l", xlab = "x", ylab = "e^x")

This produces the following graph:
$$lim_{n\to\infty} e^{n}=\infty \\ lim_{n\to-\infty} e^{n}=0 \\ lim_{n\to0} e^{n}= 1 $$ The graph shows at minus Infinity the y axis approaches 0 and at Infinity the y axis approaches Infinity. But the y axis always passes through a value of 1 when x is 0.

R code to Plot inverse ‘e’

We can also plot the graph for the inverse of ‘e’ as follows:

x <- seq(-5, 5, by = 0.1)
y <- exp(-x)
plot(x, y, type = "l", xlab = "x", ylab = "e^-x")

$$ lim_{n\to\infty} e^{n}= 0\\ lim_{n\to-\infty} e^{n}= \infty \\ lim_{n\to0} e^{n}= 1$$

Does Your Work Matter? - How a Sense of Mattering Can Transform Your Career and Well-Being

  There’s no shortage of books on positive psychology and the need for purpose in our lives. Over and over, research shows that with a str...