-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QC-AAN #8
Merged
QC-AAN #8
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7adabd
smaller input size
TDHTTTT 51a23f2
sample from QCBM for prior
TDHTTTT 491f5d8
move qcbm to model
TDHTTTT 1b15141
change name to QC-CaloGAN
TDHTTTT f9bad85
WIP train QCBM with discrimnator weights per epochs
TDHTTTT 8f258cb
copied from the main and lowered iterations
TDHTTTT a04660a
should run now; but sometimes has invalid value in log error
TDHTTTT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from sklearn.utils import shuffle | ||
import sys | ||
import yaml | ||
import h5py | ||
|
||
|
||
if __name__ == '__main__': | ||
|
@@ -56,7 +57,19 @@ 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 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') | ||
|
||
parser.add_argument('--disc-lr', action='store', type=float, default=2e-5, | ||
help='Adam learning rate for discriminator') | ||
|
@@ -98,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) | ||
|
@@ -111,7 +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 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 | ||
|
||
|
@@ -130,6 +146,10 @@ 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 | ||
|
||
|
@@ -139,11 +159,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 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)) | ||
|
@@ -170,11 +194,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], | ||
|
@@ -273,7 +298,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 | ||
|
@@ -292,6 +319,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 | ||
|
@@ -370,6 +409,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)) | ||
|
||
|
@@ -389,8 +429,18 @@ 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))) | ||
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)) | ||
|
||
# get a batch of real images | ||
image_batch_1 = first[index * batch_size:(index + 1) * batch_size] | ||
|
@@ -477,8 +527,14 @@ 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) | ||
|
||
dis_weights_f = h5py.File('./weights/{0}{1:03d}.hdf5'.format(parse_args.d_pfx, epoch), 'r') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I basically read the weight from that layer and passed it to QCBM |
||
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() |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added another layer to align our QCBM dimenstion