Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow模型保存和恢复问题_Python_Tensorflow - Fatal编程技术网

Python tensorflow模型保存和恢复问题

Python tensorflow模型保存和恢复问题,python,tensorflow,Python,Tensorflow,谢谢你的帮助。 这个问题困扰了我两天。我搜索了很多网站,没有解决 背景: 我正在学习mnist,它一开始运行正常,但当我保存模型并还原时,出现了一个错误,告诉我必须输入占位符_1。我很困惑 代码: 下面的代码是正确的 import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = input_data.read_data_sets("mnist data/", one_hot=True) import te

谢谢你的帮助。 这个问题困扰了我两天。我搜索了很多网站,没有解决

背景: 我正在学习
mnist
,它一开始运行正常,但当我保存模型并还原时,出现了一个错误,告诉我必须输入
占位符_1
。我很困惑

代码: 下面的代码是正确的

import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("mnist data/", one_hot=True)

import tensorflow as tf
# 操作符号变量来描述这些可交互的操作单元
x = tf.placeholder(tf.float32, [None, 784])


# 权重值和偏置量
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
# 实现模型
y = tf.nn.softmax(tf.matmul(x,W) + b)
# 添加一个新的占位符用于输入正确值
y_ = tf.placeholder("float", [None,10])
# 计算交叉熵:
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
# 要求TensorFlow用梯度下降算法(gradient descent algorithm)以0.01的学习速率最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 添加一个操作来初始化我们创建的变量
init = tf.initialize_all_variables()

saver = tf.train.Saver()
sess = tf.Session()
sess.run(init)
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})

save_path = saver.save(sess, "./model_mnist.ckpt",write_meta_graph=False) 
print("Model saved in life:", save_path) 

import cv2
import numpy as np

img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) 
arr = []
for i in range(28):
    for j in range(28):
        gray = 1 - img[i,j]/255
        arr.append(gray)

arr_mnist = np.array([arr])
#print(arr_mnist)

result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
#print(np.argmax(result[0]))
#print(np.sum(result[0]))
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))
#print(tf.argmax(result,1))
但是,当我想使用模型进行恢复时。出了问题

import cv2
import numpy as np
import tensorflow as tf

img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) 

arr = []

for i in range(28):
    for j in range(28):
        gray = 1 - img[i,j]/255
        arr.append(gray)

arr_mnist = np.array([arr])
#print(arr_mnist)

tf.reset_default_graph()

x = tf.placeholder("float", shape=[None, 784])
y = tf.placeholder("float", shape=[None, 10])
#keep_prob = tf.placeholder("float")

sess = tf.Session()

saver = tf.train.import_meta_graph('./model_mnist.ckpt.meta')  

saver.restore(sess, './model_mnist.ckpt')

result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))
错误是:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
因此,我想问题应该在模型保存或恢复过程中,但我无法解决。
如何更正代码?谢谢大家!

恢复图形时,您声明了两个占位符,并且在运行会话时仅提供一个占位符。
y
占位符是与错误名称匹配的占位符

但是您不需要在运行脚本中声明这些占位符。修复会处理好它。请注意,您可以使用字符串键指定提要字典:

feed_dict: { 'x': [1,2,3] }

您的恢复不正确。我也回答了类似的问题:非常感谢。是的,问题是我的变量resotre方法。谢谢非常感谢你。问题是我的变量resotre方法。谢谢