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')

Reference

official guide