Keras in R

Published by onesixx on

https://keras.rstudio.com/articles/examples/index.html
https://github.com/rstudio/cheatsheets/raw/master/keras.pdf

빠른 구현에 초점이 맞춰진, Human이 이해하기 쉬운 Neural networks API 
– CPU/ GPU 에 대해 같은 Code를 사용한다.
– Deep learning 모델을 빠르게 prototype할수 있는 쉬운 API
– 여러 back-ends (TensorFlowCNTK, or Theano.)에서 실행가능. (default는 TensorFlow)

Keras Template by Sixx

source(file.path(getwd(),"../00.global_dl.R"))
### Title: --- --- --- -- --- --- --- --- --- --- --- --- --- --- --- --- --- --
#
library(reticulate)
library(keras)
#.rs.restartR()  # cmd+shift+F10
use_condaenv(condaenv='sixxDL', required=T) #cf.> use_python, use_virtualenv, use_miniconda
use_python(python="~/.local/share/r-miniconda/envs/sixxDL/bin/python" )
###### For mac #######
#use_backend(backend="plaidml") #  (cf. "tensorflow", "cntk", "theano")

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Problem definition ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` ` Load Input data (X, Y) -------------------------------------------------
## learning: Supervised / Unsupervised / Self-supervised / Reinforcement 
## classification(binary, Multiclass), regression (scala, vector), clustering ..
## 기본가정: 미래에도 과거와 같은 패턴일 것이다 (non-stationary problems)
##           입력데이터에 따라 결과를 예측할수 있다. 
##           입력데이터는 충분한 정보를 제공한다.

### ` ` EDA / plotting ---------------------------------------------------------

### ` ` Preprocess -------------------------------------------------------------
##  Vectorization: 데이터는 tensor 형식이어야한다. 
##  Rrescale : 일반적으로 [-1,1] 또는 [0,1]
##  Normalization: X의 단위 맞춤
##  Missing Value 
##  Feature engineering


###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Train the model ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` ` Build/Reshape/complie the model ----------------------------------------
## Problem type:Last-layer activation: Loss function
## Binary classification : sigmoid :binary_crossentropy
## Multiclass, single-label classification:softmax : categorical_crossentropy
## Multiclass, multilabel classification:sigmoid: binary_crossentropy
## Regression to arbitrary values :None: mse
## Regression to values between 0 and 1:sigmoid: mse or binary_crossentropy

### ` ` Train(fitting) the model : history, summary ----------------------------

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Evaluation with baseline----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` ` Evaluate accuracy -------------------------------------------------------

### ` ` Improve the model -------------------------------------------------------

### ` ` Overfitting and underfitting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Reducing the network’s size/ Adding weight regularization / Adding dropout

## 평가지표(Metric) =  measure of success ~~~~
## balanced-classification problems: Accuracy, ROC AUC(receiver operating characteristic curve, Area Under ROC)
## class-imbalanced classification problem: Accuracy, precision-Recall(재현율)
## ranking/ multilabel classification problem: mAP(mean average precision)

## evaluation protocol ~~~~
## 데이터 충분 : hold-out validation set
## 데이터 부족 : K-fold CV(cross-validation)
## 데이터 소량 : iterated K-fold CV

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Tune the model----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

### ` ` developing a model that overfits -------------------------------------------
## Add layers.
## Make the layers bigger.
## Train for more epochs.

### ` `  Regularizing & Tuning your hyperparameters
# Add dropout.
# Try different architectures: add or remove layers.
# Add L1 and/or L2 regularization.
# Try different hyperparameters (such as the number of units per layer / the learning rate of the optimizer)
# Optionally, iterate on feature engineering: add new features, or remove features 


###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Make predictions ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` ` explain Model ------------------------------------------------------------
### ` ` NEW DATA predictions -----------------------------------------------------
###### Title: ~~~
source(file.path(getwd(),"../00.global_dl.R"))

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### 10. Load data ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` === Raw Data -------------------------------------------------------------
### ` === EDA ------------------------------------------------------------------
### ` === Preprocess : Normalize / rescale /  ----------------------------------
### ` === INPUT LAYER ----------------------------------------------------------

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### 20. Train the model ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` === Build/Reshape/complie the model --------------------------------------
### ` === Train(fitting) the model : history, summary --------------------------

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### 30. Evaluation ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` === Evaluate accuracy ----------------------------------------------------
### ` === Improve the model ----------------------------------------------------

###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### 40. Make predictions ----
###### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ` === explain Model --------------------------------------------------------
### ` === NEW DATA predictions -------------------------------------------------
# Library ----------------------------------------------------------------------
pList<- c(
  'tidyverse', 'data.table','fst', 'stringr', 'glue',
  'lubridate', 'anytime','hms', 'zoo', # DateTime manipulation
  'egg', 'plotly', 'hrbrthemes', 'ggpmisc',
  'reticulate','keras'
)

pList_new <- pList[!(pList %in% installed.packages()[,"Package"])]
if(length(pList_new)){
  install.packages(pList_new, dependencies=T)
  sapply(pList, require, character.only=T)
} else{
  sapply(pList, require, character.only=T)
}
rm(pList,pList_new)

use_condaenv(condaenv='sixxDL', required=T)
if (Sys.info()["sysname"]=="Darwin"){
\tuse_backend(backend="plaidml")
} else {
\tuse_backend(backend="tensorflow")   # cf. "cntk", "theano"
}

