def fit_discriminator(self, iterations, batch_size): if self.discrimination_mode == DiscriminatorGame.DetectEncryption: msgs, keys = gen_symmetric_encryption_data(iterations * batch_size, self.message_length) results = self.encryptor.predict([msgs, keys]) P, C, Y = replace_encryptions_with_random_entries(msgs, results, real_label=self.discriminator_real_label) return self.discriminator.fit( x=[P, C], y=Y, epochs=1, batch_size=batch_size, verbose=self.verbose ).history else: raise NotImplementedError('this GAN mode is not yet supported')
def fit_generator(self, iterations, batch_size): if self.discrimination_mode == DiscriminatorGame.DetectEncryption: msgs, keys = gen_symmetric_encryption_data(iterations * batch_size, self.message_length) real_labels = np.zeros( len(msgs)) if self.discriminator_real_label == 1 else np.ones( len(msgs)) return self.generator.fit(x=[msgs, keys], y=[msgs, real_labels], epochs=1, batch_size=batch_size, verbose=self.verbose).history else: raise NotImplementedError('this GAN mode is not yet supported')
def __call__(self, epochs=50, bob_prefit_epochs=10, eve_postfit_epochs=50, eve_minibatch_multiplier=2, iterations_per_epoch=100, batch_size=512): """ fit the model around randomly generated message/key pairs. :param epochs: the number of epochs to adversarially train for :param bob_prefit_epochs: the number of epochs to pre-fit bob for :param eve_postfit_epochs: the number of epochs to post-fit bob for :param eve_minibatch_multiplier: the minibatch multiplier for eve :param iterations_per_epoch: the iterations per epoch :param batch_size: the batch size :return: a dictionary of agent keys and concatenated training history values. when an agent is not fit while another is, their history is populated with float('nan') values. """ bob_histories = [] eve_histories = [] for i in range(0, bob_prefit_epochs): msgs, keys = gen_symmetric_encryption_data(iterations_per_epoch * batch_size) self.print('pre-fitting bob') bob_history = self.bob.fit(x=[msgs, keys], y=[msgs, msgs], epochs=1, batch_size=batch_size, verbose=self.verbose).history bob_histories.append(bob_history) eve_histories.append(nanify_dict_of_lists(bob_history)) self.save() for i in range(0, epochs): msgs, keys = gen_symmetric_encryption_data(iterations_per_epoch * batch_size) self.print('fitting bob') bob_history = self.bob.fit(x=[msgs, keys], y=[msgs, msgs], epochs=1, batch_size=batch_size, verbose=self.verbose).history bob_histories.append(bob_history) msgs, keys = gen_symmetric_encryption_data( eve_minibatch_multiplier * iterations_per_epoch * batch_size) self.print('fitting eve') eve_history = self.eve.fit(x=[msgs, keys], y=[msgs], epochs=1, batch_size=2 * batch_size, verbose=self.verbose).history eve_histories.append(eve_history) self.save() for i in range(0, eve_postfit_epochs): msgs, keys = gen_symmetric_encryption_data( iterations_per_epoch * eve_minibatch_multiplier * batch_size) print('fitting eve') eve_history = self.eve.fit(x=[msgs, keys], y=[msgs], epochs=1, batch_size=2 * batch_size, verbose=self.verbose).history eve_histories.append(eve_history) bob_histories.append(nanify_dict_of_lists(eve_history)) self.save() history = self.generate_cohesive_history({ 'alice / bob': join_list_valued_dictionaries(*bob_histories), 'eve': join_list_valued_dictionaries(*eve_histories) }) self.history = history return history