本帖最後由 evan11 於 2020-10-2 21:29 編輯
# Program1
import tensorflow as tf
import os
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, '/tmp/model/my_test_model')
print(sess.run([w1,w2]))
files_path = [os.path.abspath(x) for x in os.listdir()]
print(files_path)
arr = os.listdir("/tmp/model")
print(arr)
Run :
[array([-0.11419322, 0.1697844 ], dtype=float32), array([1.168101 , 0.6013027, 0.6706504, 1.0699968, 0.7349461],
dtype=float32)]
['/content/.config', '/content/sample_data']
['my_test_model.index', 'my_test_model.meta', 'checkpoint', 'my_test_model.data-00000-of-00001', 'my_test_model-1000.data-00000-of-00001', 'my_test_model-1000.index']
#Program2
import tensorflow as tf
with tf.Session() as sess:
saver = tf.train.import_meta_graph('/tmp/model/my_test_model.meta')
saver.restore(sess,tf.train.latest_checkpoint('/tmp/model/'))
print(sess.run('w1:0'))
#Model has been restored. Above statement will print the saved value
Run
INFO:tensorflow:Restoring parameters from /tmp/model/my_test_model
[-0.11419322 0.1697844 ]
請問巴打個0有咩用? 謝謝各位巴打
我用tensorflow 1.4.0的. |