public IList <float> Evaluate(DataFrame data) { Value features = DataFrameUtil.GetValueBatch(data); var outputVal = EvaluateInternal(features); IList <IList <float> > resultSet = outputVal.GetDenseData <float>(Model.Output); var result = resultSet[0]; return(result); }
private Value EvaluateInternal(DataFrame data) { Value features = DataFrameUtil.GetValueBatch(data); var inputDataMap = new Dictionary <Variable, Value>() { { featureVariable, features } }; var outputDataMap = new Dictionary <Variable, Value>() { { Model.Output, null } }; Model.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device); return(outputDataMap[Model.Output]); }
public Dictionary <string, List <double> > Train(object trainData, object validationData, int epoches, int batchSize, On_Epoch_Start OnEpochStart, On_Epoch_End OnEpochEnd, On_Batch_Start onBatchStart, On_Batch_End OnBatchEnd, bool shuffle = false) { XYFrame train = (XYFrame)trainData; XYFrame validation = validationData != null ? (XYFrame)validationData : null; Dictionary <string, List <double> > result = new Dictionary <string, List <double> >(); var trainer = Trainer.CreateTrainer(Model, lossFunc, metricFunc, learners); int currentEpoch = 1; Dictionary <string, double> metricsList = new Dictionary <string, double>(); while (currentEpoch <= epoches) { if (shuffle) { train.Shuffle(); } metricsList = new Dictionary <string, double>(); OnEpochStart(currentEpoch); int miniBatchCount = 1; while (train.NextBatch(miniBatchCount, batchSize)) { onBatchStart(currentEpoch, miniBatchCount); Value features = DataFrameUtil.GetValueBatch(train.CurrentBatch.XFrame); Value labels = DataFrameUtil.GetValueBatch(train.CurrentBatch.YFrame); trainer.TrainMinibatch(new Dictionary <Variable, Value>() { { featureVariable, features }, { labelVariable, labels } }, GlobalParameters.Device); OnBatchEnd(currentEpoch, miniBatchCount, trainer.TotalNumberOfSamplesSeen(), trainer.PreviousMinibatchLossAverage(), new Dictionary <string, double>() { { metricName, trainer.PreviousMinibatchEvaluationAverage() } }); miniBatchCount++; } if (!result.ContainsKey("loss")) { result.Add("loss", new List <double>()); } if (!result.ContainsKey(metricName)) { result.Add(metricName, new List <double>()); } double lossValue = trainer.PreviousMinibatchLossAverage(); double metricValue = trainer.PreviousMinibatchEvaluationAverage(); result["loss"].Add(lossValue); result[metricName].Add(metricValue); metricsList.Add(metricName, metricValue); if (validation != null) { if (!result.ContainsKey("val_loss")) { result.Add("val_loss", new List <double>()); } if (!result.ContainsKey("val_" + metricName)) { result.Add("val_" + metricName, new List <double>()); } int evalMiniBatchCount = 1; List <double> totalEvalBatchLossList = new List <double>(); List <double> totalEvalMetricValueList = new List <double>(); while (validation.NextBatch(evalMiniBatchCount, batchSize)) { Variable actualVariable = CNTKLib.InputVariable(labelVariable.Shape, DataType.Float); var evalLossFunc = Losses.Get(lossName, labelVariable, actualVariable); var evalMetricFunc = Metrics.Get(metricName, labelVariable, actualVariable); Value actual = EvaluateInternal(validation.CurrentBatch.XFrame); Value expected = DataFrameUtil.GetValueBatch(validation.CurrentBatch.YFrame); var inputDataMap = new Dictionary <Variable, Value>() { { labelVariable, expected }, { actualVariable, actual } }; var outputDataMap = new Dictionary <Variable, Value>() { { evalLossFunc.Output, null } }; evalLossFunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device); var evalLoss = outputDataMap[evalLossFunc.Output].GetDenseData <float>(evalLossFunc.Output).Select(x => x.First()).ToList(); totalEvalBatchLossList.Add(evalLoss.Average()); inputDataMap = new Dictionary <Variable, Value>() { { labelVariable, expected }, { actualVariable, actual } }; outputDataMap = new Dictionary <Variable, Value>() { { evalMetricFunc.Output, null } }; evalMetricFunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device); var evalMetric = outputDataMap[evalMetricFunc.Output].GetDenseData <float>(evalMetricFunc.Output).Select(x => x.First()).ToList(); totalEvalMetricValueList.Add(evalMetric.Average()); evalMiniBatchCount++; } result["val_loss"].Add(totalEvalBatchLossList.Average()); metricsList.Add("val_loss", totalEvalBatchLossList.Average()); result["val_" + metricName].Add(totalEvalMetricValueList.Average()); metricsList.Add("val_" + metricName, totalEvalMetricValueList.Average()); } OnEpochEnd(currentEpoch, trainer.TotalNumberOfSamplesSeen(), lossValue, metricsList); currentEpoch++; } return(result); }