티스토리 뷰
출처
http://jorditorres.org/first-contact-with-tensorflow/#cap2 (First contact with tensorflow)
https://tensorflow.rstudio.com/ (Tensorflow™ for R)
https://github.com/rstudio/tensorflow (GitHub - rstudio/tensorflow)
http://motioninsocial.com/tufte/ (Tufte in R)
http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2) (Cookbook for R)
좌표 값 생성
num_points <- 1000
x_data <- rnorm(num_points, 0, 0.55)
y_data <- 0.3 + 0.1 * x_data + rnorm(num_points,0,0.03)
plot(y_data~x_data)
require(ggplot2);require(ggthemes)
## Loading required package: ggplot2
## Loading required package: ggthemes
ggplot(data.frame(x_data,y_data),aes(x_data,y_data)) + theme_tufte() + geom_point(size=2.5,alpha=.3)
변수, loss, 최적화 알고리즘 설정.
require(tensorflow)
## Loading required package: tensorflow
# 변수 설정
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b
# loss function 설정 (MSE : Mean Squared Error)
loss <- tf$reduce_mean(tf$square(y - y_data))
# 최적화 알고리즘 설정 (경사하강법)
optimizer <- tf$train$GradientDescentOptimizer(0.5)
train <- optimizer$minimize(loss)
알고리즘 실행
# 변수 초기화
init <- tf$initialize_all_variables()
sess <- tf$Session()
sess$run(init)
# 8번 반복
iter<-8
result<-numeric(8*2);dim(result)<-c(8,2)
for(step in 1:iter){
sess$run(train)
cat(step,"-",sess$run(W),sess$run(b),"Loss =",sess$run(loss),"\n")
result[step,]<-c(sess$run(W),sess$run(b))
}
## 1 - -0.3698774 0.3166558 Loss = 0.06719695
## 2 - -0.2287394 0.311432 Loss = 0.03330565
## 3 - -0.1300698 0.3079748 Loss = 0.0167439
## 4 - -0.06109463 0.3055579 Loss = 0.008650606
## 5 - -0.0128774 0.3038683 Loss = 0.004695628
## 6 - 0.02082895 0.3026872 Loss = 0.002762934
## 7 - 0.04439145 0.3018616 Loss = 0.001818477
## 8 - 0.06086286 0.3012844 Loss = 0.001356946
최적화 과정 plot으로 확인하기
require(ggplot2);require(ggthemes)
temp_plots<-list()
for(i in 1:iter){
title_name <- paste("Iteration =",i)
temp_plots[[i]]<-ggplot(data.frame(x_data,y_data),aes(x_data,y_data)) +
theme_tufte() + geom_point(size=1,alpha=.3) + geom_abline(slope=result[i,1],intercept=result[i,2],col=2) + ggtitle(label = title_name)
}
multiplot(temp_plots,cols=2)
참고코드
Multiple graphs on one page (ggplot2)
출처 : Cookbook for R (Multiple graphs on one page (ggplot2))
http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
'Tensorflow > Tensorflow for R' 카테고리의 다른 글
Convolutional Neural Network (ver.R) (0) | 2017.08.14 |
---|---|
단일신경망 Single Layer Neural Network (ver. R) (0) | 2017.06.24 |
군집화 k-means Clustering (ver.R) (0) | 2017.06.08 |
기본 자료 구조 : 텐서 (0) | 2017.06.06 |
Tensorflow for R 설치해보기 (0) | 2017.06.05 |