반응형
0. 가상환경 설치
...더보기
conda env list
conda create -n tensorflow tensorflow jupyter notebook matplotlib
conda install notebook ipykernel
python -m ipykernel install --user --name tensorflow --display-name "Tensorflow"
conda activate tensorflow
1. Tensorflow
- OpenSource software library for numerical computation using data - flow graphs
- Computational graph? tensor + operation
- Tensors? Data의 배열, Graph의 edge 통해 value가 이동한다.
- Operations ? value를 실제 받아서 연산하는 것
- 1. Build the Model
- 2. Run the Model
Tensorflow는 이 두 가지의 반복!
2. Components in Tensorflow
-
Constant : 상수, 고정된 수
-
placeholder : input Data, 입력 데이터를 받는 Tensor
-
EX) 강아지 고양이 분류 학습 시, Placeholder는 학습할 이미지(를 말한다.
-
-
Variable : parameter, A mutual variable, 학습하는 모델의 파라미터
- y = Wx + b 일 때, 학습시키길 원하는 값인 W가 Variable, X가 placeholder(input data)
3. Building Computational Graph (Constant)
- Constant 선언
import tensorflow as tf
#constant : 변하지 않는 상수, 직접 값 넣어주고 데이터 타입 설정
a = tf.constant(3.0, dtype = tf.float32)
b = tf.constant(4.0)
# total = constant더해준 값
total = a + b
print(a)
print(b)
print(total)
# session 을 열어줘서 run을 해줘야 실제 값을 볼 수 있음.
sess = tf.Session()
print(sess.run(total))
print(sess.run([a,b]))
sess.close()
a = tf.constant([3.0,4.0], dtype=tf.float32)
sess = tf.Session()
print(sess.run(a))
sess.close()
- Placeholder 선언
# placeholder : 데이터를 담고 있는 Tensor, 값을 넣어주지 않은 깡통
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
# 실제 값은 나오지 않음
print(x)
print(y)
print(z)
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
sess = tf.Session()
# feed_dict : placeholder는 실제 반복할 데이터 그릇이기 때문에 데이터를 넣어줘야함.
print(sess.run(z, feed_dict = {x:3, y:4.5}))
sess.close()
x1 = tf.placeholder(tf.float32, shape = [2])
y1 = tf.placeholder(tf.float32, shape = [2])
z1 = x1 + y1
sess = tf.Session()
print(sess.run(z, feed_dict = {x:[1,3], y:[2,4]}))
sess.close()
- Variable 선언
방식1. Variable
# variable
# 방식 1 - .Variable
my_variable = tf.Variable("my_variable",[1,2,3])
print(my_variable)
방식2. get_variable : 이 방식 추천! Variable은 실수하기 쉬움.
# variable
# 방식 2 - .get_variable
my_variable = tf.get_variable("my_variable",[1,2,3])
# .get_variable은 초기값 설정이 가능
my_int_variable = tf.get_variable("my_int_variable",[1,2,3],dtype=tf.int32, initializer=tf.zeros_initializer)
print(my_variable)
print(my_int_variable)
- .Variable 는 언제나 새로운 객체를 만들어 낸다. 이미 같은 이름의 객체가 있다면 _1, _2 등을 붙여서 유니크(uniqu)하게 만든다.
- .get_variable 는 이미 존재하는 객체를 매개변수로 받을 수도 있다.(변수는 같은 객체를 가리키게 된다.) 그런데, 해당 객체가 없다면 새로운 객체로 만들어 낸다. 덮어쓰기 안됨.
반응형
댓글