#Save to fileImportTensorFlow as TFImportNumPy as NP## (1) Save to file stores related variables in Files#Remember to define the same dtype and shape when restoreW = tf. Variable ([[1,2,3],[3,4,5]],dtype=tf.float32,name='Weights') b= TF. Variable ([[1,2,3]],dtype=tf.float32,name='biases') Init=tf.initialize_all_variables () Saver=Tf.train.Saver () with TF. Session () as Sess:sess.run (init) Save_path= Saver.save (Sess,"my_net/save_net.ckpt") Print("Save to Path:", Save_path)## (2) Restore variables remove the relevant variable from the file#redefine the same shape and same type for you variablesW = tf. Variable (Np.arange (6). Reshape ((2,3)), dtype=tf.float32,name="Weights")#reshape ((2,3): 2 rows 3 Columnsb = tf. Variable (Np.arange (3). Reshape ((1,3)), dtype=tf.float32,name="biases")#Not need init stepSaver =Tf.train.Saver () with TF. Session () as Sess:saver.restore (Sess,"my_net/save_net.ckpt") Print("Weights:", Sess.run (W))Print("Biases:", Sess.run (b))
TensorFlow Learning (11) Writing Python code to a file