내용

글번호 798
작성자 허진경
작성일 2018-01-19 06:33:17
제목 활성화 함수 그리기 (activation function)
내용 코드와 결과
x <-seq(-3.5, 3.5,0.01)
y <- x*x
plot(x,y, type="n", bty="n", yaxt="n",xaxt="n")
lines(x,y)
arrows(3, 9, 2.2, 4, col="red", lwd=2, length=0.1)
points(0, 0, pch=20)
points(3, 9, pch=20, col="red")



#http://vis.supstat.com/2013/04/mathematical-annotation-in-r/ 에서 수식 참고
#install.packages("tidyverse")
#install.packages("ggplot2")
#install.packages("devtools")
#devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
x = seq(-6, 6, 0.01)
y = tanh(x)
ggplot() + 
  geom_point(aes(x, y), size=0.1) +
  ggtitle("tanh(x)") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=-6, y=1, hjust=0, vjust=1,
           label="tanh(italic(x))~'='~frac(2, 1+italic('e')^{'-2'~italic(x)})~-1",
           parse=TRUE, size=4.5) 

x = seq(-6, 6, 0.01)
y = 1/(1+exp(-x))
ggplot() + 
  geom_point(aes(x, y), size=0.1) +
  ggtitle("Sigmoid") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=-6, y=1, hjust=0, vjust=1,
           label="italic(f(x))~'='~frac(1, 1 + italic('e')^{'-'~italic(x)})",
           parse=TRUE, size=4.5) 

x = seq(-6, 6, 0.01)
y = ifelse(x<0, 0, x)
ggplot() + 
  geom_point(aes(x, y), size=0.1) +
  ggtitle("ReLU") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=-6, y=6, hjust=0, vjust=1,
           label="italic(f(x))~'='~bgroup('{',atop('0    for '~italic(x)~'<0', 'x    for '~italic(x)~'\u22650'),'')",
           parse=TRUE, size=4.5) 

x = seq(-4, 4, 0.01)
y = ifelse(x<0, 0, 1)
ggplot() + 
  geom_point(aes(x, y), size=0.1) +
  ggtitle("Binary step") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=-4, y=2, hjust=0, vjust=1,
           label="italic(f(x))~'='~bgroup('{',atop('0    for '~italic(x)~'<0', '1    for '~italic(x)~'\u22650'),'')",
           parse=TRUE, size=4.5) 

x = seq(-4, 4, 0.01)
y = exp(-(x**2))
ggplot() + 
  geom_point(aes(x, y), size=0.1) +
  ggtitle("Gaussian") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=-4, y=2, hjust=0, vjust=1,
           label="italic(f(x))~'='~italic(e)^{'-'~italic(x)^{'2'}}",
           parse=TRUE, size=4.5) 


x = seq(0, 20, 1)
softmax <- function(input) {
  return (exp(input) / (sum(exp(input))))
}
y = softmax(x)

ggplot() + 
  geom_line(aes(x, y), size=1) +
  ggtitle("Softmax") +
  theme(plot.title = element_text(hjust=0.5),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank()) +
  annotate('text', x=0, y=1, hjust=0, vjust=1,
           #label="sum(italic('e')^{italic('x')[italic('j')]}, j==1, J)",
           label="italic(f(x))~'='~frac(italic('e')^{italic('x')[italic('i')]}, sum(italic('e')^{italic('x')[italic('j')]}, italic('j')==1, italic('J')))~'   '~'for '~italic(i)~'=1, ..., '~italic('J')",
           parse=TRUE, size=4.5)