ذخیره و بازیابی مدل تنسورفلو در پایتون - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

ذخیره و بازیابی مدل تنسورفلو در پایتون

0 امتیاز
وقتی مدلی را آموزش میدم چطور ذخیره کنمش و بعداً چطور بارگذاری کنم؟
سوال شده فروردین 12, 1396  بوسیله ی farshid (امتیاز 137)   13 27 34

2 پاسخ

+1 امتیاز
 
بهترین پاسخ

ذخیره سازی مدل :

import tensorflow as tf

#Prepare to feed input, i.e. feed_dict and placeholders
w1 = tf.placeholder("float", name="w1")
w2 = tf.placeholder("float", name="w2")
b1= tf.Variable(2.0,name="bias")
feed_dict ={w1:4,w2:8}

#Define a test operation that we will restore
w3 = tf.add(w1,w2)
w4 = tf.multiply(w3,b1,name="op_to_restore")
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#Create a saver object which will save all the variables
saver = tf.train.Saver()

#Run the operation by feeding input
print sess.run(w4,feed_dict)
#Prints 24 which is sum of (w1+w2)*b1 

#Now, save the graph
saver.save(sess, 'my_test_model',global_step=1000)

بازیابی مدل :

import tensorflow as tf

sess=tf.Session()    
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('my_test_model-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))


# Access saved Variables directly
print(sess.run('bias:0'))
# This will print 2, which is the value of bias that we saved


# Now, let's access and create placeholders variables and
# create feed-dict to feed new data

graph = tf.get_default_graph()
w1 = graph.get_tensor_by_name("w1:0")
w2 = graph.get_tensor_by_name("w2:0")
feed_dict ={w1:13.0,w2:17.0}

#Now, access the op that you want to run. 
op_to_restore = graph.get_tensor_by_name("op_to_restore:0")

print sess.run(op_to_restore,feed_dict)
#This will print 60 which is calculated 

ما می تونیم بعد از هر چند تا min-batch شبکه را در فایلی تحت عنوان checkpoint ذخیره کنیم.

پاسخ داده شده تیر 1, 1396 بوسیله ی farnoosh (امتیاز 8,362)   20 44 59
ویرایش شده آذر 26, 1396 بوسیله ی farnoosh
+1 امتیاز

خیلی سادس 

و زمانی هم که خواستین بازیابی کنید به این شکله :

پاسخ داده شده خرداد 25, 1396 بوسیله ی mlghost (امتیاز 75)   3
...