Skip to content

Instantly share code, notes, and snippets.

@emuccino
Last active April 13, 2020 09:34
Show Gist options
  • Save emuccino/8bb443a942f959f57ebd1ed3d4fe0fba to your computer and use it in GitHub Desktop.
Save emuccino/8bb443a942f959f57ebd1ed3d4fe0fba to your computer and use it in GitHub Desktop.
Batch generating functions
from tensorflow.keras.utils import to_categorical
#function for generating latent samples for synthetic data for generator training
def generate_latent_samples(n):
#generate latent vectors with balanced targets
x = {'latent': np.random.normal(size=(n, latent_dim)),
'target': to_categorical(np.hstack([np.array([_x for _ in range(n//2)]) for _x in range(2)]),2)}
#outputs indicating postive discirmination (target value)
y = np.hstack([np.array([[0] for _ in range(n)]), x['target']])
return x, y
#function for generating synthetic samples for discriminator training
def generate_synthetic_samples(n):
latent_x, _ = generate_latent_samples(n)
#generate data with balanced targets
gen_predict = generator.predict(latent_x)
x = dict(zip(generator.output_names, gen_predict))
#outputs indicating negative discirmination
y = np.array([[1,0,0] for _ in range(n)])
return x, y
#function for generating real samples for discriminator training
def generate_real_samples(n):
#sample real data with balanced targets
samples = [np.random.choice(train_target_df[train_target_df[target] == _y].index, size=n//2) for _y in range(2)]
x = {name:np.vstack([train_df[name][sample].values.reshape(-1,1) for y,sample in enumerate(samples)]) for name in data}
for name,n_token in n_tokens.items():
x[name] = to_categorical(x[name], n_token)
#outputs indicating postive discirmination (target value)
y = np.hstack([np.array([[0] for _ in range(n)]),
to_categorical(np.hstack([train_target_df[target][sample].values for sample in samples]),2)])
return x, y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment