Created
October 1, 2017 18:46
-
-
Save anonymous/d0cb46b439fd01d59d27fc0bef50ec49 to your computer and use it in GitHub Desktop.
DCGAN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.optimizers import Adam | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
X_train.shape | |
n = len(X_train) | |
X_train = X_train.reshape(n, -1).astype(np.float32) | |
X_test = X_test.reshape(len(X_test), -1).astype(np.float32) | |
X_train /= 255.; X_test /= 255. | |
np.random.seed(100) | |
def noise(bs): return np.random.rand(bs,100) | |
def data_D(sz, G): | |
real_img = X_train[np.random.randint(0,n,size=sz)] | |
X = np.concatenate((real_img, G.predict(noise(sz)))) | |
return X, [0]*sz + [1]*sz | |
def make_trainable(net, val): | |
net.trainable = val | |
for l in net.layers: l.trainable = val | |
def weight_difference(listofweights1, listofweights2): | |
"""Returns the max of elementwise difference between two matrices""" | |
retval = [] | |
for i in range(len(listofweights1)): | |
diff = np.abs(listofweights1[i] - listofweights2[i]) | |
retval.append(np.max(diff)) | |
return retval | |
def train(D, G, m, nb_epoch=100, bs=128): | |
dl,gl=[],[] | |
for e in range(nb_epoch): | |
X,y = data_D(bs//2, G) | |
dl.append(D.train_on_batch(X,y)) | |
make_trainable(D, False) | |
weights = D.get_weights() | |
gl.append(m.train_on_batch(noise(bs), np.zeros([bs]))) | |
weights_after = D.get_weights() | |
equals = np.array_equal(weights, weights_after) | |
wd = weight_difference(weights, weights_after) | |
norm_weights = sum(map(np.linalg.norm, weights)) | |
norm_weights_after = sum( map(np.linalg.norm, weights_after)) | |
print("Epoch:{} Equals:{} Weights:{} Weights_after:{} Norm Difference: {} Wd:{} ".format(e, equals,weights[0].reshape(-1)[0], weights_after[0].reshape(-1)[0], norm_weights-norm_weights_after, wd)) | |
make_trainable(D, True) | |
return dl,gl | |
MLP_G = Sequential([ | |
Dense(200, input_shape=(100,), activation='relu'), | |
Dense(400, activation='relu'), | |
Dense(784, activation='sigmoid'), | |
]) | |
MLP_D = Sequential([ | |
Dense(300, input_shape=(784,), activation='relu'), | |
Dense(300, activation='relu'), | |
Dense(1, activation='sigmoid'), | |
]) | |
MLP_D.compile(Adam(1e-4), "binary_crossentropy") | |
MLP_m = Sequential([MLP_G,MLP_D]) | |
MLP_m.compile(Adam(1e-4), "binary_crossentropy") | |
dl,gl = train(MLP_D, MLP_G, MLP_m, 100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment