tensorflow - 向 mnist 图像数据集添加一行
问题描述
我必须向 mnist 图像数据集添加一行,该数据集被批处理为 32 个样本。这里的代码:
(mnist_images, mnist_labels), _ = tf.keras.datasets.mnist.load_data()
dataset = tf.data.Dataset.from_tensor_slices(
(tf.cast(mnist_images[...,tf.newaxis]/255, tf.float32),
tf.cast(mnist_labels,tf.int64)))
dataset = dataset.shuffle(1000).batch(32)
for images,labels in dataset.take(1):
print("Logits: ", mnist_model(images[0:1]).numpy())
b =tf.reshape(images, [784,32], tf.float32)
c = tf.concat(b,tf.ones([1,32], tf.float32),0)
我收到以下错误,但都是 dtype float 32,
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor:
shape=(1, 32), dtype=float32, numpy= array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]],
dtype=float32)>
还有另一种方法可以在图像张量中添加一行吗?
解决方案
好像您刚刚忘记使用括号 - [ ] 。
利用:
c = tf.concat([b,tf.ones([1,32], tf.float32)],0)
代替 :
c = tf.concat(b,tf.ones([1,32], tf.float32),0)
推荐阅读
- django - Django:实例化模型时如何加载相关的模型对象
- c# - 如何从 c# 代码运行 cplex 程序?
- r - 根据间隔条件合并数据帧
- php - 致命错误:第 1125 行 C:\xampp\htdocs\farmacia\vendor\phpmailer\phpmailer\src\SMTP.php 中的最大执行时间超过 30 秒
- rust - 为什么我需要在循环的每次迭代中声明一个变量才能使用它?
- c# - Asp.Net Core:更新行导致插入
- java - 找出java应用程序中的java代理端口
- css - 悬停在伪元素上时触发CSS动画?
- python - 总是使用 numpy 数组而不是 python 列表有什么缺点?
- python - 如何确保一个工作人员在 CPU 上运行?