1. 程式人生 > >解決tensorflow中報錯NotFoundError (see above for traceback): Key v1_1 not found in checkpoint問題

解決tensorflow中報錯NotFoundError (see above for traceback): Key v1_1 not found in checkpoint問題

  • 針對TensorFlow實戰框架Chp5,P113面問題
  • 在 saver.restore()載入的前面,需要新增 tf.reset_default_graph(),作用是 清除預設圖的堆疊,並設定全域性圖為預設圖

  • 儲存檔案

# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 16:17:26 2018

@author: muli
"""

import tensorflow as tf

v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0
,shape=[1]),name="v2") result = v1 + v2 # 儲存訓練好的資料 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.save(sess, "Saved_model/model.ckpt") print("已儲存完畢!")
  • 載入檔案
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 16:23:46 2018

@author: muli
"""
import tensorflow as tf # 清除預設圖的堆疊,並設定全域性圖為預設圖 tf.reset_default_graph() v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1") v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2") result = v1 + v2 # 儲存訓練好的資料 saver = tf.train.Saver() with tf.Session() as sess: print("正在載入...") saver.restore(sess, "./Saved_model/model.ckpt"
) print(sess.run(result))
  • 直接載入已經持久化的圖
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 16:29:50 2018

@author: muli
"""

import tensorflow as tf

# 清除預設圖的堆疊,並設定全域性圖為預設圖 
tf.reset_default_graph()

saver = tf.train.import_meta_graph("./Saved_model/model.ckpt.meta")

with tf.Session() as sess:
    saver.restore(sess, "./Saved_model/model.ckpt")
    print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0")))