05-02 06:32
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Artificial Intelligence / TensorFlow] R-TensorFlow 예제 - HelloWorld 본문

Artificial Intelligence/TensorFlow

[Artificial Intelligence / TensorFlow] R-TensorFlow 예제 - HelloWorld

cinema4dr12 2017. 4. 11. 11:59

by Geol Choi | 


이번 시리즈부터 R-TensorFlow 예제를 하나씩 정리해 나가기로 한다. 만약 R에 TensorFlow 개발 환경이 구축되어 있지 않다면 R에서 TensorFlow 개발환경 구축하기을 참고하기 바란다.

이번 포스팅은 모든 프로그램 예제 중의 예제 HelloWorld의 R-TensorFlow 버전이다.

이 예제는 단순히 화면에 "Hello, TensorFlow!"를 출력한다.


TensorFlow 라이브러리 로딩하기

TensorFlow 패키지가 현재 환경에 설치 되어있는지 확인하고 만약 설치되어 있지 않으면 설치하고, 해당 패키지 라이브러리를 로딩한다:


R CODE:

# import library if (! ("tensorflow" %in% rownames(installed.packages()))) { install.packages("tensorflow") } base::library(tensorflow)


constant 오퍼레이션 정의

라이브러리가 로딩되면, 모든 TensorFlow 오퍼레이션(op)은 tf라는 컨테이너를 통해 이루어진다.

다음과 같이 hello 변수에 constant op를 통해 문자열을 저장한다:


R CODE:

# Simple hello world using TensorFlow # Create a Constant op # The op is added as a node to the default graph. # # The value returned by the constructor represents the output # of the Constant op. hello <- tensorflow::tf$constant('Hello, TensorFlow!')


사실 hello는 TensorFlow에서 변수라는 개념보다는 하나의 Operation이라고 정의하는 것이 맞다. 왜냐하면 TensorFlow는 Operation을 통해 모든 연산이 일어나기 때문이다.


TensorFlow 세션 시작

다음의 코드를 통해 TensorFlow 세션을 시작한다:


R CODE:

# Start tf session sess <- tensorflow::tf$Session()


오퍼레이션 실행

다음과 같이 TensorFlow Operation을 실행한다. Operation 대상은 hello Constant이다:


R CODE:

# Run the op base::print(sess$run(hello))


이를 실행한 출력결과는 다음과 같다:

b'Hello, TensorFlow!'


전체 소스 코드

다음 코드는 지금까지 실행한 R-TensorFlow의 전체 코드이다:


R Code:


Comments