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))
Ejemplo n.º 2
0
def create_dp_optimizer(l2_clip, noise_multi, micro_batches, learning_rate):

    optimizer = DPKerasSGDOptimizer(l2_norm_clip=l2_clip,
                                    noise_multiplier=noise_multi,
                                    num_microbatches=micro_batches,
                                    learning_rate=learning_rate)

    return optimizer
def main(unused_argv):
    logging.set_verbosity(logging.INFO)
    if FLAGS.dpsgd and FLAGS.batch_size % FLAGS.microbatches != 0:
        raise ValueError(
            'Number of microbatches should divide evenly batch_size')

    # Load training and test data.
    train_data, train_labels, test_data, test_labels = load_mnist()

    # Define a sequential Keras model
    model = tf.keras.Sequential([
        tf.keras.layers.Conv2D(16,
                               8,
                               strides=2,
                               padding='same',
                               activation='relu',
                               input_shape=(28, 28, 1)),
        tf.keras.layers.MaxPool2D(2, 1),
        tf.keras.layers.Conv2D(32,
                               4,
                               strides=2,
                               padding='valid',
                               activation='relu'),
        tf.keras.layers.MaxPool2D(2, 1),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(32, activation='relu'),
        tf.keras.layers.Dense(10)
    ])

    if FLAGS.dpsgd:
        optimizer = DPKerasSGDOptimizer(
            l2_norm_clip=FLAGS.l2_norm_clip,
            noise_multiplier=FLAGS.noise_multiplier,
            num_microbatches=FLAGS.microbatches,
            learning_rate=FLAGS.learning_rate)
        # Compute vector of per-example loss rather than its mean over a minibatch.
        loss = tf.keras.losses.CategoricalCrossentropy(
            from_logits=True, reduction=tf.losses.Reduction.NONE)
    else:
        optimizer = tf.keras.optimizers.SGD(learning_rate=FLAGS.learning_rate)
        loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)

    # Compile model with Keras
    model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])

    # Train model with Keras
    model.fit(train_data,
              train_labels,
              epochs=FLAGS.epochs,
              validation_data=(test_data, test_labels),
              batch_size=FLAGS.batch_size)

    # Compute the privacy budget expended.
    if FLAGS.dpsgd:
        eps = compute_epsilon(FLAGS.epochs * 60000 // FLAGS.batch_size)
        print('For delta=1e-5, the current epsilon is: %.2f' % eps)
    else:
        print('Trained with vanilla non-private SGD optimizer')
Ejemplo n.º 4
0
 def __init__(self,
              num_epoch=60,
              dp_flag=0,
              l2_norm_clip=1.0,
              noise_multiplier=1.3,
              num_microbatches=25,
              learning_rate=0.01,
              data_size=10000,
              verbos=1,
              reduce=1):
     self.nm = noise_multiplier
     self.l2 = l2_norm_clip
     self.dp = dp_flag
     self.num_epoch = num_epoch
     self.batch_size = num_microbatches
     self.data_size = data_size
     self.verbos = verbos
     self.t_model = tf.keras.Sequential([
         tf.keras.layers.Dense(512, activation='relu'),
         tf.keras.layers.Dense(256, activation='relu'),
         tf.keras.layers.Dense(128, activation='relu'),
         tf.keras.layers.Dense(2, activation='softmax')
     ])
     if reduce:
         self.lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
             learning_rate,
             decay_steps=1000,
             decay_rate=0.96,
             staircase=True)
     else:
         self.lr_schedule = learning_rate
     if self.dp:
         self.opt = DPKerasSGDOptimizer(l2_norm_clip=self.l2,
                                        noise_multiplier=self.nm,
                                        num_microbatches=self.batch_size,
                                        learning_rate=self.lr_schedule)
     else:
         self.opt = tf.keras.optimizers.SGD(learning_rate=self.lr_schedule)
     self.loss = tf.keras.losses.SparseCategoricalCrossentropy(
         from_logits=True, reduction=tf.keras.losses.Reduction.NONE)
     self.t_model.compile(optimizer=self.opt,
                          loss=self.loss,
                          metrics=['accuracy'])
Ejemplo n.º 5
0
    def star_train(self):
        train_data, train_labels, val_data, val_labels, test_data, test_labels = self.load_candlestick(
        )

        optimizer = DPKerasSGDOptimizer(l2_norm_clip=self.l2_norm_clip,
                                        noise_multiplier=self.noise_multiplier,
                                        num_microbatches=30,
                                        learning_rate=self.learning_rate,
                                        momentum=0.9)

        loss = tf.keras.losses.CategoricalCrossentropy(
            from_logits=True, reduction=tf.losses.Reduction.NONE)
        model = self.create_model()
        model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
        self.history = model.fit(train_data.astype(np.float32),
                                 train_labels.astype(np.float32),
                                 epochs=self.epochs,
                                 validation_data=(val_data.astype(np.float32),
                                                  val_labels.astype(
                                                      np.float32)),
                                 batch_size=30)

        test_pred = model.predict(test_data)
        test_pred = np.argmax(test_pred, axis=1)
        test_true = np.argmax(test_labels, axis=1)
        test_result_cm = confusion_matrix(test_true,
                                          test_pred,
                                          labels=range(8))
        print(test_result_cm)
        count = 0
        for r in range(8):
            count += test_result_cm[r, r]
        print('testing accuracy:', count / np.sum(test_result_cm))

        self.eps = self.compute_epsilon(self.epochs * train_data.shape[0] //
                                        100)
        print('For delta=1e-5, the current epsilon is: %.2f' % self.eps)