简体   繁体   English

Tensorflow 2:序列化和解码时形状不匹配

[英]Tensorflow 2: shape mismatch when serialize and decode it back

I have a tensor A of shape (300,256,256).我有一个形状为 (300,256,256) 的张量 A。 I want to serialize A to save as tfrecord format.我想序列化 A 以保存为 tfrecord 格式。 But I cannot convert it back to tensor with same shape.但我无法将其转换回具有相同形状的张量。

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])

If I run the code above, I got a shape error:如果我运行上面的代码,我会得到一个形状错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 19660806 values, but the requested shape has 19660800 [Op:Reshape] tensorflow.python.framework.errors_impl.InvalidArgumentError:reshape 的输入是一个具有 19660806 值的张量,但请求的形状有 19660800 [Op:Reshape]

It seems that when I serialize or when I decode, 6 floats are added.似乎当我序列化或解码时,添加了 6 个浮点数。 (very weird) (很奇怪)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM