The Beautiful Future
[TensorFlow] TensorFlow Mechanics 101 본문
목표: feed-forward net을 학습과 평가
** Build the Graph
inference(), loss(), training() 세단계의 패턴으로 구성된다.
inference() - 예측값을 축력 할 수 있을때까지 그래프를 빌드 한다
loss() - inference 그래프에 loss을 축력 할 수 있는 op을 추가한다.
training() - loss 그래프에 그래디언트 계산이 가능한 op를 추가
** Inference
입력으로 placeholder을 받는다.
이 함수에 네임스페이스와 비슷한 name scope을 정의 하면서 변수 및 op을 생성 그래프를 빌드한다.
name scope -> with.tf.name_scope('hidden1')
weifhts = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1.0/math.sqrt(float(IMAGE_PIXELS))),
name='weights')
biases = tf.Variable(tf.zeros(hidden1_units]),
name = 'biases')
hidden1 = tf.nn.relu(tf.matmul(images, weights)+biases)
name scope -> with.tf.name_scope('hidden2')
....
위와 같이 하면 weight와 biases을 계속 생성하고 name_scope만 다르게 하면 된다.
그럼 각변수는 유니크한 hidden1/weights과 같은 이름을 갖게된다.
** Loss
로스를 정의해준다. 배치개수의 출력과 레이블을 받아 로스를 만들고 배치개수이기때문에 평균을 내준다.
**Training
학습에 사용 될 Gradient Descent를 정의하고 더해준다.
입력으로 로스와 러닝레이트를 받는다.
로스는 tf.summary.scalsr('loss', loss)로 입력하여 로그 또는 이벤트 파일에 사용 할 수 있도록 한다.
SummaryWriter을 이용하여 저장한다.
Gradient Descent를 정의
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
글로발 스텝을 정의할 변수 생성
global_step = tf.Variable(0, name='global_step', trainable=False)
Gradient Descent로 loss를 최소화하는 부분 정의
train_op = optimizer.minimize(loss, global_step=global_step)
**Train the Model
텐서플로우에게 모데이 디폴트 그래프로 빌드될 거란 걸 알려준다.?
with tf.Graph().as_default():
**The Session
그래프를 동작시킨다.
sess = tf.Session()
다르게 Sesseion은 with 블로고가 같이 만들어 질 수 있다.
with tf.Session() as sess:
'DNN' 카테고리의 다른 글
Install Caffe on Ubuntu 16.04 with GTX 1080, CUDA8.0, CUDNN5.1, NCCL, OPENCV3.1 (0) | 2016.11.24 |
---|---|
caffe solver (0) | 2016.10.26 |
caffe console command train graph (0) | 2016.10.24 |
tensor flow add op (0) | 2016.10.05 |
우분투 팁 (0) | 2016.09.30 |