tensorflow保存训练模型的函数
时间: 2024-05-16 08:17:27 浏览: 17
TensorFlow提供了两种保存训练模型的方式:
1. 使用Saver类来保存模型的参数变量,这种方式保存的是计算图的结构和参数变量的取值,可以在后续的程序中加载模型并继续训练或者使用模型进行预测。
```python
import tensorflow as tf
# 定义计算图
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y = tf.placeholder(tf.float32, shape=[None, 10], name='y')
W = tf.Variable(tf.zeros([784, 10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
y_ = tf.nn.softmax(tf.matmul(x, W) + b, name='y_')
cross_entropy = -tf.reduce_sum(y * tf.log(y_), name='cross_entropy')
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 创建Saver对象
saver = tf.train.Saver()
# 训练模型
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
# 保存模型
saver.save(sess, 'model/model.ckpt')
```
2. 使用SavedModelBuilder类来保存模型的计算图和变量,这种方式保存的是计算图的结构、变量的取值以及计算图中的元数据,可以在后续的程序中加载模型并直接使用。
```python
import tensorflow as tf
# 定义计算图
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y = tf.placeholder(tf.float32, shape=[None, 10], name='y')
W = tf.Variable(tf.zeros([784, 10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
y_ = tf.nn.softmax(tf.matmul(x, W) + b, name='y_')
cross_entropy = -tf.reduce_sum(y * tf.log(y_), name='cross_entropy')
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 创建SavedModelBuilder对象
builder = tf.saved_model.builder.SavedModelBuilder('model')
# 训练模型
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
# 构建模型
tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
tensor_info_y_ = tf.saved_model.utils.build_tensor_info(y_)
prediction_signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs={'images': tensor_info_x},
outputs={'scores': tensor_info_y_},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature
},
main_op=tf.tables_initializer(),
strip_default_attrs=True)
# 保存模型
builder.save()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)