io icon indicating copy to clipboard operation
io copied to clipboard

tensorflow.org avro.ipynb Failing

Open MarkDaoust opened this issue 3 years ago • 1 comments
trafficstars

The tensorflow.org import for avro.ipynb is failing:

------------------
model.fit(x=dataset, epochs=1, steps_per_epoch=1, verbose=1)
------------------

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_16881/4054086148.py in <module>
----> 1 model.fit(x=dataset, epochs=1, steps_per_epoch=1, verbose=1)

/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

TypeError: in user code:

    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 813, in train_step
        f'Target data is missing. Your model has `loss`: {self.loss}, '

    TypeError: Target data is missing. Your model has `loss`: mse, and therefore expects target data to be passed in `fit()`.

TypeError: in user code:

    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/keras/engine/training.py", line 813, in train_step
        f'Target data is missing. Your model has `loss`: {self.loss}, '

    TypeError: Target data is missing. Your model has `loss`: mse, and therefore expects target data to be passed in `fit()`.

MarkDaoust avatar Nov 22 '21 17:11 MarkDaoust

This notebook is calling model.fit(dataset) but dataset has only been generated with one feature, no labels:

features = {
    'features[*]': tfio.experimental.columnar.VarLenFeatureWithRank(dtype=tf.int32)
}

Running the model like this worked, but I don't know the avro library at all, and I get the feeling there's a better solution (e.g. if we could add as_supervised=True to make_avro_record_dataset):

features = {
    'features[*]': tfio.experimental.columnar.VarLenFeatureWithRank(dtype=tf.int32),
    'label': tf.io.FixedLenFeature(shape=[], dtype=tf.int32, default_value=-100),
}
schema = ...
dataset = ...
model = ...

def extract_label(feature):
  label = feature.pop('label')
  return tf.sparse.to_dense(feature['features[*]']), label

model.fit(x=dataset.map(extract_label), epochs=1, steps_per_epoch=1, verbose=1)

markmcd avatar Nov 23 '21 02:11 markmcd