org.deeplearning4j.arbiter.layers.FeedForwardLayerSpace Maven / Gradle / Ivy
/*-
*
* * Copyright 2016 Skymind,Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package org.deeplearning4j.arbiter.layers;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.deeplearning4j.arbiter.optimize.api.ParameterSpace;
import org.deeplearning4j.arbiter.optimize.parameter.FixedValue;
import org.deeplearning4j.nn.api.layers.LayerConstraint;
import org.deeplearning4j.nn.conf.layers.FeedForwardLayer;
import java.util.Arrays;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor //For Jackson JSON/YAML deserialization
public abstract class FeedForwardLayerSpace extends BaseLayerSpace {
protected ParameterSpace nIn;
protected ParameterSpace nOut;
protected ParameterSpace> constrainWeights;
protected ParameterSpace> constrainBias;
protected ParameterSpace> constrainAll;
protected FeedForwardLayerSpace(Builder builder) {
super(builder);
nIn = builder.nIn;
nOut = builder.nOut;
constrainWeights = builder.constrainWeights;
constrainBias = builder.constrainBias;
constrainAll = builder.constrainAll;
}
protected void setLayerOptionsBuilder(FeedForwardLayer.Builder builder, double[] values) {
super.setLayerOptionsBuilder(builder, values);
if (nIn != null)
builder.nIn(nIn.getValue(values));
if (nOut != null)
builder.nOut(nOut.getValue(values));
if (constrainWeights != null){
List c = constrainWeights.getValue(values);
if(c != null){
builder.constrainWeights(c.toArray(new LayerConstraint[c.size()]));
}
}
if (constrainBias != null){
List c = constrainBias.getValue(values);
if(c != null){
builder.constrainBias(c.toArray(new LayerConstraint[c.size()]));
}
}
if (constrainAll != null){
List c = constrainAll.getValue(values);
if(c != null){
builder.constrainAllParameters(c.toArray(new LayerConstraint[c.size()]));
}
}
}
public abstract static class Builder extends BaseLayerSpace.Builder {
protected ParameterSpace nIn;
protected ParameterSpace nOut;
protected ParameterSpace> constrainWeights;
protected ParameterSpace> constrainBias;
protected ParameterSpace> constrainAll;
public T nIn(int nIn) {
return nIn(new FixedValue<>(nIn));
}
public T nIn(ParameterSpace nIn) {
this.nIn = nIn;
return (T) this;
}
public T nOut(int nOut) {
return nOut(new FixedValue<>(nOut));
}
public T nOut(ParameterSpace nOut) {
this.nOut = nOut;
return (T) this;
}
public T constrainWeights(LayerConstraint... constraints){
return constrainWeights(new FixedValue>(Arrays.asList(constraints)));
}
public T constrainWeights(ParameterSpace> constraints){
this.constrainWeights = constraints;
return (T) this;
}
public T constrainBias(LayerConstraint... constraints){
return constrainBias(new FixedValue>(Arrays.asList(constraints)));
}
public T constrainBias(ParameterSpace> constraints){
this.constrainBias = constraints;
return (T) this;
}
public T constrainAllParams(LayerConstraint... constraints){
return constrainAllParams(new FixedValue>(Arrays.asList(constraints)));
}
public T constrainAllParams(ParameterSpace> constraints){
this.constrainAll = constraints;
return (T) this;
}
}
@Override
public String toString() {
return toString(", ");
}
@Override
protected String toString(String delim) {
StringBuilder sb = new StringBuilder();
if (nIn != null)
sb.append("nIn: ").append(nIn).append(delim);
if (nOut != null)
sb.append("nOut: ").append(nOut).append(delim);
if (constrainWeights != null)
sb.append("constrainWeights: ").append(constrainWeights).append(delim);
if (constrainBias != null)
sb.append("constrainBias: ").append(constrainBias).append(delim);
if (constrainAll != null)
sb.append("constrainAllParams: ").append(constrainAll).append(delim);
sb.append(super.toString(delim));
return sb.toString();
}
}