Tensorflow saver(save weight)

本文详细介绍使用TensorFlow进行变量的存储与读取操作。通过具体示例代码演示如何利用saver对象保存与恢复变量状态,并强调了创建指定文件夹的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

saver用于变量的读取操作,需要注意的是,在变量存储时,需要事先建立好一个文件夹。存储代码并不会自动新建文件夹,需要你人为手动建立,不然会报错。

主要保存代码为,建立一个saver,保存sess

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)

主要存储代码为:用变量名字识别应该哪个变量接受哪个值

W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name="weights")
b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name="biases")

# not need init step

saver = 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))

完整代码如下:

变量存储代码

# View more python tutorials on my Youtube and Youku channel!!!

# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial

"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
from __future__ import print_function
import tensorflow as tf
import numpy as np

# Save to file
# remember to define the same dtype and shape when restore
W = tf.Variable([[1,2,3],[3,4,5]], dtype=tf.float32, name='weights')
b = tf.Variable([[1,2,3]], dtype=tf.float32, name='biases')

# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()

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)

读取代码

# View more python tutorials on my Youtube and Youku channel!!!

# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial

"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
from __future__ import print_function
import tensorflow as tf
import numpy as np

################################################
# restore variables
# redefine the same shape and same type for your variables
W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name="weights")
b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name="biases")

# not need init step

saver = 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的面试题目及其解答 对于希望在人工智能领域找到工作的人员来说,掌握TensorFlow的相关知识至关重要。完成深入学习课程之后,能够回答基本的面试问题是求职者的一项重要技能[^2]。 #### 基础概念类问题 1. **什么是TensorFlow?** TensorFlow是一个开源软件库,专为数值计算而设计,特别适用于机器学习应用中的大规模数据集处理。它允许开发者构建并训练各种类型的神经网络和其他复杂的模型结构[^1]。 2. **解释一下张量(Tensor)的概念。** 在TensorFlow中,“张量”是指多维数组或矩阵形式的数据表示法。这些可以是一维向量、二维表格或者更高维度的形式来存储不同类型的信息。 #### 实践操作类问题 3. **如何定义一个简单的线性回归模型?** ```python import tensorflow as tf # 定义输入特征X和标签Y X = tf.constant([[1], [2], [3]], dtype=tf.float32) Y = tf.constant([2, 4, 6], dtype=tf.float32) # 创建变量W和b用于拟合直线方程y=wx+b W = tf.Variable(0., name='weight') b = tf.Variable(0., name='bias') def linear_regression(X): return W * X + b ``` 4. **怎样保存已经训练好的模型以便后续加载使用?** ```python saver = tf.train.Saver() save_path = saver.save(sess, "/tmp/model.ckpt") print("Model saved in path: %s" % save_path) with tf.Session() as sess: saver.restore(sess, "/tmp/model.ckpt") print("Model restored.") ``` #### 高级特性理解 5. **描述Eager Execution机制的作用是什么?** Eager execution是一种命令式的编程环境,在这种模式下,运算会立即执行并且返回具体的结果而不是构建图谱再运行会话。这使得调试更加直观简单,并且支持更灵活动态的操作流程控制逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值