Github user myui commented on a diff in the pull request:

    https://github.com/apache/incubator-hivemall/pull/167#discussion_r226578559
  
    --- Diff: core/src/main/java/hivemall/mf/CofactorModel.java ---
    @@ -0,0 +1,715 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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 hivemall.mf;
    +
    +import hivemall.annotations.VisibleForTesting;
    +import hivemall.fm.Feature;
    +import hivemall.utils.lang.Preconditions;
    +import hivemall.utils.math.MathUtils;
    +import it.unimi.dsi.fastutil.objects.Object2DoubleArrayMap;
    +import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
    +import org.apache.commons.math3.linear.ArrayRealVector;
    +import org.apache.commons.math3.linear.Array2DRowRealMatrix;
    +import org.apache.commons.math3.linear.RealMatrix;
    +import org.apache.commons.math3.linear.RealVector;
    +import org.apache.commons.math3.linear.SingularValueDecomposition;
    +import org.apache.hadoop.hive.ql.metadata.HiveException;
    +
    +import javax.annotation.Nonnegative;
    +import javax.annotation.Nonnull;
    +import javax.annotation.Nullable;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Random;
    +
    +
    +public class CofactorModel {
    +
    +    public enum RankInitScheme {
    +        random /* default */, gaussian;
    +
    +
    +        @Nonnegative
    +        private float maxInitValue;
    +        @Nonnegative
    +        private double initStdDev;
    +        @Nonnull
    +        public static CofactorModel.RankInitScheme resolve(@Nullable 
String opt) {
    +            if (opt == null) {
    +                return random;
    +            } else if ("gaussian".equalsIgnoreCase(opt)) {
    +                return gaussian;
    +            } else if ("random".equalsIgnoreCase(opt)) {
    +                return random;
    +            }
    +            return random;
    +        }
    +
    +        public void setMaxInitValue(float maxInitValue) {
    +            this.maxInitValue = maxInitValue;
    +        }
    +
    +        public void setInitStdDev(double initStdDev) {
    +            this.initStdDev = initStdDev;
    +        }
    +
    +
    +    }
    +
    +    @Nonnegative
    +    private final int factor;
    +
    +    // rank matrix initialization
    +    private final RankInitScheme initScheme;
    +
    +    @Nonnull
    +    private double globalBias;
    +
    +    // storing trainable latent factors and weights
    +    private final Map<String, double[]> theta;
    +    private final Map<String, double[]> beta;
    +    private final Object2DoubleMap<String> betaBias;
    +    private final Map<String, double[]> gamma;
    +    private final Object2DoubleMap<String> gammaBias;
    +
    +    private final Random[] randU, randI;
    +
    +    // hyperparameters
    +    private final float c0, c1;
    +    private final float lambdaTheta, lambdaBeta, lambdaGamma;
    +
    +    // solve
    +    private final RealMatrix B;
    +    private final RealVector A;
    +
    +    // error message strings
    +    private static final String ARRAY_NOT_SQUARE_ERR = "Array is not 
square";
    +    private static final String DIFFERENT_DIMS_ERR = "Matrix, vector or 
array do not match in size";
    +
    +    public CofactorModel(@Nonnegative int factor, @Nonnull RankInitScheme 
initScheme,
    +                         float c0, float c1, float lambdaTheta, float 
lambdaBeta, float lambdaGamma) {
    +
    +        // rank init scheme is gaussian
    +        // 
https://github.com/dawenl/cofactor/blob/master/src/cofacto.py#L98
    +        this.factor = factor;
    +        this.initScheme = initScheme;
    +        this.globalBias = 0.d;
    +        this.lambdaTheta = lambdaTheta;
    +        this.lambdaBeta = lambdaBeta;
    +        this.lambdaGamma = lambdaGamma;
    +
    +        this.theta = new HashMap<>();
    +        this.beta = new HashMap<>();
    +        this.betaBias = new Object2DoubleArrayMap<>();
    +        this.betaBias.defaultReturnValue(0.d);
    +        this.gamma = new HashMap<>();
    +        this.gammaBias = new Object2DoubleArrayMap<>();
    +        this.gammaBias.defaultReturnValue(0.d);
    +
    +        this.B = new Array2DRowRealMatrix(this.factor, this.factor);
    +        this.A = new ArrayRealVector(this.factor);
    +
    +        this.randU = newRandoms(factor, 31L);
    +        this.randI = newRandoms(factor, 41L);
    +
    +        Preconditions.checkArgument(c0 >= 0.f && c0 <= 1.f);
    +        Preconditions.checkArgument(c1 >= 0.f && c1 <= 1.f);
    +        this.c0 = c0;
    +        this.c1 = c1;
    +
    --- End diff --
    
    break line


---

Reply via email to