Skip to content
Permalink
334d4687ee
Go to file
 
 
Cannot retrieve contributors at this time
183 lines (152 sloc) 7.04 KB
# Import
import os
# Data Mangement
import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.utils import resample
# import matplotlib.pyplot as plt
# Nuerual Net Building
from keras import layers, initializers, regularizers
from keras.layers import Input, Dense, ActivityRegularization, InputLayer, Reshape, BatchNormalization, Flatten
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.utils.generic_utils import Progbar
# Third party keras gan tool
from keras_adversarial import AdversarialModel, simple_gan, gan_targets
from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling
#################################################
# Constants
#################################################
#################################################
# Functions
#################################################
def permute_sample(dataset, new_dataset_num_rows, as_data_frame=False):
"""
Given a 2-D pandas dataframe it will create a permutaiton
of each column.
"""
new_dataset = resample(dataset.iloc[:, 0],
n_samples=new_dataset_num_rows).reshape(new_dataset_num_rows, 1)
for col_index in range(1, dataset.shape[1]):
new_col = resample(dataset.iloc[:, col_index],
n_samples=new_dataset_num_rows).reshape(new_dataset_num_rows, 1)
new_dataset = np.append(new_dataset, new_col, 1)
return pd.DataFrame(new_dataset) if as_data_frame else new_dataset
def make_3d_dataset(dataset, new_dataset_num_rows, depth):
"""
Takes a 2 dimensional dataframe and makes it into a
3 dimensional dataframe by creating more 2D dataframes
and putting them together. New dataframes are made by
resampling (with replacement) the columns of the original dataset.
"""
height, width = dataset.shape
new_dataset = permute_sample(dataset, new_dataset_num_rows).reshape(new_dataset_num_rows, width, 1)
for _ in range(depth-1):
new_layer = permute_sample(dataset, new_dataset_num_rows).reshape(new_dataset_num_rows, width, 1)
new_dataset = np.append(new_dataset, new_layer, 2)
return new_dataset
#################################################
# Load data
#################################################
base_path = "../mkdataset/datasets/gan_datasets/"
# label_file_name = "tham_human_and_mouse_dataset.csv"
file_name = "tham_lasso_dataset.csv"
all_data = pd.read_csv(os.path.join(base_path, file_name))
#---------------------------------------
# Prepare Data
#---------------------------------------
just_values = all_data.iloc[1:, 3:]
x_train = make_3d_dataset(just_values, 300, 100)
#################################################
# Variables
#################################################
# Output
output_base_path = './'
#---------------------------------------
# Network Variables
#---------------------------------------
# Layer Input Output Shapes
gen_input_shape = (x_train.shape[1], x_train.shape[2])
discr_input_shape = (x_train.shape[1], x_train.shape[2])
gen_output_shape = discr_input_shape
# Generator Variables
weight_initializer = initializers.RandomNormal(mean=0.0, stddev=0.05, seed=None)
bias_initializer = initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None)
generator_regularizer = regularizers.l1(0.000000005)
descriminator_regularizer = regularizers.l2(0.0000000005)
# Training Varaibles
epochs = 2
batch_size = 1 # x_train.shape[0]
input_size = x_train.shape[1] * x_train.shape[2] * batch_size
# Build Generative model
generative_model = Sequential()
# generative_model.add(InputLayer(input_shape=gen_input_shape))
generative_model.add(InputLayer(input_shape=discr_input_shape))
generative_model.add(Flatten())
generative_model.add(Dense(units=int(1.5*input_size),
use_bias=True,
activation='relu',
kernel_initializer=weight_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=generator_regularizer))
# generative_model.add(ActivityRegularization(l1=0.02))
generative_model.add(Dense(units=int(input_size),
activation='relu',
kernel_initializer=weight_initializer,
kernel_regularizer=generator_regularizer))
generative_model.add(Reshape(discr_input_shape))
# Build Discriminator model
discriminator_model = Sequential()
discriminator_model.add(InputLayer(input_shape=discr_input_shape))
discriminator_model.add(BatchNormalization(axis=1))
discriminator_model.add(Flatten())
discriminator_model.add(Dense(units=int(1.5*input_size),
use_bias=True,
activation='relu',
kernel_initializer=weight_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=descriminator_regularizer))
# discriminator_model.add(ActivityRegularization(l2=0.02))
discriminator_model.add(Dense(units=int(0.2*input_size),
activation='relu',
kernel_initializer=weight_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=descriminator_regularizer))
discriminator_model.add(Dense(units=1, activation='sigmoid'))
# Build GAN
gan = simple_gan(generative_model, discriminator_model, normal_latent_sampling(gen_input_shape))
model = AdversarialModel(base_model=gan,
player_params=[generative_model.trainable_weights,
discriminator_model.trainable_weights],
player_names=['generator', 'discriminator'])
# Other optimizer to try AdversarialOptimizerAlternating
model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(),
player_optimizers=['adam', 'adam'], loss='binary_crossentropy')
# Print Summary of Models
generative_model.summary()
discriminator_model.summary()
gan.summary()
# Train
# gan_targets takes as inputs the # of samples
training_record = model.fit(x=x_train,
y=gan_targets(x_train.shape[0]),
epochs=epochs,
batch_size=batch_size)
# Diplay plot of loss over training
# plt.plot(history.history['player_0_loss'])
# plt.plot(history.history['player_1_loss'])
# plt.plot(history.history['loss'])
# Predict (i.e. produce new samples)
zsamples = np.random.normal(size=(1, x_train.shape[1], x_train.shape[2]))
pred = generative_model.predict(zsamples)
print(pred)
# Save new samples to file
new_samples = pd.DataFrame(pred[0,:,:])
new_samples.to_csv(os.path.join(output_base_path, 'new_samples.csv'))
# save training_record
df = pd.DataFrame(training_record.history)
df.to_csv(os.path.join(output_base_path, 'training_record.csv'))
# save models
generative_model.save(os.path.join(output_base_path, 'generator.h5'))
discriminator_model.save(os.path.join(output_base_path, "discriminator.h5"))
You can’t perform that action at this time.