这个错误是tensorflow在读取数据进行推理时经常出现的一个错误,这里以mnist数据为例,mnist训练集数据维度为(55000,784),如果读取单个数据的话只有一个数据[0,0,0,1,…,0]其维度为1x784。
但是在推理时,feed_dict需要采用的是[[0,0,0,1,…,0]],这样的话才知道有多少个数据被送进来,所以需要进行扩维度。

batch_xs=mnist.train.images[1]
#一定要扩充维度,否则就会报错
train=np.expand_dims(batch_xs, 0)

这样操作后就不会报错了~~~

完整代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.tools.inspect_checkpoint import  print_tensors_in_checkpoint_filemnist=input_data.read_data_sets("MNIST_data/",one_hot=True)
batch_xs=mnist.train.images[1]
#一定要扩充维度,否则就会报错
train=np.expand_dims(batch_xs, 0)print(train.shape)
saver = tf.train.import_meta_graph("./save/mnist_model.ckpt.meta")
gragh = tf.get_default_graph()# 获取当前图,为了后续训练时恢复变量
# tensor_name_list = [tensor.name for tensor in gragh.as_graph_def().node]# 得到当前图中所有变量的名称
# print(tensor_name_list)with tf.Session() as sess:print_tensors_in_checkpoint_file("./save/mnist_model.ckpt",None,True)saver.restore(sess,"./save/mnist_model.ckpt")X= gragh.get_tensor_by_name('Placeholder:0')z=gragh.get_tensor_by_name('output:0')result=tf.arg_max(z,1)print("result:",sess.run(result,feed_dict={X:train}))

最终输出:

识别出来的数字为: [3]
真实数字为: [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]

ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)'相关推荐

  1. 成功解决ValueError: Cannot feed value of shape (1, 10, 4) for Tensor Placeholder:0 , which has shape

    成功解决ValueError: Cannot feed value of shape (1, 10, 4) for Tensor 'Placeholder:0', which has shape '( ...

  2. TF目标检测API-Error: Argument must be a dense tensor: range(0, 3) - got shape [3], but wanted []....

    转自:https://blog.csdn.net/lovebyz/article/details/80668881 出错原因:知乎的大佬说是python3的兼容问题 解决办法:把research/ob ...

  3. 成功解决ValueError: Cannot feed value of shape (80, 15, 1, 1) for Tensor 'Placeholder_1:0', which has sh

    成功解决ValueError: Cannot feed value of shape (80, 15, 1, 1) for Tensor 'Placeholder_1:0', which has sh ...

  4. ValueError: Shape mismatch: The shape of labels (received (768,)) should equal the shape of logits e

    问题 运行tensorflow 的resnet网络,预测结果,出现报错 ValueError: Shape mismatch: The shape of labels (received (768,) ...

  5. ValueError: You are trying to load a weight file containing 0 layers into a model with 16 layers.

    ValueError: You are trying to load a weight file containing 0 layers into a model with 16 layers. 在使 ...

  6. Cannot interpret feed_dict key as Tensor: Tensor Tensor(“keras_learning_phase:0“, shape=(),

    报错信息:Cannot interpret feed_dict key as Tensor: Tensor Tensor("keras_learning_phase:0", sha ...

  7. ValueError: List argument ‘indices‘ to ‘Sparse Concat‘ Op with length 0 shorter than minimum length2

    错误ValueError: List argument 'indices' to 'Sparse Concat' Op with length 0 shorter than minimum lengt ...

  8. 成功解决ValueError: Invalid classes inferred from unique values of `y`. Expected: [0 1], got [‘0‘ ‘1‘]

    成功解决ValueError: Invalid classes inferred from unique values of `y`.  Expected: [0 1], got ['0' '1'] ...

  9. RuntimeError: output with shape [1, 28, 28] doesnt match the broadcast shape [3, 28, 28]

    pytorch执行MNIST源码 # Import things like usual%matplotlib inline %config InlineBackend.figure_format = ...

  10. Allegro 将shape转换为line,将line转换成shape

    1.将Shape转换成Line allegro导入的dxf有时是shape,所以需要将shape转换为line,将line放在Board Geometry->Outline层. allegro规 ...

最新文章

  1. 多分类任务的混淆矩阵
  2. 中国人工智能学会通讯——人工智能如何造福人类 1.1 人工智能是中性技术
  3. 【BUG记录】> Android dependency ‘androidx.vectordrawable:vectordrawable‘ has different
  4. 终于有人把计算机视觉讲明白了。。。
  5. 命令行出错Exception in thread main java.lang.UnsupportedClassVersionError:
  6. css圆角box(宽度自适应)(百度知道挖出)
  7. C语言手写快排算法,两个值时也可以使用哦!
  8. 【转】ASP.NET使用ECharts展示后台数据
  9. 【洛谷P1972】HH的项链(求区间内有多少个不同的数---树状数组)
  10. 信号与系统学习之第一章(系统的六大基本性质定义与判别:无记忆性、可逆性、因果性、稳定性、时不变性、线性)
  11. 【hexo】fluid中文乱码问题解决
  12. steam无法连接至计算机,无法连接至steam网络怎么办 无法连接至steam网络解决方法【图文】...
  13. RuntimeError: CUDA error (10): invalid device ordinal
  14. 三菱q系列plc 和电脑socket_三菱Q系列和WinCC以太网通讯的组态步骤
  15. Exception in thread “main“ redis.clients.jedis.exceptions.JedisConnectionException: Failed to create
  16. 《因子投资 - 方法与实践》新书上市
  17. pstate0 vid数值意义_光行差成因和物理意义新解及其验证方法
  18. Matlab 自编雅可比矩阵 (jacobi) 函数与官方的Jacobian matrix(雅可比矩阵)函数对比及创新
  19. BZOJ3875-[Ahoi2014Jsoi2014]骑士游戏
  20. python.plt 画三维图像

热门文章

  1. Allegro导出STP文件
  2. Chrome 开发者工具 官方文档
  3. “吃神么,买神么”的第一个Sprint计划(第三天)
  4. 批量创建用户(基于域)
  5. 【SQL Server】 SQL Server Management Studio不允许保存更改,组织保存要求解决方案
  6. CSS 布局 - 水平 amp;amp; 垂直对齐,全面的水平垂直居中方案
  7. 正则表达式过滤多个词语并替换为相同长度的星号
  8. Process Explorer 进程管理利器常用功能(中)
  9. Redis 禁止使用耗时命令和时间复杂度为O(n)的命令
  10. 面试中爱问的大数量的问题总结