Tensorflow 2.x

Published by onesixx on

Tensorflow 1.x 와 2.x :: [Book] Deep-learning-with-TensorFlow-2-and-Keras

https://community.rstudio.com/t/recreating-tensorflow-tutorials-in-r/5061

https://tensorflow.rstudio.com/guide/tensorflow/eager_execution/

https://blogs.rstudio.com/ai/posts/2019-11-27-gettingstarted-2020/ – Getting started with Keras from R – the 2020 edition
https://blogs.rstudio.com/ai/posts/2019-10-08-tf2-whatchanges/ – TensorFlow 2.0 is here – what changes for R users?

https://www.r-bloggers.com/2019/07/getting-started-with-tensorflow-keras-in-python-and-r/

tf$keras vs. Keras

기본적으로 High level API인 tf$keras를 사용하라

tensorflow와 함께 keras를 백엔드로 사용 <또는> Tensorflow에서 직접 사용가능한 tf.keras의 API를 사용?
Keras 와 tf.keras간의 1:1 대응이 없다.
Keras는 tensorflow (google)뿐만 아니라, CNTK(ms), MXnet(amazon), Theano (mila) 등을 백엔드에서 실행가능하지만, tf.keras는 여러 백엔드를 지원하지 않는다.
tf.keras의 endpoint는 (Keras에 미구현되있다.)

tf$keras vs. tensorflow 2.x 의 이점

즉시실행 (eager execution)

https://tensorflow.rstudio.com/guide/tensorflow/eager_execution/

하이레벨 API의 즉시실행

(여전히 그래프는 있지만 정적으로 정의할 필요없이) (session이나 placeholder 없이도)
Node를 즉시 정의/변경/실행 가능

모든 tf$keras API가 즉시실행 및 호환된다.

오토그래프

install.packages(“tfautograph”)
This package implements autograph for R.

측시 실행 python코드를 가져와서 자동으로 그래프생성 코드로 변환 해 준다
성능을 높이고 이식성이 좋은 모델을 만들려면 tf.function을 사용해 그래프로 변환하세요

@tf$function 을 추가해, AutoGraph로 그래프 모드에서 효율적으로 수행되도록 .
고급계산으로 호출된 모든 함수는, 자동으로 주석이 달린다.

(tf$get_variable 대신) tf$Variable을 사용하라.

http://schoolofweb.net/blog/posts/tag/python/

퍼스트클래스 함수: 프로그래밍 언어가 함수(function)를 first-class citizen으로 취급하는 것
함수 자체를 인자 (argument) 로써 다른 함수에 전달하거나 
다른 함수의 결과값으로 리턴 할수도 있고,
함수를 변수에 할당하거나 데이터 구조안에 저장할 수 있는 함수를 뜻함.
클로저(closure): 퍼스트클래스 함수를 지원하는 언어의 네임 바인딩 기술
어떤 함수를 함수 자신이 가지고 있는 환경과 함께 저장한 레코드
하나의 함수로 여러 다른 함수를 쉽게 만들수 있고, 기존함수나 모듈의 수정없이
wrapper 함수를 통해 커스터마이징할 수 있게 해줌.
데코레이터: 기존의 코드에 여러가지 기능을 추가하는 파이썬 구문
기존의 코드를 수정하지 않고도, 래퍼(wrapper) 함수를 이용하여 여러가지 기능을 추가

import tensorflow as tf





def linear_layer(x):
  return 3*x+2


@tf.function  
def simple_nn(x):
  return tf.nn.relu(linear_layer(x))

def simple_function(x):
  return 3*x

simple_nn
# 
simple_function
# 
library(tensorflow)
library(tfautograph)
library(keras)
library(tfdatasets)
# tf$executing_eagerly()

linear_layer <- function(x){
  return (3*x+2)
}

simple_nn <- autograph(function(x){
  return(tf$nn$relu(linear_layer(x)))
})

simple_function <- function(x){
  return(3*x)
}



분산

tpu 훈련지원

Keras vs. tf$keras

tensorflow2.0은 Training과 Service 로 구성된 개발생태계
분산전략을 통해 CPU, GPU, TPU에서 Training이 이루어짐.

  • Training : High level 라이브러리 - tft.data
    Low level 라이브러리 - tf.* High level 라이브러리인 tf.keras와 Estimators도 tf.*로 접근가능
    preTrained Model - tf.hub 즉시 사용가능한
    데이터셋 - tft.data 효율적으로 모델 로드
  • Serving : op-prem, cloud,
    Android, iOS, RaspberryPie,
    Node.js.....등 여러 플랫폼에서 실행된다.
tensorflow 2.0 Architecture
> simple_nn
function(x){
  return(tf$nn$relu(linear_layer(x)))
}
>

> simple_function
function(x){
  return(3*x)
}
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