Save And Restore - pai-plznw4me/tensorflow_basic GitHub Wiki
Tensorflow ์์ ์ ์ฅํด์ผ ํ ๊ฒ๋ค์ ํฌ๊ฒ 2๊ฐ์ง๊ฐ ์์ต๋๋ค. Graph(Operation, Tenor), Variable
Tensorflow native
Checkpoint
์ค์ง Variable
๋ด value ๋ง ์ ์ฅํฉ๋๋ค. Graph ๋ ์ ์ฅ ํ์ง ์์ต๋๋ค.
Checkpoints are just the weights
Save
batch_xs = [0.1, 0.2](/pai-plznw4me/tensorflow_basic/wiki/0.1,-0.2)
dense = Dense(2, 2, 'relu')
print(dense(batch_xs))
chkp_path = 'chk_point'
checkpoint = tf.train.Checkpoint(model=dense)
checkpoint.write(chkp_path)
Load
chkp_path = 'chk_point'
dense = Dense(2, 2, 'relu')
checkpoint = tf.train.Checkpoint(model=dense)
checkpoint.restore(chkp_path)
batch_xs = [0.1, 0.2](/pai-plznw4me/tensorflow_basic/wiki/0.1,-0.2)
print(dense(batch_xs))
SavedModel
Graph ์ tf.Variable ๋ด ๋ณ์๋ค์ ๋ชจ๋ ๊ฐ์ด ์ ์ฅ ํฉ๋๋ค.
Save
(โป functional graph ์ ๋ง๋ค๋ ์๋๋ ์๊ด์์ต๋๋ค. ๋ํ Model์ instance ์ ๋ง๋ค์ง ์์๋ ๊ด์ฐฎ์ต๋๋ค.
๋ฐ๋ฉด์ tf.Module ์ ์์๋ฐ์ง ์์ผ๋ฉด tf.funtion ๋ด ํจ์๋ฅผ ํ๋ฒ ์คํ ํด์ผ ํฉ๋๋ค.
์ด๊ฒ ๊ฐ๋ฅํ ์ด์ ๋ tf.Module ์ ์์๋ฐ์ ํด๋์ค๋ Tensorflow ์์ ํ์ด์ฌ ์ฝ๋๋ฅผ ์คํํ์ง ์๊ณ Graph ๋ฐ ๋ณ์๋ฅผ ์ ์ฅ ํ๊ธฐ ๋๋ฌธ์
๋๋ค. )
Models and layers can be loaded from this representation without actually making an instance of the class that created it
batch_xs = [0.1, 0.2](/pai-plznw4me/tensorflow_basic/wiki/0.1,-0.2)
dense = Dense(2, 2, 'relu')
tf.saved_model.save(dense, 'model')
Load
new_model = tf.saved_model.load('model')
print(isinstance(new_model, Dense))
# >>> False
is an internal TensorFlow user object
(โป ๋ณต์ํ ๊ฐ์ฒด๋ ๊ธฐ์กด์ ์์ฑํ๋ Dense instance ๊ฐ ์๋๋ค.
์์ฑํ ๊ฐ์ฒด๋ internal TensorFlow user object
์ด๊ธฐ ๋๋ฌธ์ด๋ค.)
Keras
Save
class SimpleDNN(tf.keras.models.Model):
...
simple_dnn = SimpleDNN()
simple_dnn.save('best_model')
Load
tf.keras.models.load_model('best_model')