All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deeplearning4j.arbiter.conf.updater.AdamSpace Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta7
Show newest version
/*******************************************************************************
 * 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;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.deeplearning4j.arbiter.optimize.api.ParameterSpace;
import org.nd4j.linalg.learning.config.Adam;
import org.nd4j.linalg.learning.config.IUpdater;
import org.nd4j.linalg.schedule.ISchedule;
import org.nd4j.shade.jackson.annotation.JsonProperty;

@Data
@EqualsAndHashCode(callSuper = false)
public class AdamSpace extends BaseUpdaterSpace {

    private ParameterSpace learningRate;
    private ParameterSpace learningRateSchedule;
    private ParameterSpace beta1;
    private ParameterSpace beta2;
    private ParameterSpace epsilon;

    public AdamSpace(ParameterSpace learningRate) {
        this(learningRate, null, null, null);
    }

    public AdamSpace(ParameterSpace learningRate, ParameterSpace beta1,
                     ParameterSpace beta2, ParameterSpace epsilon) {
        this(learningRate, null, beta1, beta2, epsilon);
    }

    public static AdamSpace withLR(ParameterSpace lr){
        return new AdamSpace(lr, null, null, null, null);
    }

    public static AdamSpace withLRSchedule(ParameterSpace lrSchedule){
        return new AdamSpace(null, lrSchedule, null, null, null);
    }

    protected AdamSpace(@JsonProperty("learningRate") ParameterSpace learningRate,
                     @JsonProperty("learningRateSchedule") ParameterSpace learningRateSchedule,
                     @JsonProperty("beta1") ParameterSpace beta1,
                     @JsonProperty("beta2") ParameterSpace beta2,
                     @JsonProperty("epsilon") ParameterSpace epsilon){
        this.learningRate = learningRate;
        this.learningRateSchedule = learningRateSchedule;
        this.beta1 = beta1;
        this.beta2 = beta2;
        this.epsilon = epsilon;
    }

    @Override
    public IUpdater getValue(double[] parameterValues) {
        double lr = learningRate == null ? Adam.DEFAULT_ADAM_LEARNING_RATE : learningRate.getValue(parameterValues);
        ISchedule lrS = learningRateSchedule == null ? null : learningRateSchedule.getValue(parameterValues);
        double b1 = beta1 == null ? Adam.DEFAULT_ADAM_LEARNING_RATE : beta1.getValue(parameterValues);
        double b2 = beta2 == null ? Adam.DEFAULT_ADAM_LEARNING_RATE : beta2.getValue(parameterValues);
        double eps = epsilon == null ? Adam.DEFAULT_ADAM_LEARNING_RATE : epsilon.getValue(parameterValues);
        if(lrS == null){
            return new Adam(lr, b1, b2, eps);
        } else {
            Adam a = new Adam(lrS);
            a.setBeta1(b1);
            a.setBeta2(b2);
            a.setEpsilon(eps);
            return a;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy