1. 程式人生 > >tensorflow教程:變數建立、初始化、儲存和載入

tensorflow教程:變數建立、初始化、儲存和載入

變數儲存到檔案

import tensorflow as tf
import numpy as np
# Create two variables.
x_data = np.float32([1,2,3,4,5,6,7,8,9,0])
weights = tf.Variable(tf.random_normal([10, 1], stddev=0.35), name="weights")
biases = tf.Variable(tf.zeros([1]), name="biases")
y = tf.matmul(x_data.reshape((1,-1)), weights)+biases
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer() saver = tf.train.Saver() # Later, when launching the model with tf.Session() as sess: # Run the init operation. sess.run(init_op) y_ = sess.run(y) print(y_) save_path = saver.save(sess, "./tmp/model.ckpt") print("Model saved in file:
", save_path)

從檔案載入變數

import tensorflow as tf
import numpy as np
# Create two variables.
x_data = np.float32([1,2,3,4,5,6,7,8,9,0])
weights = tf.Variable(tf.random_normal([10, 1], stddev=0.35), name="weights")
biases = tf.Variable(tf.zeros([1]), name="biases")
y = tf.matmul(x_data.reshape((1,-1)), weights)+biases
saver 
= tf.train.Saver() # Later, when launching the model with tf.Session() as sess: saver.restore(sess, './tmp/model.ckpt') y_ = sess.run(y) print(y_)

參考連結