Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # goal is to transform uniform(0,1) into N(-1,1)
- import matplotlib.pyplot as plt
- import random
- import numpy as np
- from keras import optimizers
- from keras.models import Sequential
- from keras.layers import Dense, Activation
- # generator
- g = Sequential([
- Dense(3, input_shape=(1,)),
- Activation('elu'),
- Dense(3),
- Activation('elu'),
- Dense(1),
- ])
- # discriminator
- # output 1 for real, 0 for fake
- # nobody likes fake people
- d = Sequential([
- Dense(3, input_shape=(1,)),
- Activation('elu'),
- Dense(3),
- Activation('elu'),
- Dense(1),
- Activation('sigmoid'),
- ])
- #sgd = optimizers.SGD(lr=0.01, momentum=0.0, nesterov=True)
- sgd = optimizers.SGD()
- g.compile(optimizer=sgd, loss='mse')
- d.compile(optimizer=sgd, loss='mse')
- for loop in range(10):
- print("******* loop %d *******" % loop)
- # train discriminator
- d.trainable = True
- X, Y = [], []
- Z = np.array([random.random() for x in range(16384)])
- samples_fake = g.predict(Z).flatten()
- samples_real = np.random.normal(-1.0, 1.0, 16384)
- plt.hist(samples_real, bins=np.arange(-3,3,0.1))
- plt.hist(samples_fake, bins=np.arange(-3,3,0.1))
- plt.show()
- X = list(samples_fake) + list(samples_real)
- Y = [0]*samples_fake.shape[0] + [1]*samples_real.shape[0]
- # shuffle together
- XY = list(zip(X,Y))
- random.shuffle(XY)
- X,Y = zip(*XY)
- d.fit(np.array(X), np.array(Y), epochs=3)
- # train generator
- d.trainable = False
- Z = np.array([random.random() for x in range(16384)])
- gd = Sequential([g, d])
- gd.compile(optimizer=sgd, loss='mse')
- gd.fit(Z, np.array([1]*Z.shape[0]), epochs=3)
Add Comment
Please, Sign In to add comment