@@ -0,0 +1,3198 @@
+#%0A# Copyright 2018 Analytics Zoo Authors.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A#%0A%0Aimport tensorflow as tf%0Aimport numpy as np%0A%0Afrom zoo import init_nncontext%0Afrom zoo.tfpark import TFDataset%0Afrom zoo.tfpark.estimator import TFEstimator, TFEstimatorSpec%0A%0A%0Adef get_data_rdd(dataset, sc):%0A    from bigdl.dataset import mnist%0A    (images_data, labels_data) = mnist.read_data_sets(%22/tmp/mnist%22, dataset)%0A    image_rdd = sc.parallelize(images_data)%0A    labels_rdd = sc.parallelize(labels_data)%0A    rdd = image_rdd.zip(labels_rdd) %5C%0A        .map(lambda rec_tuple: ((rec_tuple%5B0%5D - mnist.TRAIN_MEAN) / mnist.TRAIN_STD,%0A                                np.array(rec_tuple%5B1%5D)))%0A    return rdd%0A%0A%0Adef main():%0A    sc = init_nncontext()%0A%0A    def model_fn(features, labels, mode):%0A        from nets import lenet%0A        slim = tf.contrib.slim%0A        with slim.arg_scope(lenet.lenet_arg_scope()):%0A            logits, end_points = lenet.lenet(features, num_classes=10, is_training=True)%0A%0A        if mode == tf.estimator.ModeKeys.EVAL or mode == tf.estimator.ModeKeys.TRAIN:%0A            loss = tf.reduce_mean(%0A                tf.losses.sparse_softmax_cross_entropy(logits=logits, labels=labels))%0A            return TFEstimatorSpec(mode, predictions=logits, loss=loss)%0A        else:%0A            return TFEstimatorSpec(mode, predictions=logits)%0A%0A    def input_fn(mode):%0A        if mode == tf.estimator.ModeKeys.TRAIN:%0A            training_rdd = get_data_rdd(%22train%22, sc)%0A            dataset = TFDataset.from_rdd(training_rdd,%0A                                         features=(tf.float32, %5B28, 28, 1%5D),%0A                                         labels=(tf.int32, %5B%5D),%0A                                         batch_size=320)%0A        elif mode == tf.estimator.ModeKeys.EVAL:%0A            testing_rdd = get_data_rdd(%22test%22, sc)%0A            dataset = TFDataset.from_rdd(testing_rdd,%0A                                         features=(tf.float32, %5B28, 28, 1%5D),%0A                                         labels=(tf.int32, %5B%5D),%0A                                         batch_size=320)%0A        else:%0A            testing_rdd = get_data_rdd(%22test%22, sc).map(lambda x: x%5B0%5D)%0A            dataset = TFDataset.from_rdd(testing_rdd,%0A                                         features=(tf.float32, %5B28, 28, 1%5D),%0A                                         batch_per_thread=80)%0A%0A        return dataset%0A    estimator = TFEstimator(model_fn, tf.train.AdamOptimizer(), model_dir=%22/tmp/estimator%22)%0A%0A    estimator.train(input_fn, steps=60000//320)%0A%0A    metrics = estimator.evaluate(input_fn, %5B%22acc%22%5D)%0A    print(metrics)%0A%0A    predictions = estimator.predict(input_fn)%0A%0A    print(predictions.first())%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add TFPark Estimator API (#1207)