Repository: sql-machine-learning/elasticdl

Issue:
Error when running training on the worker
When working to let the model zoo support both running on SQLFlow and ElasticDL: https://github.com/sql-machine-learning/models/pull/27

I've encountered this error:

```
...
[2019-11-18 15:12:08,390] [INFO] [worker.py:642:_process_minibatch] Loss is 1.647733
[2019-11-18 15:12:08,482] [INFO] [task_data_service.py:173:_gen] Finish current dataset, maybe more data later
[2019-11-18 15:12:08,513] [INFO] [worker.py:709:_process_save_model_task_if_needed] The path to export model is model_output/1574089928
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/elasticdl/python/worker/main.py", line 64, in <module>
    main()
  File "/elasticdl/python/worker/main.py", line 60, in main
    worker.run()
  File "/elasticdl/python/worker/worker.py", line 876, in run
    self._train_and_evaluate()
  File "/elasticdl/python/worker/worker.py", line 825, in _train_and_evaluate
    self._process_save_model_task_if_needed()
  File "/elasticdl/python/worker/worker.py", line 712, in _process_save_model_task_if_needed
    self._model, dataset
  File "/elasticdl/python/common/model_handler.py", line 74, in get_model_to_export
    model._build_model_with_inputs(inputs=dataset, targets=None)
AttributeError: 'DNNClassifier' object has no attribute '_build_model_with_inputs'
```

Using this model: 
https://github.com/sql-machine-learning/models/pull/27/files#diff-34a6bf9b8dc3de74eb13bfcf012f9a2aR1

and the submit command was:

```
elasticdl train --image_base=sqlflow/sqlflow_models --model_def=dnnclassifier.DNNClassifier --training_data=sqlflow_test_iris_train  --data_reader_params='columns=["sepal_length", "sepal_width", "petal_length", "petal_width", "class"]' --envs='ODPS_PROJECT_NAME=gomaxcompute_driver_w7u,ODPS_ACCESS_ID=xxxxx,ODPS_ACCESS_KEY=xxxxx' --minibatch_size=32 --num_epochs=2 --model_zoo=/sqlflow_models --job_name=test-odps --num_minibatches_per_task=2 --image_pull_policy=Never --docker_base_url="tcp://192.168.64.4:2376" --docker_tlscert="/Users/wuyi/.minikube/certs/cert.pem" --docker_tlskey="/Users/wuyi/.minikube/certs/key.pem" --num_workers=2 --master_resource_request="cpu=400m,memory=256Mi" --master_resource_limit="cpu=1,memory=2048Mi" --worker_resource_request="cpu=400m,memory=256Mi" --worker_resource_limit="cpu=1,memory=3072Mi" --output=model_output --grads_to_wait=2
```

Environment:
Mac + Minikube


Discussion:
`_build_model_with_inputs` should be available for a `tf.keras.Model`. @workingloong @LiMinghao1994 Could you take a look?
I have checked that `_build_model_with_inputs` is available for subclass model and `tf.keras.Sequential`. Can you give me the source code of the `DNNClassifier`?
> I have checked that `_build_model_with_inputs` is available for subclass model and `tf.keras.Sequential`. Can you give me the source code of the `DNNClassifier`?

The source code is at: https://github.com/sql-machine-learning/models/pull/27/files#diff-34a6bf9b8dc3de74eb13bfcf012f9a2aR1

Resolving patch:
diff --git a/elasticdl/python/worker/worker.py b/elasticdl/python/worker/worker.py
--- a/elasticdl/python/worker/worker.py
+++ b/elasticdl/python/worker/worker.py
@@ -698,7 +698,12 @@ def _process_save_model_task_if_needed(self):
             dataset,
         ) = self._task_data_service.get_save_model_task_and_dataset()
         if task is not None and dataset is not None:
-            # TODO: Implement the save model execution process
+            dataset = self._dataset_fn(
+                dataset,
+                Mode.PREDICTION,
+                self._task_data_service.data_reader.metadata,
+            )
+            dataset = dataset.batch(self._minibatch_size)
             saved_model_path = task.extended_config.get(
                 SaveModelConfig.SAVED_MODEL_PATH
             )


Test patch: