Keras API

keras module provides API that you can use to train your model with differential privacy in Keras. Currently, only DP-SGD algorithm is supported. It works the following way: first you create a keras model, then you wrap it with make_private function passing essential parameters for DP training. make_private preserves the same interface that a usual Keras model has, so use it as you would use a regular Keras model. For example, call fit to train it and get a DP model. Refer to the API Reference section to see the meaning of each DP parameter. The example below shows that.

Example Usage

This section demonstrates how to integrate the Keras API into a typical Keras training workflow.

examples/keras_api_example.py
 1  (x_train, y_train), (x_test, y_test) = load_data()
 2  model = get_model()
 3
 4  epsilon = 1.1
 5  delta = 1e-5
 6  batch_size = 128
 7  epochs = 5
 8  train_size = len(x_train)
 9  dp = True
10  clipping_norm = 1.0
11
12  if dp:
13    params = keras_api.DPKerasConfig(
14        epsilon=epsilon,
15        delta=delta,
16        clipping_norm=clipping_norm,
17        batch_size=batch_size,
18        train_steps=epochs * (train_size // batch_size),
19        train_size=train_size,
20        seed=0,
21    )
22    model = keras_api.make_private(model, params)
23    print(
24        f"DP training:{epsilon=} {delta=} {clipping_norm=} {batch_size=} "
25        f" {epochs=} {train_size=}"
26    )
27  else:
28    print("Non-DP training")
29  model.compile(
30      loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]
31  )
32  model.fit(
33      x_train,
34      y_train,
35      batch_size=batch_size,
36      epochs=epochs,
37      validation_data=(x_test, y_test),
38  )

API Reference