@@ -0,0 +1,3362 @@
+#%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#%0Aimport pytest%0A%0Afrom bigdl.optim.optimizer import Adam, MaxEpoch%0Afrom zoo.pipeline.api.keras.metrics import Accuracy%0Afrom zoo.pipeline.api.net import TFOptimizer%0Afrom test.zoo.pipeline.utils.test_utils import ZooTestCase%0Aimport tensorflow as tf%0Aimport numpy as np%0Aimport os%0A%0Afrom zoo.tfpark import KerasModel, TFDataset%0A%0A%0Aclass TestTFParkModel(ZooTestCase):%0A%0A    def test_tf_optimizer_with_sparse_gradient(self):%0A        ids = np.random.randint(0, 10, size=%5B40%5D)%0A        labels = np.random.randint(0, 5, size=%5B40%5D)%0A        id_rdd = self.sc.parallelize(ids)%0A        label_rdd = self.sc.parallelize(labels)%0A        training_rdd = id_rdd.zip(label_rdd).map(lambda x: %5Bx%5B0%5D, x%5B1%5D%5D)%0A        with tf.Graph().as_default():%0A            dataset = TFDataset.from_rdd(training_rdd,%0A                                         names=%5B%22ids%22, %22labels%22%5D,%0A                                         shapes=%5B%5B%5D, %5B%5D%5D,%0A                                         types=%5Btf.int32, tf.int32%5D,%0A                                         batch_size=8)%0A            id_tensor, label_tensor = dataset.tensors%0A            embedding_table = tf.get_variable(%0A                name=%22word_embedding%22,%0A                shape=%5B10, 5%5D)%0A%0A            embedding = tf.nn.embedding_lookup(embedding_table, id_tensor)%0A            loss = tf.reduce_mean(tf.losses.%0A                                  sparse_softmax_cross_entropy(logits=embedding,%0A                                                               labels=label_tensor))%0A            optimizer = TFOptimizer(loss, Adam(1e-3))%0A            optimizer.optimize(end_trigger=MaxEpoch(1))%0A            optimizer.sess.close()%0A%0A    def test_tf_optimizer_metrics(self):%0A%0A        features = np.random.randn(20, 10)%0A        labels = np.random.randint(0, 10, size=%5B20%5D)%0A        with tf.Graph().as_default():%0A            dataset = TFDataset.from_ndarrays((features, labels),%0A                                              batch_size=4,%0A                                              val_tensors=(features, labels))%0A            feature_tensor, label_tensor = dataset.tensors%0A            output = tf.layers.dense(feature_tensor, 10)%0A            loss = tf.reduce_mean(tf.losses.%0A                                  sparse_softmax_cross_entropy(logits=output,%0A                                                               labels=label_tensor))%0A            optimizer = TFOptimizer.from_loss(loss, Adam(1e-3),%0A                                              val_outputs=%5Boutput%5D,%0A                                              val_labels=%5Blabel_tensor%5D,%0A                                              val_method=Accuracy(), metrics=%7B%22loss%22: loss%7D)%0A            optimizer.optimize(end_trigger=MaxEpoch(1))%0A            optimizer.sess.close()%0A%0A%0Aif __name__ == %22__main__%22:%0A    pytest.main(%5B__file__%5D)%0A

Support TensorFlow metric in TFOptimizer (#1593)