saver.restore时ValueError:Novariablestosave报错
(2019-05-28 23:44:54)| 标签: restorevalueerrorvariablessave | 分类: 错误处理 | 
			一、背景
   
在tensorflow中,使用saver.save()保存了模型,然后通过saver.restore读取模型使用,结果报错了。程序的相关代码如下:
    saver.restore(sess,
"model/rnnwordtest.cpkt")  
    for i in
range(32):  
     
  keys = np.reshape(np.array(inputword), [-1,
n_input, 1])    
     
  onehot_pred = sess.run(pred, feed_dict={x:
keys})    
     
  onehot_pred_index = int(tf.argmax(onehot_pred,
1).eval_r())    
     
  sentence = "%s%s" % (sentence,
words[onehot_pred_index])    
     
  inputword = inputword[1:]    
     
  inputword.append(onehot_pred_index)    
    print(sentence)  
  
 self.build() 
 self._build(self._filename, build_save=True,
build_restore=True) 
 raise ValueError("No variables to
save") 
    saver =
tf.train.Saver()  
    saver.restore(sess,
"model/rnnwordtest.cpkt")  
							
		
						
		
		
		
		
		
		
							
		
				
		
				
	...
saver = tf.train.Saver()
...
with tf.Session() as sess:
详细的报错信息如下:
Traceback (most recent call last):
saver = tf.train.Saver()
ValueError: No variables to save
二、解决方案
将saver = tf.train.Saver()移到with tf.Session() as
sess下方,即:
with tf.Session() as sess:

 加载中…
加载中…