EdwardRaff/JSAT · SCD.java
java logo
@Override
    public void train(ClassificationDataSet dataSet)
    {
        double[] targets = new double[dataSet.size()];
        for (int i = 0; i < targets.length; i++)
            targets[i] = dataSet.getDataPointCategory(i) * 2 - 1;
        train(dataSet.getNumericColumns(), targets);
    }

    @Override
    public boolean supportsWeightedData()
    {
        return false;
    }

    @Override
    public double regress(DataPoint data)
    {
        if (w != null && loss instanceof LossR)
            return ((LossR) loss).getRegression(w.dot(data.getNumericalValues()));
        else
            throw new UntrainedModelException("Model was not trained with a classification function");
    }

    @Override
    public void train(RegressionDataSet dataSet, boolean parallel)
    {
        train(dataSet);
    }

    @Override
    public void train(RegressionDataSet dataSet)
    {
        train(dataSet.getNumericColumns(), dataSet.getTargetValues().arrayCopy());
    }

    /**
     *
     * @param columns columns of the training matrix
     * @param y the target values
     */
    private void train(Vec[] columns, double[] y)
    {
        final double beta = loss.getDeriv2Max();
        double[] z = new double[y.length];///stores w.dot(x)
        w = new DenseVector(columns.length);
        Random rand = RandomUtil.getRandom();
        for (int iter = 0; iter < iterations; iter++)
        {
            final int j = rand.nextInt(columns.length);
            double g = 0;
            for (IndexValue iv : columns[j])
                g += loss.getDeriv(z[iv.getIndex()], y[iv.getIndex()]) * iv.getValue();
            g /= y.length;
            final double w_j = w.get(j);
            final double eta;
            if (w_j - g / beta > reg / beta)
                eta = -g / beta - reg / beta;
            else if (w_j - g / beta < -reg / beta)
                eta = -g / beta + reg / beta;
            else
                eta = -w_j;
            w.increment(j, eta);

            for (IndexValue iv : columns[j])
                z[iv.getIndex()] += eta * iv.getValue();
        }
    }

    @Override
    public SCD clone()
    {
        return new SCD(this);
    }
}
Similar code snippets
1.
EdwardRaff/JSAT · MahalanobisDistance.java
Match rating: 71.49% · See similar code snippets
java logo
@Override
    public <V extends Vec> void train(List<V> dataSet)
    {
        train(dataSet, false);
    }
2.
Match rating: 68.89% · See similar code snippets
java logo
@Override
    public <V extends Vec> void train(List<V> dataSet, boolean parallel)
    {
        train(dataSet);
    }
3.
EdwardRaff/JSAT · RegressorToClassifier.java
Match rating: 68.62% · See similar code snippets
java logo
@Override
    public void train(ClassificationDataSet dataSet, boolean parallel)
    {
        RegressionDataSet rds = getRegressionDataSet(dataSet);
        regressor.train(rds, parallel);
    }
4.
Match rating: 68.26% · See similar code snippets
java logo
@Override
    public void train(Dataset dataset)
    {
        transformation.train(dataset);
        regressor.train(transformation.transform(dataset));
    }
5.
Match rating: 67.16% · See similar code snippets
java logo
@Override
    public void train(Dataset dataset)
    {
        transformation.train(dataset);
        classifier.train(transformation.transform(dataset));
    }
6.
EdwardRaff/JSAT · LWL.java
Match rating: 66.86% · See similar code snippets
java logo
@Override
    public void train(RegressionDataSet dataSet, boolean parallel)
    {
        List<VecPaired<Vec, Double>> trainList = getVecList(dataSet);
        
        TrainableDistanceMetric.trainIfNeeded(dm, dataSet, parallel);
        vc.build(parallel, trainList, dm);
    }
7.
EdwardRaff/JSAT · LWL.java
Match rating: 66.0% · See similar code snippets
java logo
@Override
    public void train(ClassificationDataSet dataSet, boolean parallel)
    {
        List<VecPaired<Vec, Double>> trainList = getVecList(dataSet);
        
        TrainableDistanceMetric.trainIfNeeded(dm, dataSet, parallel);
        vc.build(parallel, trainList, dm);
        predicting = dataSet.getPredicting();
    }
