org.deeplearning4j.arbiter.conf.updater.schedule.SigmoidScheduleSpace Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.deeplearning4j.arbiter.conf.updater.schedule;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.deeplearning4j.arbiter.optimize.api.ParameterSpace;
import org.deeplearning4j.arbiter.optimize.parameter.FixedValue;
import org.nd4j.linalg.schedule.ISchedule;
import org.nd4j.linalg.schedule.PolySchedule;
import org.nd4j.linalg.schedule.ScheduleType;
import org.nd4j.linalg.schedule.SigmoidSchedule;
import org.nd4j.shade.jackson.annotation.JsonProperty;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@NoArgsConstructor //JSON
@Data
public class SigmoidScheduleSpace implements ParameterSpace {
private ScheduleType scheduleType;
private ParameterSpace initialValue;
private ParameterSpace gamma;
private ParameterSpace stepSize;
public SigmoidScheduleSpace(@NonNull ScheduleType scheduleType, @NonNull ParameterSpace initialValue,
double gamma, int stepSize){
this(scheduleType, initialValue, new FixedValue<>(gamma), new FixedValue<>(stepSize));
}
public SigmoidScheduleSpace(@NonNull @JsonProperty("scheduleType") ScheduleType scheduleType,
@NonNull @JsonProperty("initialValue") ParameterSpace initialValue,
@NonNull @JsonProperty("gamma") ParameterSpace gamma,
@NonNull @JsonProperty("stepSize") ParameterSpace stepSize){
this.scheduleType = scheduleType;
this.initialValue = initialValue;
this.gamma = gamma;
this.stepSize = stepSize;
}
@Override
public ISchedule getValue(double[] parameterValues) {
return new SigmoidSchedule(scheduleType, initialValue.getValue(parameterValues),
gamma.getValue(parameterValues), stepSize.getValue(parameterValues));
}
@Override
public int numParameters() {
return initialValue.numParameters() + gamma.numParameters() + stepSize.numParameters();
}
@Override
public List collectLeaves() {
return Arrays.asList(initialValue, gamma, stepSize);
}
@Override
public Map getNestedSpaces() {
Map out = new LinkedHashMap<>();
out.put("initialValue", initialValue);
out.put("gamma", gamma);
out.put("stepSize", stepSize);
return out;
}
@Override
public boolean isLeaf() {
return false;
}
@Override
public void setIndices(int... indices) {
if(initialValue.numParameters() > 0){
int[] sub = Arrays.copyOfRange(indices, 0, initialValue.numParameters());
initialValue.setIndices(sub);
}
if(gamma.numParameters() > 0){
int np = initialValue.numParameters();
int[] sub = Arrays.copyOfRange(indices, np, np + gamma.numParameters());
gamma.setIndices(sub);
}
if(stepSize.numParameters() > 0){
int np = initialValue.numParameters() + gamma.numParameters();
int[] sub = Arrays.copyOfRange(indices, np, np + stepSize.numParameters());
stepSize.setIndices(sub);
}
}
}