/// <summary> /// Generates output predictions for the input samples. Computation is done in batches. /// </summary> /// <param name="x">The input data frame to run prediction.</param> /// <param name="batch_size">Size of the batch.</param> /// <returns></returns> public DataFrame Predict(DataFrame x, int batch_size) { DataFrameIter dataFrameIter = new DataFrameIter(x); List <float> predictions = new List <float>(); dataFrameIter.SetBatchSize(batch_size); while (dataFrameIter.Next()) { var data = dataFrameIter.GetBatchX(); SuperArray output = data; foreach (var layer in Layers) { if (layer.SkipPred) { continue; } layer.Forward(output); output = layer.Output; } predictions.AddRange(output.List <float>()); } DataFrame result = new DataFrame(); result.Load(predictions.ToArray()); return(result); }
/// <summary> /// Generates output predictions for the input samples. Computation is done in batches. /// </summary> /// <param name="x">The input data frame to run prediction.</param> /// <param name="batch_size">Size of the batch.</param> /// <returns></returns> public Tensor Predict(DataFrame x, int batch_size) { DataFrameIter dataFrameIter = new DataFrameIter(x); List <float> predictions = new List <float>(); dataFrameIter.SetBatchSize(batch_size); long[] outshape = null; while (dataFrameIter.Next()) { var data = dataFrameIter.GetBatchX(); Tensor output = data; foreach (var layer in Layers) { if (layer.SkipPred) { continue; } layer.Forward(output); output = layer.Output; } predictions.AddRange(output.ToArray().Cast <float>()); } return(K.CreateVariable(predictions.ToArray(), outshape)); }
/// <summary> /// Runs the epoch. /// </summary> /// <param name="iteration">The iteration.</param> /// <param name="train">The train.</param> /// <param name="val">The value.</param> /// <returns></returns> private int RunEpoch(int iteration, DataFrameIter train, DataFrameIter val = null) { train_losses.Clear(); train_metrics.Clear(); val_losses.Clear(); val_metrics.Clear(); train.Reset(); if (val != null) { val.Reset(); } while (train.Next()) { var(x, y) = train.GetBatch(); RunTrainOnBatch(iteration, x, y); x.Dispose(); y.Dispose(); } if (val != null) { while (val.Next()) { var(x, y) = val.GetBatch(); var pred = Forward(x); var lossVal = LossFn.Forward(pred, y); var metricVal = MetricFn.Calc(pred, y); val_losses.Add(Ops.Mean(lossVal)); val_metrics.Add(Ops.Mean(metricVal)); x.Dispose(); y.Dispose(); lossVal.Dispose(); metricVal.Dispose(); } } return(iteration); }