Exemplo n.º 1
0
    def testDNN(self, classes):
        train_features, train_labels = test_utils.make_input_data(256, classes)
        feature_columns = []
        for key in train_features:
            feature_columns.append(tf.feature_column.numeric_column(key=key))

        optimizer = functools.partial(DPKerasSGDOptimizer,
                                      learning_rate=0.5,
                                      l2_norm_clip=1.0,
                                      noise_multiplier=0.0,
                                      num_microbatches=1)

        classifier = dnn.DNNClassifier(hidden_units=[10],
                                       activation_fn='relu',
                                       feature_columns=feature_columns,
                                       n_classes=classes,
                                       optimizer=optimizer,
                                       loss_reduction=tf.losses.Reduction.NONE)

        classifier.train(input_fn=test_utils.make_input_fn(
            train_features, train_labels, True, 16))

        test_features, test_labels = test_utils.make_input_data(64, classes)
        classifier.evaluate(input_fn=test_utils.make_input_fn(
            test_features, test_labels, False, 16))

        predict_features, predict_labels = test_utils.make_input_data(
            64, classes)
        classifier.predict(input_fn=test_utils.make_input_fn(
            predict_features, predict_labels, False))
    def testCreateTPUEstimatorSpec(self):
        """Tests that an Estimator built with this head works."""

        train_features, train_labels = test_utils.make_input_data(256, 2)
        feature_columns = []
        for key in train_features:
            feature_columns.append(tf.feature_column.numeric_column(key=key))

        head = binary_class_head.DPBinaryClassHead()
        optimizer = DPKerasSGDOptimizer(learning_rate=0.5,
                                        l2_norm_clip=1.0,
                                        noise_multiplier=0.0,
                                        num_microbatches=2)
        model_fn = test_utils.make_model_fn(head, optimizer, feature_columns)
        classifier = tf.estimator.Estimator(model_fn=model_fn)

        classifier.train(input_fn=test_utils.make_input_fn(
            train_features, train_labels, True),
                         steps=4)

        test_features, test_labels = test_utils.make_input_data(64, 2)
        classifier.evaluate(input_fn=test_utils.make_input_fn(
            test_features, test_labels, False),
                            steps=4)

        predict_features, predict_labels_ = test_utils.make_input_data(64, 2)
        classifier.predict(input_fn=test_utils.make_input_fn(
            predict_features, predict_labels_, False))
Exemplo n.º 3
0
    def testCreateTPUEstimatorSpec(self, n_classes):
        """Tests that an Estimator built with a binary head works."""

        train_features, train_labels = test_utils.make_input_data(
            256, n_classes)
        feature_columns = []
        for key in train_features:
            feature_columns.append(tf.feature_column.numeric_column(key=key))

        head = head_lib._binary_logistic_or_multi_class_head(
            n_classes=n_classes,
            weight_column=None,
            label_vocabulary=None,
            loss_reduction=tf.compat.v1.losses.Reduction.NONE)
        optimizer = DPGradientDescentGaussianOptimizer(learning_rate=0.5,
                                                       l2_norm_clip=1.0,
                                                       noise_multiplier=0.0,
                                                       num_microbatches=2)
        model_fn = make_model_fn(head, optimizer, feature_columns)
        classifier = tf_estimator.Estimator(model_fn=model_fn)

        classifier.train(input_fn=test_utils.make_input_fn(
            train_features, train_labels, True),
                         steps=4)

        test_features, test_labels = test_utils.make_input_data(64, n_classes)
        classifier.evaluate(input_fn=test_utils.make_input_fn(
            test_features, test_labels, False),
                            steps=4)

        predict_features, predict_labels = test_utils.make_input_data(
            64, n_classes)
        classifier.predict(input_fn=test_utils.make_input_fn(
            predict_features, predict_labels, False))