@Override
    public CategoricalResults classify(DataPoint data)
    {
        if(B == null)
            throw new UntrainedModelException("Model has not yet been trained");
        final Vec x = data.getNumericalValues();

        double[] probs = new double[B.length + 1];

        for (int i = 0; i < B.length; i++)
            probs[i] = x.dot(B[i])+biases[i];
        probs[B.length] = 1;
        MathTricks.softmax(probs, false);

        return new CategoricalResults(probs);
    }

    @Override
    public void train(ClassificationDataSet dataSet, boolean parallel)
    {
        train(dataSet);
    }

    @Override
    public void train(ClassificationDataSet dataSet)
    {
        final int n = dataSet.size();
        final double N = n;
        final int d = dataSet.getNumNumericalVars();
        if(d < 1)
            throw new FailedToFitException("Data set has no numeric attributes to train on");
        B = new Vec[dataSet.getClassSize()-1];
        biases = new double[B.length];
        for(int i = 0; i < B.length; i++)
            B[i] = new DenseVector(d);
        
        IntList randOrder = new IntList(n);
        ListUtils.addRange(randOrder, 0, n, 1);
        
        Vec means = null, stdDevs = null;
        
        if(standardized)
        {
            Vec[] ms = dataSet.getColumnMeanVariance();
            means = ms[0];
            stdDevs = ms[1];
            stdDevs.applyFunction(Math::sqrt);

            //Now transform it so that stdDevs holds standard deviations, and means is the mean / standDev
            means.pairwiseDivide(stdDevs);

            stdDevs.applyFunction((x)-> 1/x);
        }
        
        
        double[] zs = new double[B.length];
        
        /**
         * Contains the last time each feature was used
         */
        int[] u = new int[d];
        /**
         * Contains the current time. 
         */
        int q = 0;
        
        double prevLogLike = Double.POSITIVE_INFINITY;
        //learing rate in use
        double eta;
        
        for(int iter = 0; iter < epochs; iter++)
        {
            Collections.shuffle(randOrder);
            double logLike = 0;
            eta = learningRateDecay.rate(iter, epochs, initialLearningRate);
            final double etaReg = regularization*eta;
            
            for (int batch = 0; batch < randOrder.size(); batch += miniBatchSize)
            {
                int batchCount = Math.min(miniBatchSize, randOrder.size() - batch);
                double batchFrac = 1.0 / batchCount;
                for (int k = 0; k < batchCount; k++)
                {
                    int j = randOrder.get(batch+k);
                    final int c_j = dataSet.getDataPointCategory(j);
                    final Vec x_j = dataSet.getDataPoint(j).getNumericalValues();

                    //compute softmax
                    for (int i = 0; i < B.length; i++)
                        zs[i] = x_j.dot(B[i]) + biases[i];

                    MathTricks.softmax(zs, true);


                    //lazy apply lost rounds of regularization
                    if (prior != Prior.UNIFORM)
                    {
                        for (IndexValue iv : x_j)
                        {
                            int i = iv.getIndex();
                            if(u[i] == 0)
                                continue;
                            double etaRegScaled = etaReg * (u[i] - q) / N;
                            for (Vec b : B)
                            {
                                double bVal = b.get(i);
                                double bNewVal = bVal;
                                if (standardized)
                                    bNewVal += etaRegScaled * prior.gradientError(bVal * stdDevs.get(i) - means.get(i), 1, alpha);
                                else
                                    bNewVal += etaRegScaled * prior.gradientError(bVal, 1, alpha);

                                if (clipping && signum(bVal) != signum(bNewVal))
                                    b.set(i, 0);
                                else
                                    b.set(i, bNewVal);
                            }
                            u[i] = q;
                        }

                        //No need to do bias here, b/c bias is always up to date
                    }

                    for (int c = 0; c < B.length; c++)
                    {
                        Vec b = B[c];
                        double p_c = zs[c];
                        double log_pc = log(p_c);
                        if (!Double.isInfinite(log_pc))
                            logLike += log_pc;
                        double errScaling = (c == c_j ? 1 : 0) - p_c;
                        b.mutableAdd(batchFrac*eta * errScaling, x_j);
                        if (useBias)
                            biases[c] += batchFrac*eta * errScaling + etaReg * prior.gradientError(biases[c] - 1, 1, alpha);
                    }
                }
                
                q++;
            }

            logLike *= -1;
            if (prior != Prior.UNIFORM)
            {
                for (int i = 0; i < d; i++)
                {
                    if (u[i] - q == 0)
                    {
                        for (Vec b : B)
                            if (standardized)
                                logLike += regularization*prior.logProb(b.get(i) * stdDevs.get(i) - means.get(i), 1, alpha);
                            else
                                logLike += regularization*prior.logProb(b.get(i), 1, alpha);
                        continue;
                    }
                    double etaRegScaled = etaReg * (u[i] - q) / N;
                    for (Vec b : B)
                    {
                        double bVal = b.get(i);
                        if (bVal == 0.0)
                            continue;
                        double bNewVal = bVal;
                        if (standardized)
                            bNewVal += etaRegScaled * prior.gradientError(bVal * stdDevs.get(i) - means.get(i), 1, alpha);
                        else
                            bNewVal += etaRegScaled * prior.gradientError(bVal, 1, alpha);
                        
                        if (clipping && signum(bVal) != signum(bNewVal))
                            b.set(i, 0);
                        else
                            b.set(i, bNewVal);

                        if(standardized)
                            logLike += regularization*prior.logProb(b.get(i) * stdDevs.get(i) - means.get(i), 1, alpha);
                        else
                            logLike += regularization*prior.logProb(b.get(i), 1, alpha);
                    }
                    u[i] = q;
                }
            }
            
            double dif = abs(prevLogLike-logLike)/(abs(prevLogLike)+abs(logLike));
            if(dif < tolerance)
                break;
            else
                prevLogLike = logLike;
            
        }
    }

    @Override
    public boolean supportsWeightedData()
    {
        return false;
    }
    
    /**
     * Returns the raw coefficient vector used without the bias term. For a 
     * multinomial Logistic model, there are C-1 coefficient vectors. C is the
     * number of output classes. Altering the returned vector will alter the 
     * model. The i'th index of the vector corresponds to the weight therm for
     * the i'th index in an input. 
     * 
     * @param id which coefficient vector to obtain
     * @return the vector of variable coefficients. 
     */
    public Vec getCoefficientVector(int id)
    {
        return B[id];
    }

    @Override
    public Classifier clone()
    {
        return new StochasticMultinomialLogisticRegression(this);
    }
}
9.
EdwardRaff/JSAT · LWL.java
Match rating: 64.31% · See similar code snippets
java logo
private List<VecPaired<Vec, Double>> getVecList(ClassificationDataSet dataSet)
    {
        List<VecPaired<Vec, Double>> trainList = 
                new ArrayList<>(dataSet.size());
        for(int i = 0; i < dataSet.size(); i++)
            trainList.add(new VecPaired<>(
                    dataSet.getDataPoint(i).getNumericalValues(), 
                    new Double(dataSet.getDataPointCategory(i))));
        return trainList;
    }
    
    private List<VecPaired<Vec, Double>> getVecList(RegressionDataSet dataSet)
    {
        List<VecPaired<Vec, Double>> trainList = 
                new ArrayList<>(dataSet.size());
        for(int i = 0; i < dataSet.size(); i++)
            trainList.add(new VecPaired<>(
                    dataSet.getDataPoint(i).getNumericalValues(), 
                    dataSet.getTargetValue(i)));
        return trainList;
    }

    private void setClassifier(Classifier classifier)
    {
        this.classifier = classifier;
        if(classifier instanceof Regressor)
            this.regressor = (Regressor) classifier;
    }

    private void setRegressor(Regressor regressor)
    {
        this.regressor = regressor;
        if(regressor instanceof Classifier)
            this.classifier = (Classifier)regressor;
    }

    /**
     * Sets the number of neighbors that will be used to create the local model
     * @param k the number of neighbors to obtain
     */
    public void setNeighbors(int k)
    {
        if(k <= 1)
            throw new RuntimeException("An average requires at least 2 neighbors to be taken into account");
        this.k = k;
    }

    /**
     * Returns the number of neighbors that will be used to create each local model
     * @return the number of neighbors that will be used
     */
    public int getNeighbors()
    {
        return k;
    }

    /**
     * Sets the distance metric that will be used for the nearest neighbor search
     * @param dm the distance metric to use for nearest neighbor search
     */
    public void setDistanceMetric(DistanceMetric dm)
    {
        this.dm = dm;
    }

    /**
     * Returns the distance metric in use
     * @return the distance metric in use
     */
    public DistanceMetric getDistanceMetric()
    {
        return dm;
    }
    
    /**
     * Sets the kernel function that will be used to set the weights of each 
     * data point in the local set
     * @param kf the kernel function to use for weighting
     */
    public void setKernelFunction(KernelFunction kf)
    {
        this.kf = kf;
    }

    /**
     * Returns the kernel function that will be used to set the weights. 
     * @return the kernel function that will be used to set the weights
     */
    public KernelFunction getKernelFunction()
    {
        return kf;
    }
    
    /**
     * Guesses the distribution to use for the number of neighbors to consider
     *
     * @param d the dataset to get the guess for
     * @return the guess for the Neighbors parameter
     */
    public static Distribution guessNeighbors(DataSet d)
    {
        return new UniformDiscrete(25, Math.min(200, d.size()/5));
    }
}
10.
Match rating: 63.82% · See similar code snippets
java logo
@Override
    public void train(Dataset dataset)
    {
        labelEnumeration.putAll(dataset.getLabelEnumeration());
        classifier.train(dataset);
    }