python - 如何修复:“预期的形状 TensorShape([Dimension(None), Dimension(785)]) 获得了形状为 TensorShape([Dimension(785)]) 的数据集”
问题描述
所以我正在尝试使用 tensorflow 构建 Fashion-MNIST CNN 分类器。我得到了这个维度错误。我是 tensorflow 的新手。
我阅读了 tensorflow 文档,但我没有解决我的问题。我正在分享我发现与错误相关的代码。以下是整个回溯
Traceback (most recent call last):
File "<ipython-input-2-8a4292875dbe>", line 208, in <module>
model.build()
File "<ipython-input-2-8a4292875dbe>", line 133, in build
self.getdata()
File "<ipython-input-2-8a4292875dbe>", line 65, in getdata
self.test_data_init = iterator.make_initializer(test_data)
# initializer for test_data
File "/home/aditya/anaconda3/envs/train/lib/python3.7/site-
packages/tensorflow/python/data/ops/iterator_ops.py", line 369,
in make_initializer
(self.output_shapes, dataset_output_shapes))
TypeError: Expected output shapes compatible with
TensorShape([Dimension(None), Dimension(785)]) but got dataset
with output shapes TensorShape([Dimension(785)]).
我的模型.build()
def build(self):
'''
Build the computation graph
'''
self.getdata()
self.inference()
self.loss()
self.optimize()
self.eval()
self.summary()
我的获取数据()
def getdata(self):
with tf.name_scope('data'):
train_data=tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(10000) # if you want to shuffle your data
train_data = train_data.batch(self.batch_size)
test_data=tf.data.Dataset.from_tensor_slices(test)
iterator=tf.data.Iterator.from_structure(train_data.output_types,train_data.output_shapes)
self.train_data_init = iterator.make_initializer(train_data) # initializer for train_data
self.test_data_init = iterator.make_initializer(test_data) # initializer for test_data
img, self.label = iterator.get_next()
self.img = tf.reshape(img, shape=[-1, 28, 28, 1])
# reshape the image to make it work with tf.nn.conv2d
我期待找到一种重塑的方法,但我做不到。任何帮助将不胜感激
解决方案
推荐阅读
- graphql - 从@nestjs/graphql 实体迁移到 schema.prisma?
- python - 它不会绘制我的矩形(prdroid 上的 pygame)
- java - 如何获取检票口图像的 url 以在开放图形元标记中使用它
- python - 如何将 0-1 图像浮点数组转换为 0-255 整数数组
- reactjs - 如何使用 Redux-RTK 设置 React-Testing-Library(在 typescript 中)
- javascript - 从对象传递函数作为来自同一对象的另一个函数中的回调时获取未定义
- apache - php脚本未在带有php7的ubuntu 20.04上执行
- python - Python上的摊销时间表
- pyarrow - 使用pyarrow编写箭头数据集时如何解决“打开文件过多错误”?
- ios - 我的导航栏在 Xcode 13.0 中没有转到屏幕顶部