python - tensorflow 2 : shape mismatch when serialize and decode it back

标签 python numpy tensorflow tensorflow2.0

我有一个形状为 (300,256,256) 的张量 A。 我想序列化 A 以保存为 tfrecord 格式。 但我无法将其转换回具有相同形状的张量。

A = tf.convert_to_tensor( *a numpy array with float32 type* )
B = tf.io.serialize_tensor(A)
C = tf.reshape(tf.io.decode_raw(B, out_type=tf.float32),[300,256,256])

如果我运行上面的代码,我会得到一个形状错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 19660806 values, but the requested shape has 19660800 [Op:Reshape]

似乎当我序列化或解码时,添加了 6 个 float 。 (很奇怪)

最佳答案

尝试使用:tf.io.parse_tensor(),而不是tf.io.decode_raw()

https://www.tensorflow.org/api_docs/python/tf/io/parse_tensor

关于python - tensorflow 2 : shape mismatch when serialize and decode it back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60183980/

相关文章:

Python Button 绑定(bind)两个函数并使用绑定(bind)发送参数

python - 如何在 Python 中通过字符串访问类变量?

python - 使用 GitPython,我该怎么做 git submodule update --init

python - 线性方程的 Numpy 语法

python - 与所有开关一起编程,运行良好,但 argparse '--help' 抛出很多错误

python - numpy 数组 : basic questions

python - Python3-ModuleNotFoundError : No module named 'numpy'

tensorflow - 如何将以下代码从 pytorch 更改为 tensorflow?

python-3.x - 为什么在 tensorflow 中出现这个错误?

c++ - 如何从 TensorFlow .pb 模型中获取权重格式?