CS/Deep Learning

[Deep Learning] 가중치 초기화 & Check Point

[Deep Learning] 가중치 초기화 & Check Point

 

가중치 초기화 (Initialize Weights)

 초기 가중치 값은 딥러닝 학습에서 중요한 문제다. 가중치 설정을 잘못할 경우 Vanishing Gradient 등 문제를 야기할 수 있기 때문이다. 그럼 이러한 초기 가중치를 설정하는 방법들을 알아보자.

 

Randomly

 

# Random normal
w1 = tf.Variable(tf.random_normal(shape=[784, 256]))

 

Uniform distribution

 

# Random uniform
w2 = tf.Variable(tf.random_uniform(shape=[784, 256]))

 

Xavier

 가장 성능이 뛰어난 weight 초기화 값

# Xavier
w3 = tf.get_variable("w3", shape=[784, 256], initializer = tf.contrib.layers.xavier_initializer())

 

 

 

Checkpoint

 model을 학습시킬때 매우 큰 데이터가 있으면 시간이 아주 오래걸린다. 따라서 모델을 저장해두고 나중에 다시 불러와서 학습시킬수 있게 모델을 저장하는 방법이 있다.

 

우선 저장되있는 데이터가 checkpoint파일이 없으므로 save로 저장을 해준다.

 

import tensorflow as tf

# data
# model
# loss function and optimizer

ckpt_path = './model/scene.ckpt'
with tf.Session as sess:
	saver = tf.train.Saver()
    
    # training
    
    saver.save(sess, ckpt_path)

 

저장 후, 다시 모델을 불러올 때, restore를 사용한다.

 

import tensorflow as tf

# data
# model
# loss function and optimizer

ckpt_path = './model/scene.ckpt'
with tf.Session as sess:
	saver = tf.train.Saver()
    saver.restore(sess, ckpt_path)
    
    # training
    
    saver.save(sess, ckpt_path)
728x90
반응형