kwotsin
kwotsin

Reputation: 2923

TensorFlow: Why is there a need to reshape non-sparse elements once when parsing a TF-example from TFRecord files?

In the TensorFlow documentation at GitHub, there is this following code:

# Reshape non-sparse elements just once:
for k in self._keys_to_features:
  v = self._keys_to_features[k]
  if isinstance(v, parsing_ops.FixedLenFeature):
    example[k] = array_ops.reshape(example[k], v.shape)

I am wondering why there is a need to reshape a FixedLenFeature tensor after parsing it from a TFRecord file.

In fact, what is the difference between a FixedLenFeature and VarLenFeature and what is their relevance to a Tensor? I am loading images in this case, so why would all of them be classified as a FixedLenFeature? What is an example of a VarLenFeature?

Upvotes: 2

Views: 561

Answers (1)

Allen Lavoie
Allen Lavoie

Reputation: 5808

Tensors are stored on disk without shape information in an Example protocol buffer format (TFRecord files are collections of Examples). The documentation in the .proto file describes things fairly well, but the basic point is that Tensor entries are stored in row-major order with no shape information, so that must be provided when the Tensors are read. Note that the situation is similar for storing Tensors in memory: the shape information is kept separately, and just reshaping a Tensor changes only metadata (things like transpose, on the other hand, can be expensive).

VarLenFeatures are sequences such as sentences which would be difficult to batch together as regular Tensors, since the resulting shape would be ragged. The parse_example documentation has some good examples. Images are fixed length in that, if you load a batch of them, they'll all have the same shape (e.g. they're all 32x32 pixels, so a batch of 10 can have shape 10x32x32).

Upvotes: 1

Related Questions