Skip to content

Commit

Permalink
more changes from Veronika.
Browse files Browse the repository at this point in the history
  • Loading branch information
wendycwong committed Oct 23, 2024
1 parent b46698e commit 57515d7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 17 deletions.
12 changes: 6 additions & 6 deletions h2o-algos/src/main/java/hex/hglm/HGLMModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ protected PredictScoreResult predictScoreImpl(Frame fr, Frame adaptFrm, String d
mb = gs._mb;
if (forTraining) {
_output._yMinusXTimesZ = gs._yMinusXTimesZ;
_output._yMinusfixPredSquare = mb._yMinusfixPredSquare;
_output._yMinusFixPredSquare = mb._yMinusFixPredSquare;
} else { // store for all frames other than the training frame
_output._yminusxtimesz_valid = gs._yMinusXTimesZ;
_output._yMinusfixPredSquare_valid = mb._yMinusfixPredSquare;
_output._yMinusXTimesZValid = gs._yMinusXTimesZ;
_output._yMinusFixPredSquareValid = mb._yMinusFixPredSquare;
}
rawFrame = gs.outputFrame();
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public static class HGLMModelOutput extends Model.Output {
public double[][][] _arjtarj;
public double[][][] _afjtarj;
public double[][] _yMinusXTimesZ; // generate during training
public double[][] _yminusxtimesz_valid; // store same value for frames other than training frame
public double[][] _yMinusXTimesZValid; // store same value for frames other than training frame
public int _num_fixed_coeffs;
public int _num_random_coeffs;
int[] _randomCatIndices;
Expand All @@ -202,8 +202,8 @@ public static class HGLMModelOutput extends Model.Output {
public int _iterations;
public int _nobs;
public int _nobs_valid;
public double _yMinusfixPredSquare;
public double _yMinusfixPredSquare_valid;
public double _yMinusFixPredSquare;
public double _yMinusFixPredSquareValid;
public TwoDimTable _scoring_history_valid;

/**
Expand Down
14 changes: 7 additions & 7 deletions h2o-algos/src/main/java/hex/hglm/MetricBuilderHGLM.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MetricBuilderHGLM extends ModelMetricsSupervised.MetricBuilderSuper
public double[] _beta;
public double[][] _ubeta;
public double[][] _tmat;
public double _yMinusfixPredSquare;
public double _yMinusFixPredSquare;
public double _sse;
public int _nobs;

Expand All @@ -48,7 +48,7 @@ public double[] perRow(double[] ds, float[] yact, double weight, double offset,
private void add2(double yresp, double predictedVal, double weight, double[] input, double[] randomInput,
double[][] yMinusXTimesZ, int level2Index, double offset) {
double temp = yresp- ArrayUtils.innerProduct(_beta, input)-offset;
_yMinusfixPredSquare += temp*temp;
_yMinusFixPredSquare += temp*temp;
ArrayUtils.add(yMinusXTimesZ[level2Index], ArrayUtils.mult(randomInput, temp));
_nobs++;
temp = yresp-predictedVal;
Expand All @@ -58,7 +58,7 @@ private void add2(double yresp, double predictedVal, double weight, double[] inp
@Override
public void reduce(MetricBuilderHGLM other) {
_metricBuilder.reduce(other._metricBuilder);
_yMinusfixPredSquare += other._yMinusfixPredSquare;
_yMinusFixPredSquare += other._yMinusFixPredSquare;
_sse += other._sse;
_nobs += other._nobs;
}
Expand All @@ -78,10 +78,10 @@ public ModelMetrics makeModelMetrics(Model m, Frame f, Frame adaptedFrame, Frame

if (forTraining) {
double loglikelihood = calHGLMLlg(metricsRegression._nobs, tmat, hglmM._output._tau_e_var, hglmM._output._arjtarj,
this._yMinusfixPredSquare, hglmM._output._yMinusXTimesZ);
this._yMinusFixPredSquare, hglmM._output._yMinusXTimesZ);
mm = new ModelMetricsRegressionHGLM(m, f, metricsRegression._nobs, this.weightedSigma(), loglikelihood,
this._customMetric, hglmM._output._iterations, hglmM._output._beta, hglmM._output._ubeta,
tmat, hglmM._output._tau_e_var, metricsRegression._MSE, this._yMinusfixPredSquare / metricsRegression._nobs,
tmat, hglmM._output._tau_e_var, metricsRegression._MSE, this._yMinusFixPredSquare / metricsRegression._nobs,
metricsRegression.mae(), metricsRegression._root_mean_squared_log_error,
metricsRegression._mean_residual_deviance, metricsRegression.aic());
} else {
Expand All @@ -97,10 +97,10 @@ public ModelMetrics makeModelMetrics(Model m, Frame f, Frame adaptedFrame, Frame
HGLMTask.ComputationEngineTask engineTask = new HGLMTask.ComputationEngineTask(null, hglmM._parms, dinfo);
engineTask.doAll(dinfo._adaptedFrame);
double loglikelihood = calHGLMLlg(engineTask._nobs, tmat, hglmM._output._tau_e_var, engineTask._ArjTArj,
this._yMinusfixPredSquare, hglmM._output._yminusxtimesz_valid);
this._yMinusFixPredSquare, hglmM._output._yMinusXTimesZValid);
mm = new ModelMetricsRegressionHGLM(m, f, metricsRegression._nobs, this.weightedSigma(), loglikelihood,
this._customMetric, hglmM._output._iterations, hglmM._output._beta, hglmM._output._ubeta, tmat,
hglmM._output._tau_e_var,metricsRegression._MSE, this._yMinusfixPredSquare/metricsRegression._nobs,
hglmM._output._tau_e_var,metricsRegression._MSE, this._yMinusFixPredSquare /metricsRegression._nobs,
metricsRegression.mae(), metricsRegression._root_mean_squared_log_error,
metricsRegression._mean_residual_deviance, metricsRegression.aic());
hglmM._output._nobs_valid = engineTask._nobs;
Expand Down
4 changes: 2 additions & 2 deletions h2o-algos/src/main/java/hex/schemas/HGLMModelV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public HGLMModelOutputV3 fillFromImpl(HGLMModel.HGLMModelOutput impl) {
"HGLM random effect coefficients", ubeta, random_coefficient_names, impl._group_column_names));
icc = impl._icc;
residual_variance = impl._tau_e_var;
mean_residual_fixed = impl._yMinusfixPredSquare/impl._nobs;
mean_residual_fixed = impl._yMinusFixPredSquare /impl._nobs;
if (impl._nobs_valid > 0)
mean_residual_fixed_valid = impl._yMinusfixPredSquare_valid/impl._nobs_valid;
mean_residual_fixed_valid = impl._yMinusFixPredSquareValid /impl._nobs_valid;
return this;
}
}
Expand Down
97 changes: 96 additions & 1 deletion h2o-algos/src/test/java/hex/hglm/HGLMBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public void checkWithManualResults1(HGLMModel model, String response, Frame fr,
check3DArrays(model._output._arjtarj, arjTArj, TOL);
// checkDoubleArrays(model._output._zttimesz, zTTimesZ, TOL);
checkDoubleArrays(model._output._yMinusXTimesZ, yMinusXTimesZ, TOL);
assertEquals(model._output._yMinusfixPredSquare, yMinusfixPredSquare, TOL);
assertEquals(model._output._yMinusFixPredSquare, yMinusfixPredSquare, TOL);
}

public void formVector(double[][] matrix, int level2Unit, double[] vector, double response) {
Expand Down Expand Up @@ -890,4 +890,99 @@ public void fillDataRows(Frame fr, int rowInd, String[] coefNames, String[] rCoe
if (rCoeffNames[index] != "intercept")
zvals[index] = fr.vec(rCoeffNames[index]).at(rowInd);
}

@Test
public void testFrameToArray1() {
/***
* test for one column
*/
Scope.enter();
try {
int arrayLen = 18;
final double[] arrayContent = genRandomArray(arrayLen, 123);
Frame tMat = new TestFrameBuilder()
.withColNames("C1")
.withVecTypes(T_NUM)
.withDataForCol(0, arrayContent)
.build();
Scope.track(tMat);
double[][] fromFrame = generateArrayFromFrame(tMat);
for (int index=0; index<arrayLen; index++)
assertEquals(arrayContent[index], fromFrame[index][0], 1e-6);
} finally {
Scope.exit();
}
}

public double[][] generateArrayFromFrame(Frame fr) {
double[][] fromFrame = new double[(int) fr.numRows()][fr.numCols()];
final ArrayUtils.FrameToArray f2a = new ArrayUtils.FrameToArray(0, fr.numCols()-1, fromFrame.length, fromFrame);
fromFrame = f2a.doAll(fr).getArray();
return fromFrame;
}

@Test
public void testFrameToArray2() {
/***
* test for one row and multiple columns
*/
Scope.enter();
try {
int arrayLen = 10;
double[] arrayContent = genRandomArray(arrayLen, 123);
Frame tMat = new TestFrameBuilder()
.withColNames("C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10")
.withVecTypes(T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM)
.withDataForCol(0, new double[]{arrayContent[0]})
.withDataForCol(1, new double[]{arrayContent[1]})
.withDataForCol(2, new double[]{arrayContent[2]})
.withDataForCol(3, new double[]{arrayContent[3]})
.withDataForCol(4, new double[]{arrayContent[4]})
.withDataForCol(5, new double[]{arrayContent[5]})
.withDataForCol(6, new double[]{arrayContent[6]})
.withDataForCol(7, new double[]{arrayContent[7]})
.withDataForCol(8, new double[]{arrayContent[8]})
.withDataForCol(9, new double[]{arrayContent[9]})
.build();
Scope.track(tMat);
double[][] fromFrame = generateArrayFromFrame(tMat);
for (int index=0; index<arrayLen; index++)
assertEquals(arrayContent[index], fromFrame[0][index], 1e-6);
} finally{
Scope.exit();
}
}

@Test
public void testFrameToArray3() {
/***
* test for multiple columns and rows
*/
Scope.enter();
try {
int numCol = 8;
int numRow = 18;
double[][] arrayContents = genRandomMatrix(numCol, numRow, 123);
Frame tMat = new TestFrameBuilder()
.withColNames("C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8")
.withVecTypes(T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM, T_NUM)
.withDataForCol(0, arrayContents[0])
.withDataForCol(1, arrayContents[1])
.withDataForCol(2, arrayContents[2])
.withDataForCol(3, arrayContents[3])
.withDataForCol(4, arrayContents[4])
.withDataForCol(5, arrayContents[5])
.withDataForCol(6, arrayContents[6])
.withDataForCol(7, arrayContents[7])
.build();
Scope.track(tMat);
double[][] fromFrame = generateArrayFromFrame(tMat);
for (int rowInd = 0; rowInd < numRow; rowInd++)
for (int colInd = 0; colInd < numCol; colInd++)
assertEquals(arrayContents[colInd][rowInd], fromFrame[rowInd][colInd], 1e-6);
} finally {
Scope.exit();
}
}

}
3 changes: 2 additions & 1 deletion h2o-py/tests/testdir_algos/glm/pyunit_benign_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

def test_benign():
training_data = h2o.import_file(pyunit_utils.locate("smalldata/logreg/benign.csv"))
training_data[3] = training_data[3].asfactor()

Y = 3
X = [0, 1, 2, 4, 5, 6, 7, 8, 9, 10]

model = H2OGeneralizedLinearEstimator(family="binomial", alpha=0, Lambda=1e-5)
model = H2OGeneralizedLinearEstimator(family="binomial", alpha=0, lambda_=1e-5)
model.train(x=X, y=Y, training_frame=training_data)

Expand Down

0 comments on commit 57515d7

Please sign in to comment.