<jupyter_start><jupyter_text>Training using a model with privacy preserving method (Differential Privacy)- hyper parameters to ad noise and clip gradients are used for differential privacy<jupyter_code>epochs, batch_size, learning_rate = 100, 32, 0.2
#privacy parameter
l2_norm_clip, noise_multiplier, num_microbatches = 1.0, 0.2, batch_size
if batch_size % num_microbatches != 0:
    num_microbatches = batch_size
train_size, val_size = int(trainX.shape[0]/batch_size) * batch_size, int(valX.shape[0]/batch_size) * batch_size
trainXm, trainYm, valXm, valYm = trainX[:train_size], trainY[:train_size], valX[:val_size], valY[:val_size]
from tensorflow_privacy.privacy.analysis import compute_dp_sgd_privacy
compute_dp_sgd_privacy.compute_dp_sgd_privacy(n=train_size, batch_size=batch_size, noise_multiplier=noise_multiplier, epochs=epochs, delta=1/train_size)
from modules.model_definition import AlexNet
from modules.DM import trainer_differential_privacy
from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras import DPKerasSGDOptimizer

# uncomment to save the model
model_path = None# os.path.join(model_dir, 'AlexNet_differential_1d_batch-' + str(batch_size) + '_l2-' + str(l2_norm_clip) + '_noise-' + str(noise_multiplier) + '.h5')
model = AlexNet().build_1d(trainX.shape[1:], n_classes, activation='softmax', verbose=1)
optimizer = DPKerasSGDOptimizer(l2_norm_clip=l2_norm_clip, noise_multiplier=noise_multiplier, num_microbatches=num_microbatches, learning_rate=learning_rate)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction=tf.losses.Reduction.NONE)
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
trainer_differential_privacy.traintf(sess, model, trainXm, trainYm, validation_data=(valXm, valYm), 
                                        epochs=epochs, batch_size=batch_size, model_path=model_path, verbose=1)
test_size = int(testX.shape[0]/batch_size) * batch_size
testXm = testX
if test_size < testX.shape[0]:
    fill = np.zeros((batch_size - (testX.shape[0] - test_size), *testX.shape[1:]))
    testXm = np.concatenate([testX, fill], axis=0)
# uncomment to save the model
report_path = None#os.path.join(result_dir, 'AlexNet_differential_1d_batch-' + str(batch_size) + '_l2-' + str(l2_norm_clip) + '_noise-' + str(noise_multiplier) + '_report.txt')
preds = np.argmax(model.predict(testXm, batch_size=batch_size, verbose=1), axis=1)
utils.compute_classification_report(testY, preds[:testX.shape[0]], save=report_path, verbose=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1150197628458498, 'fraction_numerical': 0.015019762845849802, 'mean_word_length': 7.408637873754153, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '371628', 'license': 'MIT', 'n_tokens_mistral': 935, 'n_tokens_neox': 901, 'n_words': 158, 'path': 'src/notebooks/DM/notebook_differential_privacy.ipynb', 'repo_name': 'DominiqueMercier/PPML-TSA', 'size': '2530'}