TensorFlow的模型保存与加载
import os os.environ[‘TF_CPP_MIN_LOG_LEVEL‘] = ‘2‘ import tensorflow as tf #tensorboard --logdir="./" def linearregression(): with tf.variable_scope("original_data"): X = tf.random_normal([100,1],mean=0.0,stddev=1.0) y_true = tf.matmul(X,[[0.8]]) + [[0.7]] with tf.variable_scope("linear_model"): weights = tf.Variable(initial_value=tf.random_normal([1,1])) bias = tf.Variable(initial_value=tf.random_normal([1,1])) y_predict = tf.matmul(X,weights)+bias with tf.variable_scope("loss"): loss = tf.reduce_mean(tf.square(y_predict-y_true)) with tf.variable_scope("optimizer"): optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss) #收集观察张量 tf.summary.scalar("losses",loss) tf.summary.histogram("weight",weights) tf.summary.histogram("biases",bias) #合并收集的张量 merge = tf.summary.merge_all() init = tf.global_variables_initializer() saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) print(weights.eval(),bias.eval()) # 模型加载 saver.restore(sess,"./model/linearregression") print(weights.eval(),bias.eval()) # filewriter = tf.summary.FileWriter("./tmp",graph=sess.graph) # for i in range(1000): # sess.run(optimizer) # print("loss:", sess.run(loss)) # print("weight:", sess.run(weights)) # print("bias:", sess.run(bias)) # summary = sess.run(merge) # filewriter.add_summary(summary,i) # # #checkpoint文件,模型保存 # saver.save(sess,"./model/linearregression") if __name__ == ‘__main__‘: linearregression()
相关推荐
Micusd 2020-11-19
xjtukuixing 2020-10-27
lybbb 2020-10-15
lybbb 2020-09-29
ghjk0 2020-09-24
yamaxifeng 2020-09-09
GDGYZL 2020-08-28
lybbb 2020-08-28
Icevivian 2020-08-25
comwayLi 2020-08-16
carbon0 2020-08-16
源式羽语 2020-08-09
sherry颖 2020-08-01
songbinxu 2020-07-19
sherry颖 2020-07-18
Niteowl 2020-07-15
Kindle君 2020-07-15
源式羽语 2020-07-04
源式羽语 2020-06-28