解决tensorflow 的 Saver.restore()无法从本地读取变量的问题

最近做tensorflow 手写数字识别的时候遇到了一个问题,Saver的restore()方法无法从本地恢复变量,导致了每次都会重新训练。

原来代码

saver = tf.train.Saver(max_to_keep=5)
epoch = tf.Variable(0, name='epoch', trainable=False) sess = tf.Session()
sess.run(tf.global_variables_initializer()) ckpt_dir = "./model/" if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir) ckpt = tf.train.latest_checkpoint(ckpt_dir) if ckpt != None:
saver.restore(sess, ckpt)
else:
print('Train from scratch') start = sess.run(epoch)

修改代码

epoch = tf.Variable(0, name='epoch', trainable=False)
saver = tf.train.Saver(max_to_keep=5) sess = tf.Session()
sess.run(tf.global_variables_initializer()) ckpt_dir = "./model/" if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir) ckpt = tf.train.latest_checkpoint(ckpt_dir) if ckpt != None:
saver.restore(sess, ckpt)
else:
print('Train from scratch') start = sess.run(epoch)

其实主要改变的就是以下两行的顺序

epoch = tf.Variable(0, name='epoch', trainable=False)

saver = tf.train.Saver(max_to_keep=5)

解决tensorflow Saver.restore()无效的问题的更多相关文章

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

    x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) w = tf.Variable(tf.zeros([1, 1], dtype ...

  2. 深度学习原理与框架-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] 代码说 ...

  3. 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 1.cv2.resize(图片压缩) 2..get_shape()[1:4].num_elements(获得最后三维度之和) 3.saver.save(训练参数的保存) 4.tf.train.import_meta_graph(加载模型结构) 5.saver.restore(训练参数载入)

    1.cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR) 参数说明:image表示输入图片,image_size表示变 ...

  4. TensorFlow Saver的使用方法

    我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用作测试.Tensorflow针对这一需求提供了Saver类. Saver类提供了向checkpoint ...

  5. TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model

      TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model Checkmate is designed to be a simple drop-i ...

  6. paip.解决access出现 -2147467259 无效的参数量

    paip.解决access出现 -2147467259   无效的参数量 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http: ...

  7. saver.restore()遇到的错误

    运行python程序执行  saver.restore(sess,"E:/pythonFile/untitled/deepLearning/model/model.ckpt")   ...

  8. 解决tensorflow模型保存时Saver报错:TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray

    TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray 对于下面的实际代码: import ...

  9. tensorflow saver简介+Demo with linear-model

    tf.train.Saver提供Save和Restore Tensorflow变量的功能,常用于保存.还原模型训练结果,这在自己的训练和迁移学习中都很有用. 训练.保存脚本: import tenso ...

随机推荐

  1. PVE上安装黑裙辉6.2

    参考文章:https://post.smzdm.com/p/a25r8mo2/ http://www.myxzy.com/post-488.html 环境介绍 1.Proxmox VE(以下简称PVE ...

  2. C++-随机数的产生

    一.随机数 以前学C语言的时候感觉随机数没啥用的,现在想想是自己无知啦,在帮人做一个项目的时候发现随机数还是相当有用的,我们可以利用随机数来生成大量的测试数据. 有两种方法可以让你的程序每次运行结果不 ...

  3. mysql 查询——逻辑查询

    --去重查询 distinct select distinct gander from student; --逻辑查询 and or not --查询18-28之间的数据 select *from s ...

  4. js -- 高阶函数的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 普及C组第四题(8.18)

    1574. [提高]X-因子链 (File IO): input:factor.in output:factor.out 时间限制: 1000 ms  空间限制: 131072 KB 题目描述 给一个 ...

  6. 洛谷P1433 吃奶酪

    #include<iostream> #include<math.h> using namespace std ; ; int n; bool st[N]; double x[ ...

  7. 洛谷P1327 数列排序

    https://www.luogu.org/problem/P1327 #include<bits/stdc++.h> #define Ll long long using namespa ...

  8. AcWing 791. 高精度加法

    https://www.acwing.com/problem/content/793/ #include<bits/stdc++.h> using namespace std; vecto ...

  9. 【C语言】用指针作为形参完成数据的升序排列

    #include<stdio.h> void sort(int *x,int n); int main() { ] = { ,,,,,,,,, },i; sort(arr, ); prin ...

  10. 【vue】 router.beforeEach

    import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...