Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created April 7, 2017 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/f29b640caa237e413e05c98d8c334ddd to your computer and use it in GitHub Desktop.
Save MartinThoma/f29b640caa237e413e05c98d8c334ddd to your computer and use it in GitHub Desktop.
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