由浅入深之Tensorflow(4)----Saver&restore

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

w = tf.Variable(tf.zeros([1, 1], dtype=tf.float32))
b = tf.Variable(tf.ones([1, 1], dtype=tf.float32))
y_hat = tf.add(b, tf.matmul(x, w))

...more setup for optimization and what not...

saver = tf.train.Saver()  # defaults to saving all variables - in this case w and b

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    if FLAGS.train:
        for i in xrange(FLAGS.training_steps):
            ...training loop...
            if (i + 1) % FLAGS.checkpoint_steps == 0:
                saver.save(sess, FLAGS.checkpoint_dir + ‘model.ckpt‘,
                           global_step=i+1)
    else:
        # Here‘s where you‘re restoring the variables w and b.
        # Note that the graph is exactly as it was when the variables were
        # saved in a prior training run.
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            ...no checkpoint found...

        # Now you can run the model to get predictions
        batch_x = ...load some data...
        predictions = sess.run(y_hat, feed_dict={x: batch_x})
时间: 2024-08-10 19:16:48

由浅入深之Tensorflow(4)----Saver&restore的相关文章

莫烦tensorflow(9)-Save&Restore

import tensorflow as tfimport numpy as np ##save to file#rember 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') # init = tf

深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)

1. tf.nn.embedding_lookup(W, X) W的维度为[len(vocabulary_list), 128], X的维度为[?, 8],组合后的维度为[?, 8, 128] 代码说明一下:即根据每一行X中的一个数,从W中取出对应行的128个数据,比如X[1, 3]个数据是3062,即从W中的第3062行取出128个数据 import numpy as np import tensorflow as tf data = np.array([[2, 1], [3, 4], [5,

由浅入深之Tensorflow(1)----linear_regression实现

Tensorflow是目前非常流行的deeplearning框架,学习Tensorflow最好的方法是github上的tf项目https://github.com/tensorflow/tensorflow 或者阅读极客学院主导翻译的中文教程http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html . 此处对tensorflow的基本语法不予赘述,直接贴上源码: import numpy as np i

由浅入深之Tensorflow(2)----logic_regression实现

import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data def initWeights(shape): return tf.Variable(tf.random_normal(shape, stddev = 0.1)) def initBiases(shape): return tf.Variable(tf.random_normal(shape,

『TensorFlow』徒手装高达_战斗数据收集模块原型_save&restore

顺便一提,上节定义的网络结构有问题,现已修改,之后会陆续整理上来.两种常用(我会的)的加载方式:1. ''' 使用原网络保存的模型加载到自己重新定义的图上 可以使用python变量名加载模型,也可以使用节点名 ''' import AlexNet as Net import AlexNet_train as train import random import tensorflow as tf IMAGE_PATH = './flower_photos/daisy/5673728_71b8cb5

Tensorflow restore 恢复/载入模型.

Tensorflow 恢复/载入模型.可以有两种方法: 1.首先利用tensorflow建立和以前一样的图.再利用restore恢复参数. .....定义图...... sess=tf.Session() saver=tf.train.Saver() sess.run(tf.global_variables_initializer()) saver.restore(sess, weigths_path) 2.或者直接加载以前的图,将saver修改为: sess=tf.Session() save

tensorflow 1.0 学习:模型的保存与恢复(Saver)

将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf.train.Saver() 在创建这个Saver对象的时候,有一个参数我们经常会用到,就是 max_to_keep 参数,这个是用来设置保存模型的个数,默认为5,即 max_to_keep=5,保存最近的5个模型.如果你想每训练一代(epoch)就想保存一次模型,则可以将 max_to_keep设置

TensorFlow Saver的使用方法

我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用作测试.Tensorflow针对这一需求提供了Saver类. Saver类提供了向checkpoints文件保存和从checkpoints文件中恢复变量的相关方法.Checkpoints文件是一个二进制文件,它把变量名映射到对应的tensor值 . 只要提供一个计数器,当计数器触发时,Saver类可以自动的生成checkpoint文件.这让我们可以在训练过程中保存多个中间结果.例如,我们可以保存每一

tensorflow的save和restore

使用tensorflow中的save和restore可以对模型进行保存和恢复 import tensorflow as tf v1 = tf.Variable(tf.random_normal([1,2]), name="v1") v2 = tf.Variable(tf.random_normal([2,3]), name="v2") init_op = tf.global_variables_initializer() saver = tf.train.Saver