본문 바로가기

Deep Learning

TensorFlow - 변수형 종류, 행렬 다루기

@markdown

# TensorFlow 변수형 종류, 행렬 다루기

____

## TensorFlow 상수형

- `tf.constant(value, dtype=tf.float32, shape=None, name='const', verify_shape=False)`


- value : 지정할 상수 값

- dtype : 상수형 데이터(실수 정수 등)

- shape : 행렬의 차원 지정, (shape = [3,3]은 3x3 행렬을 저장)

- name : 상수 선언 후 사용할 상수 이름


## TensorFlow Placeholder 변수

____

- `tf.placeholder(dtype, shape, name)`

<pre><code class="python" style="font-size:14px">x = tf.placeholder(tf.float32, shape=[None, 784])

</code></pre>

- x에 값이 할당한 것이 아닌, 실수값들의 2D 텐서이다.

- 첫번째 차원의 크기를 `None`으로 한 것은 여기서 정하지 않는 것을 의미(None x 784)

- 데이터 입력이 되지 않는 `비어있는` 상태이다.


- `sess.run(tf.global_variables_initializer())`

- 변수들을 세션이 시작되기 전에 초기화 시켜주는 코드로, 위에서 지정한 미리 정해진 초기 값으로 넣어주는 역할을 한다.


## TensorFlow 데이터 입력(피딩, feeding)

____

- `sess.run(graph, feed_dict={변수 : 입력 데이터})`


<pre><code class="python" style="font-size:14px"># tensorflow 모듈 import

import tensorflow as tf

# 입력 데이터 배열 정의

input_data = [1,2,3,4,5]

# x를 float32형 placeholder로 지정

x = tf.placeholder(dtype=tf.float32)

# y 그래프 정의

y = x * 2

# 세션 정의

sess = tf.Session()

# y 그래프에 입력할 x 값에 데이터 feeding 하면서 동시에 세션이 실행된다. 

result = sess.run(y,feed_dict={x:input_data})


print(result)

</code></pre>

- 위 코드 실행 결과, feed_dict에 의해 정해진 피드 데이터를 하나씩 읽어 곱하기 2한 값을 출력한다. 


## TensorFlow Variable 변수

____

- 흔히 다른 프로그래밍 언어에서 사용하는 데이터형 변수이다.


<pre><code class="python" style="font-size:14px">import tensorflow as tf


# feeding 값 선언

input_data = [1,2,3,4,5]

# placeholder 변수 선언

x = tf.placeholder(dtype=tf.float32)

# W = 2라는 값 선언

W = tf.Variable([2],dtype=tf.float32)

y = W * x


sess = tf.Session()

# 변수 초기화 코드를 세션에 전달한다.

sess.run(tf.global_variables_initializer())

# y 그래프에 input_date feeding 하면서 세션 실행한다.


result = sess.run(y,feed_dict={x:input_data})

print(result)

</code></pre>


- 위 코드 실행결과 input_data 각 값에 * W 해준 결과 값이 나온다.

> 실행결과 : [  2.   4.   6.   8.  10.]

- TensorFlow의 변수형은 세션 실행전에 반드시 초기화를 해주어야한다. 


## TensorFlow 상수 행렬 표현

____

- `1x3 * 3x1` 행렬곱 결과는 `1x1`이다.

<pre><code class="python" style="font-size:14px">import tensorflow as tf

# x = 1x3 상수 행렬 선언

x = tf.constant([[1.0,2.0,3.0]])

# w = 3x1 상수 행렬 선언

w = tf.constant([[2.0],[2.0],[2.0]])

# matmul() 함수를 사용한 행렬곱

y = tf.matmul(x,w)

sess = tf.Session()

init = tf.global_variables_initializer()

sess.run(init)

result = sess.run(y)


print(result)

</code></pre>

> 실행결과 : [[ 12.]]

## TensorFlow 변수 행렬 표현

____

<pre><code class="python" style="font-size:14px">import tensorflow as tf

x = tf.Variable([[1.0, 2.0, 3.0]], dtype=tf.float32)

w = tf.Variable([[2.0],[2.0],[2.0]], dtype=tf.float32)

y = tf.matmul(x, w)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

result = sess.run(y)

print(result)

</code></pre>

> 실행결과 : [[ 12.]]

## TensorFlow 변수 & placeholder 행렬 표현

____


<pre><code class="python" style="font-size:14px">import tensorflow as tf

# feeding 데이터 선언

input_data = [[1.0, 2.0, 3.0],[4.0, 5.0, 6.0],[7.0, 8.0, 9.0]]

x = tf.Variable([[2.0],[2.0],[2.0]], dtype=tf.float32)

w = tf.placeholder(dtype=tf.float32, shape=[None, 3])

y = tf.matmul(w, x)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

# 위에서 선언한 placeholder에 데이터 feeding 해주는 과정

result = sess.run(y, feed_dict={w:input_data})

print(result)

</code></pre>

- placeholder에 input_data feeding한 실행결과 

>[[ 12.]

 >[ 30.]

 >[ 48.]] 이 출력된다.


## TensorFlow 행렬 용어

____

- Scalar : 상수(s = 222), 0차원

- Vector : 벡터(v = [1, 2, 3]), 1차원

- Matrix : 2차원 행렬(m = [[1, 2, 3],[4, 5, 6,],[7, 8, 9]]), 2차원

- 3-Tensor : 큐브( [[1], [2], [3]],  [[4], [5], [6]], ...  ]) 3차원(장난감 큐브)