码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow教程:变量创建、初始化、保存和加载

时间:2017-04-26 18:11:47      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:top   orm   ble   mode   init   store   atm   ros   name   

变量保存到文件

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_)

参考链接

http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variables.html

tensorflow教程:变量创建、初始化、保存和加载

标签:top   orm   ble   mode   init   store   atm   ros   name   

原文地址:http://www.cnblogs.com/linyuanzhou/p/6769870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!