본문 바로가기

AI & Deep Learning/실습

tensorflow 간단한 실행 - Constant

상수(Constant) 이용  / Computational Graph

 

예제 일단 따라해보기

ex1>

 


(tensorflow) $ python

python / import tensorflow as tf
sess = tf.Session()

 >>> import tensorflow as tf                #tensorflow 임포트

>>> hello = tf.constant(‘Hello, TensorFlow!’)

>>> sess = tf.Session()

>>> print(sess.run(hello))

 

Hello, TensorFlow!

 

Q. tf.constant  에서 constant가 무엇인지 궁금해 짐

A. tensorflow 에서의 상수

 

import tensorflow as tf

tf.constant(x)를 시용하여 상수 선언.

 

 

- 출력 방법2가지(ex. 정수)

1) 그냥 x 값 그대로 출력

2) tensorflow 에서 제공하는 session 실행 해 출력

 

 

1) 그냥 x 값 그대로 출력

>>> x=tf.constant(3)

>>> print(x)

 

Tensor(“Const:0”, shape=(), dtype=int32)

 

: type이 int32 이고 shape가 값이 없는 real number(scalar) 로 구성된  tensor를 출력

 

2) tensorflow 에서 제공하는 session 실행 해 출력

>>> sess = tf.Session()

>>> result = sess.run(x)

>>> print(result)

 

3

 

: tensorflow 는 이런  session 안에서만 실제적 연산, 로직을 수행하도록 되어 있음

: tensor는 다양한 차원의 데이터 표현, 연산을 위한 기본 데이터 객체로, 우리가 생성하는 모든 데이터는 이 tensor 로 생성된 후에 모든 연산에 사용 되는 것

 

: tensorflow 의 기본 동작은 이러한  op 들이 session 안에서만 실제적으로 실행되도록 하는 것

: session밖에서는 정의, graph build. / 실행과는 무관

 


 ex2>

node1,2,3
adder_node

 

 

텐서플로우의 기본 동작


1. graph build : node1, node2, node3 만들기 

graph build

2. sess.run(op) : print 아니고 노드에 op 를 넣어 실행


3. 계산값 출력: return

Constant

 

 

 

출처 : 모두의 딥러닝 https://hunkim.github.io/ml/

'AI & Deep Learning > 실습' 카테고리의 다른 글

linear regression 구현_tensorflow  (0) 2019.04.01
tensorflow 간단한 실행 - Placeholder  (0) 2019.04.01