# Set CONSTANT Value ---------------------------------------------------------------
options(encoding="UTF-8")
options(digits=16, scipen=666, max.print=666, digits.secs=6)
Pjt_PATH =getwd()
DATA_PATH=file.path("~","DATA",str_split(Pjt_PATH, "/")[[1]] %>% tail(1))
ifelse(dir.exists(DATA_PATH), FALSE, dir.create(DATA_PATH))

theme_set(
  hrbrthemes::theme_ipsum(base_size=9) +
    theme(
      #legend.position = "none",
      #axis.ticks = element_blank(),
      #panel.grid = element_blank()
      panel.background = element_rect(fill="white")
    )
)

# Function ---------------------------------------------------------------------
uF_checkFolder <- function(Folder){
  ifelse(dir.exists(Folder), FALSE, dir.create(Folder))
}
https://keras.rstudio.com/
https://www.tensorflow.org/guide/keras/overview?hl=ko( python)

개요

What is TensorFlow ?

복잡한 계산을 여러개의 GPU/ CPU가 수행하게 하는 ML의 Open-source 라이브러리
by Google Brain Team.

What is Keras ?

여러 back-end들(TensorFlow, CNTK, or Theano) 위에서 동작할수 있는 Neural Networks API.
CNN, RNN같은 어려운 model을 쉽게 build할 수 있게 해준다.

  • DeepLearning 모델을 개발할수 있는 고급 구성요소를 제공하는 모델 수준 라이브러리
  • 저수준 연산(Tensor연산 , 미분...)은 처리하지 않는다.

install

#install.packages('reticulate')
library(reticulate)

###### For Ubutun #######  
###### ~~~~~~~~~~~~~~~~~~~~~~~ miniconda ---------------------------------------
###### 10. Install miniconda ---------------------------------------------------
# install_miniconda()
# miniconda_path()            # "/home/oschung/.local/share/r-miniconda"

if (Sys.getenv('RETICULATE_MINICONDA_PATH')=="") {
\tSys.setenv('RETICULATE_MINICONDA_PATH'=miniconda_path())
} else {
\tSys.getenv('RETICULATE_MINICONDA_PATH')
}
# Sys.setenv(RETICULATE_PYTHON = "python/bin/python")

###### 20. Create/Add my conda(virtual)env -------------------------------------
# conda_binary()       # "/home/oschung/.local/share/r-miniconda/bin/conda"

# conda_create(envname="sixxDL")
# $ conda create --name sixxDL python=3.6
# $ conda activate sixxDL
# $ conda install -c conda-forge scipy=1.4.1

# conda_remove(envname="sixxDL")  #  conda_remove(envname="sixxDL", conda=conda_binary())

# conda_list()                    # conda_list(conda=conda_binary())

###### 30. Activate my conda(virtual)env ---------------------------------------
use_condaenv(condaenv='sixxDL', required=T) #cf.> use_python, use_virtualenv, use_miniconda
#use_python(python="~/.local/share/r-miniconda/envs/sixxDL/bin/python" )

###### 40. ETC -----------------------------------------------------------------
py_config()
# py_install("pandas")
# py_install("matplotlib")
# py_install("scikit-learn")
#.rs.restartR()  # cmd+shift+F10

###### ~~~~~~~~~~~~~~~~~~~~~~~ Keras -------------------------------------------
# install.packages("keras")
library(keras)
#install_keras(method="conda", tensorflow="gpu")
#install.packages('reticulate')
library(reticulate)

###### For mac #######
###### ~~~~~~~~~~~~~~~~~~~~~~~ miniconda ---------------------------------------
###### 10. Install miniconda ---------------------------------------------------
# install_miniconda()
# miniconda_path()

if (Sys.getenv('RETICULATE_MINICONDA_PATH')=="") {
\tSys.setenv('RETICULATE_MINICONDA_PATH'=miniconda_path())
} else {
\tSys.getenv('RETICULATE_MINICONDA_PATH')
}
# Sys.setenv(RETICULATE_PYTHON = "python/bin/python")

###### 20. Create/Add my conda(virtual)env -------------------------------------
# conda_binary()

# conda_create(envname="sixxDL")
# $ conda create --name sixxDL python=3.6
# $ conda activate sixxDL
# $ conda install -c conda-forge scipy=1.4.1

# conda_remove(envname="sixxDL")  #  conda_remove(envname="sixxDL", conda=conda_binary())

# conda_list()                    # conda_list(conda=conda_binary())

###### 30. Activate my conda(virtual)env ---------------------------------------
use_condaenv(condaenv='sixxDL', required=T) #cf.> use_python, use_virtualenv, use_miniconda 

###### 40. ETC -----------------------------------------------------------------
py_config()
# py_install("pandas")
# py_install("matplotlib")
# py_install("scikit-learn")
#.rs.restartR()  # cmd+shift+F10

###### ~~~~~~~~~~~~~~~~~~~~~~~ Keras -------------------------------------------
# install.packages("keras")
library(keras)
# install_keras(method="conda", tensorflow="gpu")
# tensorflow::install_tensorflow(method="conda")
# conda_install('sixxDL', 'plaidml-keras')
use_backend(backend="plaidml") #  (cf. "tensorflow", "cntk", "theano")
Categories: Keras

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x