public WeightsAndBias RunEpoch(Example[] trainSet, ISGDModel <WeightsAndBias> model, WeightsAndBias curWeights) { foreach (var e in trainSet) { model.Update(curWeights, e); } return(curWeights); }
public Params RunEpoch(DSet <Example> trainSet, ISGDModel <Params> model, Params curParams) { Params accumulatedParams = trainSet.Fold( (wb, e) => { return(model.Update(wb, e)); }, model.AddParams, curParams); var averagedParams = model.ScaleParams(accumulatedParams, (float)(1.0 / trainSet.NumPartitions)); return(averagedParams); }
public float GetAccuracy(DSet <Example> dataset, ISGDModel <Params> model, Params curParams) { int hits = dataset.Fold((c, ex) => model.Predict(curParams, ex) == ex.Label ? c + 1 : c, (c1, c2) => c1 + c2, 0); return((float)hits / dataset.Count()); }
/// <summary> /// Runs the outer loop of an SGD, parameterized by type of dataset (e.g.: Example[] or DSet<Example>) /// and type of model Params (currently only WeightsAndBias, but could be anything, such as a big neural net). /// The epochRunner runs each epoch and decides the strategy to run the epoch (distributed, sequential, multi-threaded), /// and it is also used to evaluates the accuracy after each 10 epochs. /// The model is passed to the epochRunner to the the individual updates, and so long as the param types match, any model /// can be used with any epoch runner. This is why we require each model to be able to average its parameters. /// To do: Use a validation set based criterion to stop, rather than number of epochs. /// </summary> private static Params RunSGD <Dataset, Params>(Dataset trainSet, Dataset testSet, ISGDModel <Params> model, Params initialParams, ISGDEpoch <Dataset, Params> epochRunner, int numEpochs) { var weightsAndBias = initialParams; var sw = System.Diagnostics.Stopwatch.StartNew(); for (int epoch = 0; epoch < numEpochs; epoch++) { sw.Restart(); Params epochResult = epochRunner.RunEpoch(trainSet, model, weightsAndBias); Console.WriteLine($"Epoch {epoch}. took: {sw.Elapsed}"); if (epoch % 10 == 0) { sw.Restart(); float trainAcc = epochRunner.GetAccuracy(trainSet, model, epochResult); Console.WriteLine($"Train Accuracy: {trainAcc}"); if (testSet != null) { float testAcc = epochRunner.GetAccuracy(testSet, model, epochResult); Console.WriteLine($"Test Accuracy: {testAcc}"); } Console.WriteLine($"Evaluating accuracies took: {sw.Elapsed}."); Console.WriteLine(); } weightsAndBias = epochResult; } return(weightsAndBias); }
public float GetAccuracy(Example[] dataset, ISGDModel <WeightsAndBias> model, WeightsAndBias curWeights) { int hits = dataset.Sum(ex => model.Predict(curWeights, ex) == ex.Label ? 1 : 0); return((float)hits / dataset.Length); }