New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New functionality (epoch tracking and constraints) #3957
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4aaef0c
Add epoch counter functionality + tests
AlexDBlack 8c43aa5
Epoch count functionality + tests for Spark
AlexDBlack 00e1f6b
Constraints API + implementations
AlexDBlack f8c4040
Constraints: configuration, plug into optimizers
AlexDBlack 5236fcc
Fixes and test
AlexDBlack af3bd13
Fix constraint implementations
AlexDBlack 07915f7
Javadoc
AlexDBlack 94972d4
Test + fix JSON ser/de for constraints
AlexDBlack 6715b01
Typos and unnecessary arg removal
AlexDBlack File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
102 changes: 102 additions & 0 deletions
102
...learning4j-core/src/test/java/org/deeplearning4j/nn/conf/constraints/TestConstraints.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.deeplearning4j.nn.conf.constraints; | ||
|
||
import org.deeplearning4j.nn.api.layers.LayerConstraint; | ||
import org.deeplearning4j.nn.conf.MultiLayerConfiguration; | ||
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; | ||
import org.deeplearning4j.nn.conf.constraint.MaxNormConstraint; | ||
import org.deeplearning4j.nn.conf.constraint.MinMaxNormConstraint; | ||
import org.deeplearning4j.nn.conf.constraint.NonNegativeConstraint; | ||
import org.deeplearning4j.nn.conf.constraint.UnitNormConstraint; | ||
import org.deeplearning4j.nn.conf.distribution.NormalDistribution; | ||
import org.deeplearning4j.nn.conf.layers.DenseLayer; | ||
import org.deeplearning4j.nn.conf.layers.OutputLayer; | ||
import org.deeplearning4j.nn.graph.ComputationGraph; | ||
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; | ||
import org.deeplearning4j.nn.weights.WeightInit; | ||
import org.deeplearning4j.util.ModelSerializer; | ||
import org.junit.Test; | ||
import org.nd4j.linalg.api.ndarray.INDArray; | ||
import org.nd4j.linalg.factory.Nd4j; | ||
import org.nd4j.linalg.lossfunctions.LossFunctions; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class TestConstraints { | ||
|
||
@Test | ||
public void testConstraints() throws Exception { | ||
|
||
|
||
LayerConstraint[] constraints = new LayerConstraint[]{ | ||
new MaxNormConstraint(0.5, 1), | ||
new MinMaxNormConstraint(0.3, 0.4, 1), | ||
new NonNegativeConstraint(), | ||
new UnitNormConstraint(1) | ||
}; | ||
|
||
for(LayerConstraint lc : constraints){ | ||
|
||
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() | ||
.constraints(lc) | ||
.learningRate(0.0) | ||
.weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0,5)) | ||
.list() | ||
.layer(new DenseLayer.Builder().nIn(12).nOut(10).build()) | ||
.layer(new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nIn(10).nOut(8).build()) | ||
.build(); | ||
|
||
MultiLayerNetwork net = new MultiLayerNetwork(conf); | ||
net.init(); | ||
|
||
List<LayerConstraint> exp = Collections.singletonList(lc.clone()); | ||
assertEquals(exp, net.getLayer(0).conf().getLayer().getConstraints()); | ||
assertEquals(exp, net.getLayer(1).conf().getLayer().getConstraints()); | ||
|
||
INDArray input = Nd4j.rand(3, 12); | ||
INDArray labels = Nd4j.rand(3, 8); | ||
|
||
net.fit(input, labels); | ||
|
||
INDArray w0 = net.getParam("0_W"); | ||
INDArray b0 = net.getParam("0_b"); | ||
INDArray w1 = net.getParam("1_W"); | ||
INDArray b1 = net.getParam("1_b"); | ||
|
||
if(lc instanceof MaxNormConstraint){ | ||
assertTrue(w0.norm2(1).maxNumber().doubleValue() <= 0.5 ); | ||
assertTrue(w1.norm2(1).maxNumber().doubleValue() <= 0.5 ); | ||
} else if(lc instanceof MinMaxNormConstraint){ | ||
assertTrue(w0.norm2(1).minNumber().doubleValue() >= 0.3 ); | ||
assertTrue(w0.norm2(1).maxNumber().doubleValue() <= 0.4 ); | ||
assertTrue(w1.norm2(1).minNumber().doubleValue() >= 0.3 ); | ||
assertTrue(w1.norm2(1).maxNumber().doubleValue() <= 0.4 ); | ||
} else if(lc instanceof NonNegativeConstraint ){ | ||
assertTrue(w0.minNumber().doubleValue() >= 0.0 ); | ||
} else if(lc instanceof UnitNormConstraint ){ | ||
assertEquals(w0.norm2(1).minNumber().doubleValue(), 1.0, 1e-6 ); | ||
assertEquals(w0.norm2(1).maxNumber().doubleValue(), 1.0, 1e-6 ); | ||
assertEquals(w1.norm2(1).minNumber().doubleValue(), 1.0, 1e-6 ); | ||
assertEquals(w1.norm2(1).maxNumber().doubleValue(), 1.0, 1e-6 ); | ||
} | ||
|
||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
ModelSerializer.writeModel(net, baos, true); | ||
byte[] bytes = baos.toByteArray(); | ||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); | ||
MultiLayerNetwork restored = ModelSerializer.restoreMultiLayerNetwork(bais, true); | ||
|
||
assertEquals(net.getLayerWiseConfigurations(), restored.getLayerWiseConfigurations()); | ||
assertEquals(net.params(), restored.params()); | ||
} | ||
|
||
} | ||
|
||
} |
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
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
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
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
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
15 changes: 15 additions & 0 deletions
15
deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/api/layers/LayerConstraint.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.deeplearning4j.nn.api.layers; | ||
|
||
import org.deeplearning4j.nn.api.Layer; | ||
import org.nd4j.shade.jackson.annotation.JsonTypeInfo; | ||
|
||
import java.io.Serializable; | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") | ||
public interface LayerConstraint extends Cloneable, Serializable { | ||
|
||
void applyConstraint(Layer layer, int iteration, int epoch); | ||
|
||
LayerConstraint clone(); | ||
|
||
} |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see where the call would come in the optimizer, but again it seems dangerous that this could be applied less than once (or more than once, depending on the constraint) per pass. Now that we're tracking epochs, could we have some epoch checking to make sure two successive calls are idempotent and less than one per epoch log a warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I'll be implementing it is that it'll get called once per iteration, after updates have been applied - it won't be possible to apply it more than once per iteration (and, even if it does: all of the implementations will give the same result with multiple sequential appliacions.
The epoch (and, iteration) number is just informational, to allow flexibility for users to have behaviour that depends on the number of epochs passed.