/// <summary> /// 分类问题中产生的如果是概率,需要把概率转换为编码结果 /// 后面再解码得到最终的分类 /// </summary> /// <param name="probability">概率</param> /// <param name="code">编码</param> public static void ProbabilityToCode(TensorOld probability, TensorOld code) { if (probability.Rank == 1) { probability = probability.Reshape(1, probability.ElementCount); } if (probability.Rank != 2) { throw new Exception("to do codec, Rank must be 2"); } if (probability.shape[1] == 1) { TensorOld.Apply(probability, code, a => a > 0.5 ? 1 : 0); } else { var buff = new double[probability.shape[1]]; for (int i = 0; i < probability.shape[0]; i++) { probability.GetByDim1(i, buff); ComputeCode(buff); Array.Copy(buff, 0, code.GetRawValues(), i * buff.Length, buff.Length); } } }
public List <double> PredictValue(TensorOld X) { X = Normalize(X); LastRawResult = Model.Predict(X); LastCodecResult = Utilities.ProbabilityToCode(LastRawResult); return(LastCodecResult.GetRawValues().ToList()); }
private static bool RowEqual(TensorOld t1, TensorOld t2, int start, int len) { for (int i = 0; i < len; i++) { if (t1.GetRawValues()[start + i] != t2.GetRawValues()[start + i]) { return(false); } } return(true); }
private void SetTrainingData(TensorOld trainX, TensorOld trainY, TensorOld testX, TensorOld testY) { if (Normalizer != null) { TrainX = Normalizer.Normalize(trainX); if (testX != null) { TestX = Normalizer.Normalize(testX); } } else { TrainX = trainX; TestX = testX; } if (LabelCodec != null) { TrainY = LabelCodec.Encode(trainY.GetRawValues()); if (testY != null) { TestY = LabelCodec.Encode(testY.GetRawValues()); } } else { TrainY = trainY; TestY = testY; } var shape = TrainX.Shape; shape[0] = BatchSize; xBuff = new TensorOld(shape); yBuff = new TensorOld(BatchSize, TrainY.shape[1]); sampleCount = TrainX.shape[0]; xWidth = TrainX.ElementCount / TrainX.shape[0]; yWidth = TrainY.ElementCount / TrainY.shape[0]; batchPerEpoch = trainX.shape[0] / BatchSize; if (trainX.shape[0] % BatchSize != 0) { batchPerEpoch++; } }
private void CopyToBatch(int trainIndex, int batchIndex) { Array.Copy(TrainX.GetRawValues(), trainIndex * xWidth, xBuff.GetRawValues(), batchIndex * xWidth, xWidth); Array.Copy(TrainY.GetRawValues(), trainIndex * yWidth, yBuff.GetRawValues(), batchIndex * yWidth, yWidth); }