From c7adabde42139fd8ba55017b4d7cf244853f5738 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 15:17:36 -0800 Subject: [PATCH 1/7] smaller input size --- CaloGAN/models/train.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/CaloGAN/models/train.py b/CaloGAN/models/train.py index 98aac7e..28f947e 100644 --- a/CaloGAN/models/train.py +++ b/CaloGAN/models/train.py @@ -56,7 +56,10 @@ def get_parser(): help='batch size per update') parser.add_argument('--latent-size', action='store', type=int, default=1024, - help='size of random N(0, 1) latent space to sample') + help='size of QCBM prior latent space to sample') + + parser.add_argument('--nb-samples', action='store', type=int, default=-1, + help='number of samples to train') parser.add_argument('--disc-lr', action='store', type=float, default=2e-5, help='Adam learning rate for discriminator') @@ -130,6 +133,7 @@ def get_parser(): nb_epochs = parse_args.nb_epochs batch_size = parse_args.batch_size latent_size = parse_args.latent_size + nb_samples = parse_args.nb_samples verbose = parse_args.prog_bar no_attn = parse_args.no_attn @@ -144,6 +148,7 @@ def get_parser(): logger.debug('number of epochs = {}'.format(nb_epochs)) logger.debug('batch size = {}'.format(batch_size)) logger.debug('latent size = {}'.format(latent_size)) + logger.debug('number of samples = {}'.format(latent_size)) logger.debug('progress bar enabled = {}'.format(verbose)) logger.debug('Using attention = {}'.format(no_attn == False)) logger.debug('discriminator learning rate = {}'.format(disc_lr)) @@ -170,11 +175,12 @@ def _load_data(particle, datafile): d = h5py.File(datafile, 'r') # make our calo images channels-last - first = np.expand_dims(d['layer_0'][:], -1) - second = np.expand_dims(d['layer_1'][:], -1) - third = np.expand_dims(d['layer_2'][:], -1) + first = np.expand_dims(d['layer_0'][:nb_samples], -1) + second = np.expand_dims(d['layer_1'][:nb_samples], -1) + third = np.expand_dims(d['layer_2'][:nb_samples], -1) + # convert to MeV - energy = d['energy'][:].reshape(-1, 1) * 1000 + energy = d['energy'][:nb_samples].reshape(-1, 1) * 1000 sizes = [ first.shape[1], first.shape[2], @@ -477,8 +483,8 @@ def _load_data(particle, datafile): epoch + 1, np.mean(epoch_disc_loss, axis=0))) # save weights every epoch - generator.save_weights('{0}{1:03d}.hdf5'.format(parse_args.g_pfx, epoch), + generator.save_weights('./weights/{0}{1:03d}.hdf5'.format(parse_args.g_pfx, epoch), overwrite=True) - discriminator.save_weights('{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), + discriminator.save_weights('./weights/{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), overwrite=True) From 51a23f2e250c6c451710e5299bc4a91606093186 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 16:38:52 -0800 Subject: [PATCH 2/7] sample from QCBM for prior --- CaloGAN/models/train.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/CaloGAN/models/train.py b/CaloGAN/models/train.py index 28f947e..45aca29 100644 --- a/CaloGAN/models/train.py +++ b/CaloGAN/models/train.py @@ -56,7 +56,16 @@ def get_parser(): help='batch size per update') parser.add_argument('--latent-size', action='store', type=int, default=1024, - help='size of QCBM prior latent space to sample') + help='size of classical prior from N(0,1) to sample') + + parser.add_argument('--nb-qubits', action='store', type=int, default=8, + help='number of qubits to use for QCBM') + + parser.add_argument('--qcbm-nb-layer', action='store', type=int, default=7, + help='number of layers for QCBM ansatz') + + parser.add_argument('--qcbm-nb-shots', action='store', type=int, default=20000, + help='number of shots for QCBM') parser.add_argument('--nb-samples', action='store', type=int, default=-1, help='number of samples to train') @@ -115,6 +124,7 @@ def get_parser(): calculate_energy, scale, inpainting_attention) from architectures import build_generator, build_discriminator + from qcbm import qcbm_approx_probs, qcbm_probs, initialize_weights # batch, latent size, and whether or not to be verbose with a progress bar @@ -133,6 +143,9 @@ def get_parser(): nb_epochs = parse_args.nb_epochs batch_size = parse_args.batch_size latent_size = parse_args.latent_size + nb_qubits = parse_args.nb_qubits + qcbm_nb_layers = parse_args.qcbm_nb_layer + qcbm_nb_shots = parse_args.qcbm_nb_shots nb_samples = parse_args.nb_samples verbose = parse_args.prog_bar no_attn = parse_args.no_attn @@ -143,12 +156,15 @@ def get_parser(): yaml_file = parse_args.dataset + if nb_qubits > 0: + latent_size = 2**nb_qubits + logger.debug('parameter configuration:') logger.debug('number of epochs = {}'.format(nb_epochs)) logger.debug('batch size = {}'.format(batch_size)) logger.debug('latent size = {}'.format(latent_size)) - logger.debug('number of samples = {}'.format(latent_size)) + logger.debug('number of image samples = {}'.format(nb_samples)) logger.debug('progress bar enabled = {}'.format(verbose)) logger.debug('Using attention = {}'.format(no_attn == False)) logger.debug('discriminator learning rate = {}'.format(disc_lr)) @@ -376,6 +392,7 @@ def _load_data(particle, datafile): logger.info('commencing training') + qcbm_weights = initialize_weights(qcbm_nb_layers, nb_qubits) for epoch in range(nb_epochs): logger.info('Epoch {} of {}'.format(epoch + 1, nb_epochs)) @@ -395,8 +412,15 @@ def _load_data(particle, datafile): elif index % 10 == 0: logger.debug('processed {}/{} batches'.format(index + 1, nb_batches)) - # generate a new batch of noise - noise = np.random.normal(0, 1, (batch_size, latent_size)) + # sample from QCBM + if nb_qubits > 0: + logger.info('sampling prior from QCBM...') + noise = qcbm_approx_probs(qcbm_weights, nb_qubits) + noise = np.array([i for i in noise.values()]) + noise = np.concatenate((noise,np.zeros(latent_size-noise.size))) + noise = np.tile(noise, (batch_size, 1)) + else: + noise = np.random.normal(0, 1, (batch_size, latent_size)) # get a batch of real images image_batch_1 = first[index * batch_size:(index + 1) * batch_size] From 491f5d8ef4407da378904e9f7e711f635b8ee68f Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 16:39:17 -0800 Subject: [PATCH 3/7] move qcbm to model --- qcbm_train.py => CaloGAN/models/qcbm.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename qcbm_train.py => CaloGAN/models/qcbm.py (100%) diff --git a/qcbm_train.py b/CaloGAN/models/qcbm.py similarity index 100% rename from qcbm_train.py rename to CaloGAN/models/qcbm.py From 1b15141677a67f5211d8d5490000077c3c69a920 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 16:39:47 -0800 Subject: [PATCH 4/7] change name to QC-CaloGAN --- {CaloGAN => QC-CaloGAN}/LICENSE | 0 {CaloGAN => QC-CaloGAN}/README.md | 0 .../analysis/CaloGANTrainingComparison.ipynb | 0 {CaloGAN => QC-CaloGAN}/analysis/analysis.ipynb | 0 {CaloGAN => QC-CaloGAN}/analysis/feats1d.py | 0 {CaloGAN => QC-CaloGAN}/analysis/ndap.py | 0 {CaloGAN => QC-CaloGAN}/docker/Dockerfile | 0 {CaloGAN => QC-CaloGAN}/docker/requirements.txt | 0 {CaloGAN => QC-CaloGAN}/figures/3d.jpg | Bin .../figures/caloGAN_discr_rev.jpg | Bin {CaloGAN => QC-CaloGAN}/figures/caloGAN_gen.jpg | Bin {CaloGAN => QC-CaloGAN}/generation/.root | Bin {CaloGAN => QC-CaloGAN}/generation/GNUmakefile | 0 .../generation/batch_generate.sh | 0 {CaloGAN => QC-CaloGAN}/generation/cfg/pdsf-env.sh | 0 {CaloGAN => QC-CaloGAN}/generation/cfg/run2.mac | 0 {CaloGAN => QC-CaloGAN}/generation/convert.py | 0 .../generation/convert_to_cells_fine.py | 0 .../generation/convert_to_cells_v2.py | 0 {CaloGAN => QC-CaloGAN}/generation/extract_cell.py | 0 {CaloGAN => QC-CaloGAN}/generation/generate.cc | 0 .../generation/include/ActionInitialization.hh | 0 .../generation/include/Analysis.hh | 0 .../generation/include/DetectorConstruction.hh | 0 .../generation/include/EventAction.hh | 0 .../generation/include/PrimaryGeneratorAction.hh | 0 .../generation/include/RunAction.hh | 0 .../generation/include/RunData.hh | 0 .../generation/include/SteppingAction.hh | 0 {CaloGAN => QC-CaloGAN}/generation/launch | 0 .../generation/plot_1D_features.py | 0 .../generation/src/ActionInitialization.cc | 0 .../generation/src/DetectorConstruction.cc | 0 .../generation/src/EventAction.cc | 0 .../generation/src/PrimaryGeneratorAction.cc | 0 {CaloGAN => QC-CaloGAN}/generation/src/RunAction.cc | 0 {CaloGAN => QC-CaloGAN}/generation/src/RunData.cc | 0 .../generation/src/SteppingAction.cc | 0 {CaloGAN => QC-CaloGAN}/models/__init__.py | 0 {CaloGAN => QC-CaloGAN}/models/architectures.py | 0 {CaloGAN => QC-CaloGAN}/models/ops.py | 0 {CaloGAN => QC-CaloGAN}/models/particles.yaml | 0 {CaloGAN => QC-CaloGAN}/models/qcbm.py | 0 {CaloGAN => QC-CaloGAN}/models/train.py | 0 {CaloGAN => QC-CaloGAN}/models/train_cgan.py | 0 45 files changed, 0 insertions(+), 0 deletions(-) rename {CaloGAN => QC-CaloGAN}/LICENSE (100%) rename {CaloGAN => QC-CaloGAN}/README.md (100%) rename {CaloGAN => QC-CaloGAN}/analysis/CaloGANTrainingComparison.ipynb (100%) rename {CaloGAN => QC-CaloGAN}/analysis/analysis.ipynb (100%) rename {CaloGAN => QC-CaloGAN}/analysis/feats1d.py (100%) rename {CaloGAN => QC-CaloGAN}/analysis/ndap.py (100%) rename {CaloGAN => QC-CaloGAN}/docker/Dockerfile (100%) rename {CaloGAN => QC-CaloGAN}/docker/requirements.txt (100%) rename {CaloGAN => QC-CaloGAN}/figures/3d.jpg (100%) rename {CaloGAN => QC-CaloGAN}/figures/caloGAN_discr_rev.jpg (100%) rename {CaloGAN => QC-CaloGAN}/figures/caloGAN_gen.jpg (100%) rename {CaloGAN => QC-CaloGAN}/generation/.root (100%) rename {CaloGAN => QC-CaloGAN}/generation/GNUmakefile (100%) rename {CaloGAN => QC-CaloGAN}/generation/batch_generate.sh (100%) rename {CaloGAN => QC-CaloGAN}/generation/cfg/pdsf-env.sh (100%) rename {CaloGAN => QC-CaloGAN}/generation/cfg/run2.mac (100%) rename {CaloGAN => QC-CaloGAN}/generation/convert.py (100%) rename {CaloGAN => QC-CaloGAN}/generation/convert_to_cells_fine.py (100%) rename {CaloGAN => QC-CaloGAN}/generation/convert_to_cells_v2.py (100%) rename {CaloGAN => QC-CaloGAN}/generation/extract_cell.py (100%) rename {CaloGAN => QC-CaloGAN}/generation/generate.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/ActionInitialization.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/Analysis.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/DetectorConstruction.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/EventAction.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/PrimaryGeneratorAction.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/RunAction.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/RunData.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/include/SteppingAction.hh (100%) rename {CaloGAN => QC-CaloGAN}/generation/launch (100%) rename {CaloGAN => QC-CaloGAN}/generation/plot_1D_features.py (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/ActionInitialization.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/DetectorConstruction.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/EventAction.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/PrimaryGeneratorAction.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/RunAction.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/RunData.cc (100%) rename {CaloGAN => QC-CaloGAN}/generation/src/SteppingAction.cc (100%) rename {CaloGAN => QC-CaloGAN}/models/__init__.py (100%) rename {CaloGAN => QC-CaloGAN}/models/architectures.py (100%) rename {CaloGAN => QC-CaloGAN}/models/ops.py (100%) rename {CaloGAN => QC-CaloGAN}/models/particles.yaml (100%) rename {CaloGAN => QC-CaloGAN}/models/qcbm.py (100%) rename {CaloGAN => QC-CaloGAN}/models/train.py (100%) rename {CaloGAN => QC-CaloGAN}/models/train_cgan.py (100%) diff --git a/CaloGAN/LICENSE b/QC-CaloGAN/LICENSE similarity index 100% rename from CaloGAN/LICENSE rename to QC-CaloGAN/LICENSE diff --git a/CaloGAN/README.md b/QC-CaloGAN/README.md similarity index 100% rename from CaloGAN/README.md rename to QC-CaloGAN/README.md diff --git a/CaloGAN/analysis/CaloGANTrainingComparison.ipynb b/QC-CaloGAN/analysis/CaloGANTrainingComparison.ipynb similarity index 100% rename from CaloGAN/analysis/CaloGANTrainingComparison.ipynb rename to QC-CaloGAN/analysis/CaloGANTrainingComparison.ipynb diff --git a/CaloGAN/analysis/analysis.ipynb b/QC-CaloGAN/analysis/analysis.ipynb similarity index 100% rename from CaloGAN/analysis/analysis.ipynb rename to QC-CaloGAN/analysis/analysis.ipynb diff --git a/CaloGAN/analysis/feats1d.py b/QC-CaloGAN/analysis/feats1d.py similarity index 100% rename from CaloGAN/analysis/feats1d.py rename to QC-CaloGAN/analysis/feats1d.py diff --git a/CaloGAN/analysis/ndap.py b/QC-CaloGAN/analysis/ndap.py similarity index 100% rename from CaloGAN/analysis/ndap.py rename to QC-CaloGAN/analysis/ndap.py diff --git a/CaloGAN/docker/Dockerfile b/QC-CaloGAN/docker/Dockerfile similarity index 100% rename from CaloGAN/docker/Dockerfile rename to QC-CaloGAN/docker/Dockerfile diff --git a/CaloGAN/docker/requirements.txt b/QC-CaloGAN/docker/requirements.txt similarity index 100% rename from CaloGAN/docker/requirements.txt rename to QC-CaloGAN/docker/requirements.txt diff --git a/CaloGAN/figures/3d.jpg b/QC-CaloGAN/figures/3d.jpg similarity index 100% rename from CaloGAN/figures/3d.jpg rename to QC-CaloGAN/figures/3d.jpg diff --git a/CaloGAN/figures/caloGAN_discr_rev.jpg b/QC-CaloGAN/figures/caloGAN_discr_rev.jpg similarity index 100% rename from CaloGAN/figures/caloGAN_discr_rev.jpg rename to QC-CaloGAN/figures/caloGAN_discr_rev.jpg diff --git a/CaloGAN/figures/caloGAN_gen.jpg b/QC-CaloGAN/figures/caloGAN_gen.jpg similarity index 100% rename from CaloGAN/figures/caloGAN_gen.jpg rename to QC-CaloGAN/figures/caloGAN_gen.jpg diff --git a/CaloGAN/generation/.root b/QC-CaloGAN/generation/.root similarity index 100% rename from CaloGAN/generation/.root rename to QC-CaloGAN/generation/.root diff --git a/CaloGAN/generation/GNUmakefile b/QC-CaloGAN/generation/GNUmakefile similarity index 100% rename from CaloGAN/generation/GNUmakefile rename to QC-CaloGAN/generation/GNUmakefile diff --git a/CaloGAN/generation/batch_generate.sh b/QC-CaloGAN/generation/batch_generate.sh similarity index 100% rename from CaloGAN/generation/batch_generate.sh rename to QC-CaloGAN/generation/batch_generate.sh diff --git a/CaloGAN/generation/cfg/pdsf-env.sh b/QC-CaloGAN/generation/cfg/pdsf-env.sh similarity index 100% rename from CaloGAN/generation/cfg/pdsf-env.sh rename to QC-CaloGAN/generation/cfg/pdsf-env.sh diff --git a/CaloGAN/generation/cfg/run2.mac b/QC-CaloGAN/generation/cfg/run2.mac similarity index 100% rename from CaloGAN/generation/cfg/run2.mac rename to QC-CaloGAN/generation/cfg/run2.mac diff --git a/CaloGAN/generation/convert.py b/QC-CaloGAN/generation/convert.py similarity index 100% rename from CaloGAN/generation/convert.py rename to QC-CaloGAN/generation/convert.py diff --git a/CaloGAN/generation/convert_to_cells_fine.py b/QC-CaloGAN/generation/convert_to_cells_fine.py similarity index 100% rename from CaloGAN/generation/convert_to_cells_fine.py rename to QC-CaloGAN/generation/convert_to_cells_fine.py diff --git a/CaloGAN/generation/convert_to_cells_v2.py b/QC-CaloGAN/generation/convert_to_cells_v2.py similarity index 100% rename from CaloGAN/generation/convert_to_cells_v2.py rename to QC-CaloGAN/generation/convert_to_cells_v2.py diff --git a/CaloGAN/generation/extract_cell.py b/QC-CaloGAN/generation/extract_cell.py similarity index 100% rename from CaloGAN/generation/extract_cell.py rename to QC-CaloGAN/generation/extract_cell.py diff --git a/CaloGAN/generation/generate.cc b/QC-CaloGAN/generation/generate.cc similarity index 100% rename from CaloGAN/generation/generate.cc rename to QC-CaloGAN/generation/generate.cc diff --git a/CaloGAN/generation/include/ActionInitialization.hh b/QC-CaloGAN/generation/include/ActionInitialization.hh similarity index 100% rename from CaloGAN/generation/include/ActionInitialization.hh rename to QC-CaloGAN/generation/include/ActionInitialization.hh diff --git a/CaloGAN/generation/include/Analysis.hh b/QC-CaloGAN/generation/include/Analysis.hh similarity index 100% rename from CaloGAN/generation/include/Analysis.hh rename to QC-CaloGAN/generation/include/Analysis.hh diff --git a/CaloGAN/generation/include/DetectorConstruction.hh b/QC-CaloGAN/generation/include/DetectorConstruction.hh similarity index 100% rename from CaloGAN/generation/include/DetectorConstruction.hh rename to QC-CaloGAN/generation/include/DetectorConstruction.hh diff --git a/CaloGAN/generation/include/EventAction.hh b/QC-CaloGAN/generation/include/EventAction.hh similarity index 100% rename from CaloGAN/generation/include/EventAction.hh rename to QC-CaloGAN/generation/include/EventAction.hh diff --git a/CaloGAN/generation/include/PrimaryGeneratorAction.hh b/QC-CaloGAN/generation/include/PrimaryGeneratorAction.hh similarity index 100% rename from CaloGAN/generation/include/PrimaryGeneratorAction.hh rename to QC-CaloGAN/generation/include/PrimaryGeneratorAction.hh diff --git a/CaloGAN/generation/include/RunAction.hh b/QC-CaloGAN/generation/include/RunAction.hh similarity index 100% rename from CaloGAN/generation/include/RunAction.hh rename to QC-CaloGAN/generation/include/RunAction.hh diff --git a/CaloGAN/generation/include/RunData.hh b/QC-CaloGAN/generation/include/RunData.hh similarity index 100% rename from CaloGAN/generation/include/RunData.hh rename to QC-CaloGAN/generation/include/RunData.hh diff --git a/CaloGAN/generation/include/SteppingAction.hh b/QC-CaloGAN/generation/include/SteppingAction.hh similarity index 100% rename from CaloGAN/generation/include/SteppingAction.hh rename to QC-CaloGAN/generation/include/SteppingAction.hh diff --git a/CaloGAN/generation/launch b/QC-CaloGAN/generation/launch similarity index 100% rename from CaloGAN/generation/launch rename to QC-CaloGAN/generation/launch diff --git a/CaloGAN/generation/plot_1D_features.py b/QC-CaloGAN/generation/plot_1D_features.py similarity index 100% rename from CaloGAN/generation/plot_1D_features.py rename to QC-CaloGAN/generation/plot_1D_features.py diff --git a/CaloGAN/generation/src/ActionInitialization.cc b/QC-CaloGAN/generation/src/ActionInitialization.cc similarity index 100% rename from CaloGAN/generation/src/ActionInitialization.cc rename to QC-CaloGAN/generation/src/ActionInitialization.cc diff --git a/CaloGAN/generation/src/DetectorConstruction.cc b/QC-CaloGAN/generation/src/DetectorConstruction.cc similarity index 100% rename from CaloGAN/generation/src/DetectorConstruction.cc rename to QC-CaloGAN/generation/src/DetectorConstruction.cc diff --git a/CaloGAN/generation/src/EventAction.cc b/QC-CaloGAN/generation/src/EventAction.cc similarity index 100% rename from CaloGAN/generation/src/EventAction.cc rename to QC-CaloGAN/generation/src/EventAction.cc diff --git a/CaloGAN/generation/src/PrimaryGeneratorAction.cc b/QC-CaloGAN/generation/src/PrimaryGeneratorAction.cc similarity index 100% rename from CaloGAN/generation/src/PrimaryGeneratorAction.cc rename to QC-CaloGAN/generation/src/PrimaryGeneratorAction.cc diff --git a/CaloGAN/generation/src/RunAction.cc b/QC-CaloGAN/generation/src/RunAction.cc similarity index 100% rename from CaloGAN/generation/src/RunAction.cc rename to QC-CaloGAN/generation/src/RunAction.cc diff --git a/CaloGAN/generation/src/RunData.cc b/QC-CaloGAN/generation/src/RunData.cc similarity index 100% rename from CaloGAN/generation/src/RunData.cc rename to QC-CaloGAN/generation/src/RunData.cc diff --git a/CaloGAN/generation/src/SteppingAction.cc b/QC-CaloGAN/generation/src/SteppingAction.cc similarity index 100% rename from CaloGAN/generation/src/SteppingAction.cc rename to QC-CaloGAN/generation/src/SteppingAction.cc diff --git a/CaloGAN/models/__init__.py b/QC-CaloGAN/models/__init__.py similarity index 100% rename from CaloGAN/models/__init__.py rename to QC-CaloGAN/models/__init__.py diff --git a/CaloGAN/models/architectures.py b/QC-CaloGAN/models/architectures.py similarity index 100% rename from CaloGAN/models/architectures.py rename to QC-CaloGAN/models/architectures.py diff --git a/CaloGAN/models/ops.py b/QC-CaloGAN/models/ops.py similarity index 100% rename from CaloGAN/models/ops.py rename to QC-CaloGAN/models/ops.py diff --git a/CaloGAN/models/particles.yaml b/QC-CaloGAN/models/particles.yaml similarity index 100% rename from CaloGAN/models/particles.yaml rename to QC-CaloGAN/models/particles.yaml diff --git a/CaloGAN/models/qcbm.py b/QC-CaloGAN/models/qcbm.py similarity index 100% rename from CaloGAN/models/qcbm.py rename to QC-CaloGAN/models/qcbm.py diff --git a/CaloGAN/models/train.py b/QC-CaloGAN/models/train.py similarity index 100% rename from CaloGAN/models/train.py rename to QC-CaloGAN/models/train.py diff --git a/CaloGAN/models/train_cgan.py b/QC-CaloGAN/models/train_cgan.py similarity index 100% rename from CaloGAN/models/train_cgan.py rename to QC-CaloGAN/models/train_cgan.py From f9bad85b4e9591bef1d9c95c394b2156f1127ef4 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 20:05:28 -0800 Subject: [PATCH 5/7] WIP train QCBM with discrimnator weights per epochs --- QC-CaloGAN/models/qcbm.py | 4 ++++ QC-CaloGAN/models/train.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/QC-CaloGAN/models/qcbm.py b/QC-CaloGAN/models/qcbm.py index 60802c1..cf26b03 100644 --- a/QC-CaloGAN/models/qcbm.py +++ b/QC-CaloGAN/models/qcbm.py @@ -130,6 +130,10 @@ def initialize_weights(layers, num_wires): a[l].append([np.random.random()*np.pi*2 for _ in range(num_wires-1-i)]) return np.array(a) +def train_qcbm(true_prob, weights): + """Train the QCBM""" + return weights + ######################### ######################### if __name__ == "__main__": diff --git a/QC-CaloGAN/models/train.py b/QC-CaloGAN/models/train.py index 45aca29..64c310b 100644 --- a/QC-CaloGAN/models/train.py +++ b/QC-CaloGAN/models/train.py @@ -21,6 +21,7 @@ from sklearn.utils import shuffle import sys import yaml +import h5py if __name__ == '__main__': @@ -110,6 +111,7 @@ def get_parser(): parse_args = parser.parse_args() # delay the imports so running train.py -h doesn't take 5,234,807 years + import tensorflow as tf import tensorflow.keras.backend as K from tensorflow.keras.layers import (Activation, AveragePooling2D, Dense, Embedding, Flatten, Input, Lambda, UpSampling2D) @@ -124,7 +126,7 @@ def get_parser(): calculate_energy, scale, inpainting_attention) from architectures import build_generator, build_discriminator - from qcbm import qcbm_approx_probs, qcbm_probs, initialize_weights + from qcbm import qcbm_approx_probs, qcbm_probs, initialize_weights, train_qcbm # batch, latent size, and whether or not to be verbose with a progress bar @@ -295,7 +297,9 @@ def _load_data(particle, datafile): mbd_energy ]) - fake = Dense(1, activation='sigmoid', name='fakereal_output')(p) + qcbm_w = Dense(2**nb_qubits, activation='linear', name='qcbm')(p) + + fake = Dense(1, activation='sigmoid', name='fakereal_output')(qcbm_w) discriminator_outputs = [fake, total_energy] discriminator_losses = ['binary_crossentropy', 'mae'] # ACGAN case @@ -314,6 +318,18 @@ def _load_data(particle, datafile): discriminator = Model(calorimeter + [input_energy], discriminator_outputs) + tf.keras.utils.plot_model( + discriminator, + to_file="discriminator.png", + show_shapes=True, + show_dtype=False, + show_layer_names=True, + rankdir="TB", + expand_nested=False, + dpi=96, + ) + + discriminator.compile( optimizer=Adam(lr=disc_lr, beta_1=adam_beta_1), loss=discriminator_losses @@ -418,7 +434,10 @@ def _load_data(particle, datafile): noise = qcbm_approx_probs(qcbm_weights, nb_qubits) noise = np.array([i for i in noise.values()]) noise = np.concatenate((noise,np.zeros(latent_size-noise.size))) + logger.info(noise) + logger.info(noise.shape) noise = np.tile(noise, (batch_size, 1)) + logger.info(noise.shape) else: noise = np.random.normal(0, 1, (batch_size, latent_size)) @@ -512,3 +531,8 @@ def _load_data(particle, datafile): discriminator.save_weights('./weights/{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), overwrite=True) + + dis_weights_f = h5py.File('./weights/{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), 'r') + qcbm_dis_weights = dis_weights_f['qcbm']['qcbm']['kernel:0'][:].flatten() + qcbm_weights = train_qcbm(qcbm_dis_weights, qcbm_weights) + dis_weights_f.close() From 8f258cbd9c923fe08a3a16d64ca8b3e533956c10 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 20:14:28 -0800 Subject: [PATCH 6/7] copied from the main and lowered iterations --- QC-CaloGAN/models/qcbm.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/QC-CaloGAN/models/qcbm.py b/QC-CaloGAN/models/qcbm.py index cf26b03..a469775 100644 --- a/QC-CaloGAN/models/qcbm.py +++ b/QC-CaloGAN/models/qcbm.py @@ -132,6 +132,12 @@ def initialize_weights(layers, num_wires): def train_qcbm(true_prob, weights): """Train the QCBM""" + for i in range(1000): + weights = weights - 0.01* SPSA_grad(approx_cost_fn, weights) #cost using approx sample probabilities + #weights = weights - 0.01* exact_grad_cost(weights) #cost using exact sample probabilities + if i % 100 == 0: + #print("Approx Cost:", KL_Loss_dict(exact_prob_dict, qcbm_approx_probs(weights, num_wires))) + print("True Cost:", KL_Loss(exact_prob_dist, qcbm_probs(weights, num_wires))) return weights ######################### From a04660a596dae59093c9a386e6dd00bd605c13c9 Mon Sep 17 00:00:00 2001 From: TDHTTTT Date: Thu, 25 Feb 2021 20:39:24 -0800 Subject: [PATCH 7/7] should run now; but sometimes has invalid value in log error --- QC-CaloGAN/models/qcbm.py | 7 ++++++- QC-CaloGAN/models/train.py | 8 +++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/QC-CaloGAN/models/qcbm.py b/QC-CaloGAN/models/qcbm.py index a469775..baeb769 100644 --- a/QC-CaloGAN/models/qcbm.py +++ b/QC-CaloGAN/models/qcbm.py @@ -130,8 +130,13 @@ def initialize_weights(layers, num_wires): a[l].append([np.random.random()*np.pi*2 for _ in range(num_wires-1-i)]) return np.array(a) -def train_qcbm(true_prob, weights): + +def train_qcbm(exact_prob_dist, weights): """Train the QCBM""" + exact_prob_dict = {outcome:exact_prob_dist[outcome] for outcome in range(2**num_wires)} + def approx_cost_fn(weights): + return KL_Loss_dict(exact_prob_dict, qcbm_approx_probs(weights, num_wires)) + for i in range(1000): weights = weights - 0.01* SPSA_grad(approx_cost_fn, weights) #cost using approx sample probabilities #weights = weights - 0.01* exact_grad_cost(weights) #cost using exact sample probabilities diff --git a/QC-CaloGAN/models/train.py b/QC-CaloGAN/models/train.py index 64c310b..4b362d3 100644 --- a/QC-CaloGAN/models/train.py +++ b/QC-CaloGAN/models/train.py @@ -125,8 +125,9 @@ def get_parser(): from ops import (minibatch_discriminator, minibatch_output_shape, Dense3D, calculate_energy, scale, inpainting_attention) - from architectures import build_generator, build_discriminator - from qcbm import qcbm_approx_probs, qcbm_probs, initialize_weights, train_qcbm + from exp_architectures import build_generator, build_discriminator + from exp_qcbm import (qcbm_approx_probs, qcbm_probs, initialize_weights, + train_qcbm, SPSA_grad, KL_Loss) # batch, latent size, and whether or not to be verbose with a progress bar @@ -533,6 +534,7 @@ def _load_data(particle, datafile): overwrite=True) dis_weights_f = h5py.File('./weights/{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), 'r') - qcbm_dis_weights = dis_weights_f['qcbm']['qcbm']['kernel:0'][:].flatten() + qcbm_dis_weights = dis_weights_f['fakereal_output']['fakereal_output']['kernel:0'][:].flatten() + logger.info("discriminator qcbm weights ({}): {}".format(qcbm_dis_weights.shape,qcbm_dis_weights)) qcbm_weights = train_qcbm(qcbm_dis_weights, qcbm_weights) dis_weights_f.close()