C2w2 assignment exercise 2

Here is my function:

def get_records(dataset, num_records):
    '''Extracts records from the given dataset.
        dataset (TFRecordDataset): dataset saved by ExampleGen
        num_records (int): number of records to preview
    # initialize an empty list
    records = []

    # Use the `take()` method to specify how many records to get
    for tfrecord in dataset.take(num_records):
        # Get the numpy property of the tensor
        serialized_example = tfrecord.numpy()
        # Initialize a `tf.train.Example()` to read the serialized data
        example = tf.train.Example()
        # Read the example data (output is a protocol buffer message)
        # convert the protocol buffer message to a Python dictionary
        response = MessageToDict(pb)
        # append to the records list
    return records

when I run

# Get 3 records from the dataset
sample_records = get_records(dataset, 3)

# Print the output

I get this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-9-31db5e330f69> in <module>
      1 # Get 3 records from the dataset
----> 2 sample_records = get_records(dataset, 3)
      4 # Print the output
      5 pp.pprint(sample_records)

<ipython-input-8-3ea79f3055fe> in get_records(dataset, num_records)
     24         # convert the protocol buffer message to a Python dictionary
---> 25         response = MessageToDict(pb)
     27         # append to the records list

/opt/conda/lib/python3.8/site-packages/google/protobuf/json_format.py in MessageToDict(message, including_default_value_fields, preserving_proto_field_name, use_integers_for_enums, descriptor_pool, float_precision)
    173       float_precision=float_precision)
    174   # pylint: disable=protected-access
--> 175   return printer._MessageToJsonObject(message)

/opt/conda/lib/python3.8/site-packages/google/protobuf/json_format.py in _MessageToJsonObject(self, message)
    208   def _MessageToJsonObject(self, message):
    209     """Converts message to an object according to Proto3 JSON Specification."""
--> 210     message_descriptor = message.DESCRIPTOR
    211     full_name = message_descriptor.full_name
    212     if _IsWrapperMessage(message_descriptor):

AttributeError: 'int' object has no attribute 'DESCRIPTOR'

Everything up until that cell works as expected. Is it that I am using the MessageToDict signature incorrectly or that there is a version incompatibility issue with MessageToDict?

Hi Giorgio! Welcome to Discourse! I think the issue here is in appending to the records list. It looks like you’re appending to example_dict instead. Also note that ParseFromString doesn’t return anything. It is a method that will parse the string into the example. If it still doesn’t work, feel free to reference the related ungraded lab for this week to get more hints. Hope this helps!

Right, ParseFromString is a method, not a function, my bad. Then MessageToDict takes an instance of tf.train.Example(), whose state has been mutated by ParseFromString.

Thank you!