Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created April 7, 2017 14:39
Show Gist options
  • Select an option

  • Save MartinThoma/f29b640caa237e413e05c98d8c334ddd to your computer and use it in GitHub Desktop.

Select an option

Save MartinThoma/f29b640caa237e413e05c98d8c334ddd to your computer and use it in GitHub Desktop.

Revisions

  1. MartinThoma created this gist Apr 7, 2017.
    34 changes: 34 additions & 0 deletions custreg.py
    Original 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)