Created
April 7, 2017 14:39
-
-
Save MartinThoma/f29b640caa237e413e05c98d8c334ddd to your computer and use it in GitHub Desktop.
Revisions
-
MartinThoma created this gist
Apr 7, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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)