Created
April 7, 2017 14:39
-
-
Save MartinThoma/f29b640caa237e413e05c98d8c334ddd 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 as np | |
from keras import backend as K | |
from keras.models import Sequential | |
from keras.layers import Dense, Activation | |
def fro_norm(w): | |
"""Frobenius norm.""" | |
return K.sqrt(K.sum(K.square(K.abs(w)))) | |
def ort_reg(w): | |
"""Orthogonal regularization.""" | |
m = K.dot(K.transpose(w), w) - K.eye(w.shape) | |
return fro_norm(m) | |
X = np.random.randn(100, 100) | |
y = np.random.randint(2, size=(100, 1)) | |
model = Sequential() | |
# apply regularization here. applies regularization to the | |
# output (activation) of the layer | |
model.add(Dense(32, input_shape=(100,), | |
activity_regularizer=ort_reg)) | |
model.add(Dense(1)) | |
model.add(Activation('softmax')) | |
model.compile(loss="binary_crossentropy", | |
optimizer='sgd', | |
metrics=['accuracy']) | |
model.fit(X, y, epochs=1, batch_size=32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment