Repository: sql-machine-learning/elasticdl

Issue:
Simplify user API: don't rely on column name to find labels.



Discussion:

Resolving patch:
diff --git a/edl_k8s_examples/mnist_model.py b/edl_k8s_examples/mnist_model.py
--- a/edl_k8s_examples/mnist_model.py
+++ b/edl_k8s_examples/mnist_model.py
@@ -41,7 +41,7 @@ def call(self, inputs, training=False):
 def loss(output, labels):
     return tf.reduce_mean(
         tf.nn.sparse_softmax_cross_entropy_with_logits(
-            logits=output, labels=labels['label']))
+            logits=output, labels=labels))
 
 def optimizer(lr=0.1):
     return tf.train.GradientDescentOptimizer(lr)
@@ -65,4 +65,4 @@ def input_fn(records):
     images = np.concatenate(image_list, axis=0)
     images = np.reshape(images, (batch_size, 28, 28))
     labels = np.array(label_list)
-    return {'image': images, 'label': labels}
+    return ({'image': images}, labels)
diff --git a/elasticdl/worker/worker.py b/elasticdl/worker/worker.py
--- a/elasticdl/worker/worker.py
+++ b/elasticdl/worker/worker.py
@@ -108,7 +108,7 @@ def distributed_train(self):
                             self.get_model(
                                 max(self._model_version, min_model_version))
 
-                            batch_input_data = self._input_fn(record_buf)
+                            batch_input_data, batch_label = self._input_fn(record_buf)
 
                             with tf.GradientTape() as tape:
                                 inputs = []
@@ -117,7 +117,7 @@ def distributed_train(self):
                                 if len(inputs) == 1:
                                     inputs = inputs[0]
                                 outputs = self._model.call(inputs, training=True)
-                                loss = self._loss(outputs, batch_input_data)
+                                loss = self._loss(outputs, batch_label)
 
                                 # TODO:  Add regularization loss if any,
                                 #        which should be divided by the number of contributing workers.
@@ -158,7 +158,7 @@ def local_train(self, file_list, batch_size, epoch=1, kwargs=None):
                         if not record_buf:
                             break
 
-                        data = self._input_fn(record_buf)
+                        data, labels = self._input_fn(record_buf)
 
                         with tf.GradientTape() as tape:
                             inputs = []
@@ -167,7 +167,7 @@ def local_train(self, file_list, batch_size, epoch=1, kwargs=None):
                             if len(inputs) == 1:
                                 inputs = inputs[0]
                             outputs = self._model.call(inputs, training=True)
-                            loss = self._loss(outputs, data)
+                            loss = self._loss(outputs, labels)
 
                             # Add regularization loss if any.
                             # Note: for distributed training, the regularization loss should


Test patch:
diff --git a/elasticdl/worker/test_module.py b/elasticdl/worker/test_module.py
--- a/elasticdl/worker/test_module.py
+++ b/elasticdl/worker/test_module.py
@@ -11,7 +11,7 @@
 input_names = ['x']
 
 def loss(outputs, labels):
-    return tf.reduce_mean(tf.square(outputs - labels['y'])) 
+    return tf.reduce_mean(tf.square(outputs - labels)) 
 
 def input_fn(records):
     x_list = []
@@ -27,7 +27,7 @@ def input_fn(records):
     xs = np.reshape(xs, (batch_size, 1))
     ys = np.concatenate(y_list, axis=0)
     ys = np.reshape(xs, (batch_size, 1))
-    return {'x': xs, 'y': ys}
+    return ({'x': xs}, ys)
 
 def optimizer(lr=0.1):
     return tf.train.GradientDescentOptimizer(lr)