Created
April 14, 2017 12:47
-
-
Save cympfh/17c10dc6d828917510650bef6040dcff to your computer and use it in GitHub Desktop.
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 | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.optimizers import Adam | |
from keras import backend as K | |
from visdom import Visdom | |
B = 1000 | |
N = 10 | |
X = numpy.random.rand(B, N) | |
model = Sequential() | |
model.add(Dense(N, input_shape=(N,))) | |
model.add(Dense(N)) | |
def kl(dummy, q): | |
m = K.mean(q, axis=0) # across the mini-batch | |
v = K.maximum(K.mean((q - m) ** 2, axis=0), K.epsilon()) | |
return 0.5 * (-N + K.sum(m ** 2 + v - K.log(v))) | |
opt = Adam(clipnorm=0.9) | |
model.compile(loss=kl, optimizer=opt) | |
model.fit(X, X, epochs=100) | |
# test & plot | |
Y = model.predict(X) | |
plot = Visdom() | |
plot.histogram(Y[:, 0], opts={'title': 'Y'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment