def train_single_gan(i, num_epochs, gan_params, metrics, gan_path, out_dtype): print(gan_params.loc[i]) np.random.seed(gan_params.loc[i, "seed"]) data, scaling_values = rescale_multivariate_data(generate_random_fields(gan_params.loc[i, "train_size"], gan_params.loc[i, "data_width"], gan_params.loc[i, "length_scale"])) print(scaling_values) scaling_values.to_csv(join(gan_path, "scaling_values_{0:04d}.csv".format(i)), index_label="Channel") batches_per_epoch = int(gan_params.loc[i, "train_size"] / gan_params.loc[i, "batch_size"]) batch_size = int(gan_params.loc[i, "batch_size"]) batch_diff = data.shape[0] % batch_size #batch_seeds = np.random.randint(0, 1000000, size=batches_per_epoch) if batch_diff > 0: data = data[:data.shape[0]-batch_diff] print("Making generator model") gen, vec_input = generator_model(input_size=int(gan_params.loc[i, "generator_input_size"]), filter_width=int(gan_params.loc[i, "filter_width"]), min_data_width=int(gan_params.loc[i, "min_data_width"]), min_conv_filters=int(gan_params.loc[i, "min_conv_filters"]), output_size=data.shape[1:], stride=2, activation=gan_params.loc[i, "activation"], dropout_alpha=float(gan_params.loc[i, "dropout_alpha"])) disc, enc, image_input = encoder_disc_model(input_size=data.shape[1:], filter_width=int(gan_params.loc[i, "filter_width"]), min_data_width=int(gan_params.loc[i, "min_data_width"]), min_conv_filters=int(gan_params.loc[i, "min_conv_filters"]), output_size=int(gan_params.loc[i, "generator_input_size"]), activation=gan_params.loc[i, "activation"], dropout_alpha=float(gan_params.loc[i, "dropout_alpha"])) optimizer = Adam(lr=gan_params.loc[i, "learning_rate"], beta_1=gan_params.loc[i, "beta_one"]) gen_model = Model(vec_input, gen) disc_model = Model(image_input, disc) enc_model = Model(image_input, enc) gen_model.compile(optimizer=optimizer, loss="mse") enc_model.compile(optimizer=optimizer, loss="mse") disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) gen_disc_model = stack_gen_disc(gen_model, disc_model) gen_disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) enc_gen_model = stack_enc_gen(enc_model, gen_model, disc_model) enc_gen_model.compile(optimizer=optimizer, loss="mse", metrics=["mse", "mae"]) print("gen model") print(gen_model.summary()) print("disc model") print(disc_model.summary()) print("gen disc model") print(gen_disc_model.summary()) print("enc gen model") print(enc_gen_model.summary()) train_linked_gan(data, gen_model, enc_model, disc_model, gen_disc_model, enc_gen_model, int(gan_params.loc[i, "generator_input_size"]), gan_path, i, batch_size=int(gan_params.loc[i, "batch_size"]), metrics=metrics, num_epochs=num_epochs, scaling_values=scaling_values, out_dtype=out_dtype) del data
def evaluate_gan_config(gpu_num, data_path, variable_names, num_epochs, gan_params, metrics, gan_path, out_dtype): """ Args: gpu_num: data_path: variable_names: num_epochs: gan_params: metrics: gan_path: out_dtype: Returns: """ try: os.environ["CUDA_VISIBLE_DEVICES"] = "{0:d}".format(gpu_num) print("Loading data {0}".format(gpu_num)) if "tsi" in data_path: data = load_tsi_data(data_path, variable_names) else: data = load_storm_patch_data(data_path, variable_names) for c in [2, 3]: data[:, :, :, c] = np.sqrt(data[:, :, :, c]) print("Rescaling data {0}".format(gpu_num)) scaled_data, scaling_values = rescale_multivariate_data(data) session = K.tf.Session(config=K.tf.ConfigProto(allow_soft_placement=True, gpu_options=K.tf.GPUOptions(allow_growth=True), log_device_placement=False)) K.set_session(session) with K.tf.device("/gpu:{0:d}".format(0)): for i in gan_params.index.values: print("Starting combo {0:d}".format(i)) scaling_values.to_csv(join(gan_path, "scaling_values_{0:03d}.csv".format(i)), index_label="Channel") print(gan_params.loc[i]) batch_size = int(gan_params.loc[i, "batch_size"]) batch_diff = scaled_data.shape[0] % batch_size gen, vec_input = generator_model(input_size=int(gan_params.loc[i, "generator_input_size"]), filter_width=int(gan_params.loc[i, "filter_width"]), min_data_width=int(gan_params.loc[i, "min_data_width"]), min_conv_filters=int(gan_params.loc[i, "min_conv_filters"]), output_size=scaled_data.shape[1:], stride=2) disc, enc, image_input = encoder_disc_model(input_size=scaled_data.shape[1:], filter_width=int(gan_params.loc[i, "filter_width"]), min_data_width=int(gan_params.loc[i, "min_data_width"]), min_conv_filters=int(gan_params.loc[i, "min_conv_filters"]), output_size=int(gan_params.loc[i, "generator_input_size"])) optimizer = Adam(lr=gan_params.loc[i, "learning_rate"], beta_1=gan_params.loc[i, "beta_one"]) #disc_optimizer = Adam(lr=2 * gan_params.loc[i, "learning_rate"], # beta_1=gan_params.loc[i, "beta_one"]) gen_model = Model(vec_input, gen) disc_model = Model(image_input, disc) enc_model = Model(image_input, enc) gen_model.compile(optimizer=optimizer, loss="mse") enc_model.compile(optimizer=optimizer, loss="mse") disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) print("gen model") print(gen_model.summary()) print("disc model") print(disc_model.summary()) gen_disc_model = stack_gen_disc(gen_model, disc_model) #gen_disc_model = Model(vec_input, gen_disc) gen_disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) enc_gen_model = stack_enc_gen(enc_model, gen_model, disc_model) #enc_gen_model = Model(image_input, enc_gen) enc_gen_model.compile(optimizer=optimizer, loss="mse", metrics=["mse", "mae"]) print("gen model") print(gen_model.summary()) print("disc model") print(disc_model.summary()) print("gen disc model") print(gen_disc_model.summary()) print("enc gen model") print(enc_gen_model.summary()) history = train_linked_gan(scaled_data[:-batch_diff], gen_model, enc_model, disc_model, gen_disc_model, enc_gen_model, int(gan_params.loc[i, "generator_input_size"]), gan_path, i, batch_size=int(gan_params.loc[i, "batch_size"]), metrics=metrics, num_epochs=num_epochs, scaling_values=scaling_values, out_dtype=out_dtype) history.to_csv(join(gan_path, "gan_loss_history_{0:03d}.csv".format(i)), index_label="Time") except Exception as e: print(traceback.format_exc()) raise e return
def train_single_gan(gan_index, num_epochs, gan_params, metrics, gan_path): print(gan_params.loc[gan_index]) np.random.seed(gan_params.loc[gan_index, "seed"]) data, scaling_values = normalize_multivariate_data( generate_random_fields(gan_params.loc[gan_index, "train_size"], gan_params.loc[gan_index, "data_width"], gan_params.loc[gan_index, "length_scale"])) scaling_values.to_csv(join(gan_path, "scaling_values_{0:04d}.csv".format(gan_index)), index_label="Channel") batch_size = int(gan_params.loc[gan_index, "batch_size"]) batch_diff = data.shape[0] % batch_size if batch_diff > 0: data = data[:data.shape[0] - batch_diff] print("create gan models") gen_model = generator_model( input_size=int(gan_params.loc[gan_index, "generator_input_size"]), filter_width=int(gan_params.loc[gan_index, "filter_width"]), min_data_width=int(gan_params.loc[gan_index, "min_data_width"]), min_conv_filters=int(gan_params.loc[gan_index, "min_conv_filters"]), output_size=data.shape[1:], stride=int(gan_params.loc[gan_index, "stride"]), activation=gan_params.loc[gan_index, "activation"], dropout_alpha=float(gan_params.loc[gan_index, "dropout_alpha"])) disc_model = discriminator_model( input_size=data.shape[1:], filter_width=int(gan_params.loc[gan_index, "filter_width"]), min_data_width=int(gan_params.loc[gan_index, "min_data_width"]), min_conv_filters=int(gan_params.loc[gan_index, "min_conv_filters"]), activation=gan_params.loc[gan_index, "activation"], stride=int(gan_params.loc[gan_index, "stride"]), dropout_alpha=float(gan_params.loc[gan_index, "dropout_alpha"])) ind_enc_model = encoder_model( input_size=data.shape[1:], filter_width=int(gan_params.loc[gan_index, "filter_width"]), min_data_width=int(gan_params.loc[gan_index, "min_data_width"]), min_conv_filters=int(gan_params.loc[gan_index, "min_conv_filters"]), output_size=int(gan_params.loc[gan_index, "generator_input_size"]), activation=gan_params.loc[gan_index, "activation"], stride=int(gan_params.loc[gan_index, "stride"]), dropout_alpha=float(gan_params.loc[gan_index, "dropout_alpha"])) optimizer = Adam(lr=gan_params.loc[gan_index, "learning_rate"], beta_1=gan_params.loc[gan_index, "beta_one"]) gen_model.compile(optimizer=optimizer, loss="mse") ind_enc_model.compile(optimizer=optimizer, loss="mse") disc_model.compile(optimizer=optimizer, loss="binary_crossentropy") ind_enc_model.compile(optimizer=optimizer, loss="mse") gen_disc_model = stack_gen_disc(gen_model, disc_model) gen_disc_model.compile(optimizer=optimizer, loss="binary_crossentropy") gen_enc_model = stack_gen_enc(gen_model, ind_enc_model) gen_enc_model.compile(optimizer=optimizer, loss="mse", metrics=["mse", "mae"]) print("gen model") print(gen_model.summary()) print("disc model") print(disc_model.summary()) print("gen disc model") print(gen_disc_model.summary()) print("enc gen model") print(gen_enc_model.summary()) history = train_gan_quiet( data, gen_model, disc_model, ind_enc_model, gen_disc_model, gen_enc_model, int(gan_params.loc[gan_index, "generator_input_size"]), int(gan_params.loc[gan_index, "batch_size"]), num_epochs, gan_index, gan_path) history.to_csv(join(gan_path, "gan_history_{0:04d}.csv".format(gan_index)), index_label="Index") del data
def __init__(self, data_width=32, num_input_channels=15, filter_width=5, min_conv_filters=16, min_data_width=4, encoding_channels=100, activation="relu", use_dropout=False, dropout_alpha=0, output_activation="linear", use_noise=False, noise_sd=0.1, pooling="mean", stride=2, num_epochs=10, batch_size=128, learning_rate=0.001, beta_one=0.5, index=0, penalty="l1", C=0.01): self.data_width = data_width self.index = index self.num_input_channels = num_input_channels self.filter_width = filter_width self.min_conv_filters = min_conv_filters self.min_data_width = min_data_width self.encoding_channels = encoding_channels self.activation = activation self.use_dropout = use_dropout self.dropout_alpha = dropout_alpha self.use_noise = use_noise self.noise_sd = noise_sd self.pooling = pooling self.output_activation = output_activation self.stride = stride self.num_epochs = num_epochs self.batch_size = batch_size self.learning_rate = learning_rate self.beta_one = beta_one self.penalty = penalty self.C = C self.gen, self.gen_input = generator_model(input_size=self.encoding_channels, filter_width=self.filter_width, min_data_width=self.min_data_width, min_conv_filters=min_conv_filters, output_size=(self.data_width, self.data_width, self.num_input_channels), stride=self.stride, activation=self.activation, output_activation=self.output_activation, use_dropout=self.use_dropout, dropout_alpha=self.dropout_alpha, use_noise=self.use_noise, noise_sd=self.noise_sd) self.disc, self.disc_input = discriminator_model(input_size=(self.data_width, self.data_width, self.num_input_channels), filter_width=self.filter_width, min_data_width=self.min_data_width, min_conv_filters=self.min_conv_filters, stride=self.stride, activation=self.activation, use_dropout=self.use_dropout, dropout_alpha=self.dropout_alpha, use_noise=self.use_noise, noise_sd=self.noise_sd, pooling=self.pooling) self.enc, self.enc_input = encoder_model(input_size=(self.data_width, self.data_width, self.num_input_channels), filter_width=self.filter_width, min_data_width=self.min_data_width, min_conv_filters=self.min_conv_filters, output_size=self.encoding_channels, stride=self.stride, activation=self.activation, use_dropout=self.use_dropout, dropout_alpha=self.dropout_alpha, use_noise=self.use_noise, noise_sd=self.noise_sd, pooling=self.pooling) optimizer = Adam(lr=self.learning_rate, beta_1=self.beta_one, clipnorm=1.) self.discriminator = Model(self.disc_input, self.disc) self.discriminator.compile(optimizer=optimizer, loss="binary_crossentropy") self.generator = Model(self.gen_input, self.gen) self.generator.compile(optimizer=optimizer, loss="mse") self.gen_disc = stack_gen_disc(self.generator, self.discriminator) self.gen_disc.compile(optimizer=optimizer, loss="binary_crossentropy") self.encoder = Model(self.enc_input, self.enc) self.encoder.compile(optimizer=optimizer, loss="mse") self.gen_enc = stack_gen_enc(self.generator, self.encoder) self.gen_enc.compile(optimizer=optimizer, loss="mse") print("Generator") print(self.generator.summary()) print("Discriminator") print(self.discriminator.summary()) print("Encoder") print(self.encoder.summary()) print("Gen Disc") print(self.gen_disc.summary()) print("Gen Enc") print(self.gen_enc.summary()) self.logistic = LogisticRegression(penalty=self.penalty, C=self.C, solver="saga", verbose=1) return
def main(): ua_vars = [ 'geopotential_height_500_mb_prev', 'geopotential_height_700_mb_prev', 'geopotential_height_850_mb_prev', 'temperature_500_mb_prev', 'temperature_700_mb_prev', 'temperature_850_mb_prev', 'dew_point_temperature_500_mb_prev', 'dew_point_temperature_700_mb_prev', 'dew_point_temperature_850_mb_prev', 'u-component_of_wind_500_mb_prev', 'u-component_of_wind_700_mb_prev', 'u-component_of_wind_850_mb_prev', 'v-component_of_wind_500_mb_prev', 'v-component_of_wind_700_mb_prev', 'v-component_of_wind_850_mb_prev' ] ua_data, ua_centers, ua_dates = load_storm_patch_data( "/scratch/dgagne/ncar_ens_storm_patches/", ua_vars) ua_norm, ua_scaling = normalize_multivariate_data(ua_data) train_indices = np.load("/scratch/dgagne/storm_ua_gan/train_indices.npy") test_indices = np.load("/scratch/dgagne/storm_ua_gan/test_indices.npy") batch_size = 32 batch_diff = train_indices.shape[0] % batch_size session = K.tf.Session(config=K.tf.ConfigProto(allow_soft_placement=True, gpu_options=K.tf.GPUOptions( allow_growth=True), log_device_placement=False)) K.set_session(session) with K.tf.device("/gpu:{0:d}".format(0)): metrics = ["accuracy"] num_epochs = [1, 5, 10] gen, vec_input = generator_model(input_size=64, min_conv_filters=32, min_data_width=4, filter_width=3, output_size=(32, 32, 15), activation="selu") disc, enc, image_input = encoder_disc_model(input_size=(32, 32, 15), min_conv_filters=32, output_size=64, min_data_width=4, filter_width=3, activation="selu") optimizer = Adam(lr=0.0001, beta_1=0.5) gen_model = Model(vec_input, gen) disc_model = Model(image_input, disc) enc_model = Model(image_input, enc) gen_model.compile(optimizer=optimizer, loss="mse") enc_model.compile(optimizer=optimizer, loss="mse") disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) gen_disc_model = stack_gen_disc(gen_model, disc_model) gen_disc_model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=metrics) gen_enc_model = stack_gen_enc(gen_model, enc_model, disc_model) gen_enc_model.compile(optimizer=optimizer, loss="mse", metrics=["mse", "mae"]) print("gen model") print(gen_model.summary()) print("disc model") print(disc_model.summary()) print("gen disc model") print(gen_disc_model.summary()) print("enc gen model") print(gen_enc_model.summary()) gan_path = "/scratch/dgagne/storm_ua_gan/" train_linked_gan(ua_norm[train_indices[:train_indices.shape[0] - batch_diff]], gen_model, enc_model, disc_model, gen_disc_model, gen_enc_model, 64, gan_path, 0, batch_size=batch_size, metrics=metrics, num_epochs=num_epochs, scaling_values=ua_scaling, out_dtype=np.float32, ind_encoder=None) session.close()