TestX = np.concatenate( (TestX, utils.multi_prep(TestXt, target_sig_length))) TrainY = np.concatenate((TrainY, TrainYt)) TestY = np.concatenate((TestY, TestYt)) del TrainXt, TestXt, TrainYt, TestYt TrainX = np.expand_dims(TrainX, axis=2) TestX = np.expand_dims(TestX, axis=2) TrainY = to_categorical(TrainY, num_classes=len(target_class)) TestY = to_categorical(TestY, num_classes=len(target_class)) toc = time.time() print('Time for data processing--- ' + str(toc - tic) + ' seconds---') # ------------------------ 网络生成与训练 ---------------------------- model = net.build_network(config) model.summary() model_name = 'myNet.h5' checkpoint = ModelCheckpoint(filepath=model_name, monitor='val_categorical_accuracy', mode='max', save_best_only='True') lr_scheduler = LearningRateScheduler(config.lr_schedule) callback_lists = [checkpoint, lr_scheduler] model.fit(x=TrainX, y=TrainY, batch_size=config.batch_size, epochs=50, verbose=1, validation_data=(TestX, TestY),
def bits_loss(msg, output, message_length): """Autoencoder error in number of different bits.""" return reconstruction_loss(msg, output) * message_length message_length = 16 # in bits key_length = message_length # in bits batch = 512 # Number of messages to train on at once adv_iter = 100 # Adversarial iterations max_iter = 20 # Individual agent iterations learning_rate = 0.0008 if __name__ == "__main__": msg, key = build_input_layers(message_length, key_length) alice_output, bob_output, eve_output = build_network(msg, key) eve_loss = reconstruction_loss(msg, eve_output) bob_reconst_loss = reconstruction_loss(msg, bob_output) bob_loss = bob_reconst_loss + (0.5 - eve_loss)**2 eve_bit_loss = bits_loss(msg, eve_output, message_length) bob_bit_loss = bits_loss(msg, bob_output, message_length) AB_vars = (tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "alice") + tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "bob")) E_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "eve") trainAB = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize( bob_loss, var_list=AB_vars)