Guest User

George Hotz 30 Aug 2018 programming live-stream

a guest
Sep 2nd, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # goal is to transform uniform(0,1) into N(-1,1)
  3.  
  4. import matplotlib.pyplot as plt
  5. import random
  6. import numpy as np
  7.  
  8. from keras import optimizers
  9. from keras.models import Sequential
  10. from keras.layers import Dense, Activation
  11.  
  12. # generator
  13. g = Sequential([
  14.   Dense(3, input_shape=(1,)),
  15.   Activation('elu'),
  16.   Dense(3),
  17.   Activation('elu'),
  18.   Dense(1),
  19. ])
  20.  
  21. # discriminator
  22. # output 1 for real, 0 for fake
  23. # nobody likes fake people
  24. d = Sequential([
  25.   Dense(3, input_shape=(1,)),
  26.   Activation('elu'),
  27.   Dense(3),
  28.   Activation('elu'),
  29.   Dense(1),
  30.   Activation('sigmoid'),
  31. ])
  32.  
  33. #sgd = optimizers.SGD(lr=0.01, momentum=0.0, nesterov=True)
  34. sgd = optimizers.SGD()
  35.  
  36. g.compile(optimizer=sgd, loss='mse')
  37. d.compile(optimizer=sgd, loss='mse')
  38.  
  39. for loop in range(10):
  40.   print("******* loop %d *******" % loop)
  41.   # train discriminator
  42.   d.trainable = True
  43.   X, Y = [], []
  44.  
  45.   Z = np.array([random.random() for x in range(16384)])
  46.   samples_fake = g.predict(Z).flatten()
  47.   samples_real = np.random.normal(-1.0, 1.0, 16384)
  48.  
  49.   plt.hist(samples_real, bins=np.arange(-3,3,0.1))
  50.   plt.hist(samples_fake, bins=np.arange(-3,3,0.1))
  51.   plt.show()
  52.  
  53.   X = list(samples_fake) + list(samples_real)
  54.   Y = [0]*samples_fake.shape[0] + [1]*samples_real.shape[0]
  55.  
  56.   # shuffle together
  57.   XY = list(zip(X,Y))
  58.   random.shuffle(XY)
  59.   X,Y = zip(*XY)
  60.   d.fit(np.array(X), np.array(Y), epochs=3)
  61.  
  62.   # train generator
  63.   d.trainable = False
  64.   Z = np.array([random.random() for x in range(16384)])
  65.   gd = Sequential([g, d])
  66.   gd.compile(optimizer=sgd, loss='mse')
  67.   gd.fit(Z, np.array([1]*Z.shape[0]), epochs=3)
Add Comment
Please, Sign In to add comment