如何正常化Encog的csv输出coloumn?

时间:2016-06-12 13:27:25

标签: c# encog

鉴于此CSV(GoogleSheets)。我想保持数值不变。我怎样才能真正利用这些数据来训练我的前馈网络?

// Load and prepare training data
var dataSource = new CSVDataSource("trainingData.csv", true, CSVFormat.DecimalPoint);
var data = new VersatileMLDataSet(dataSource);
ColumnDefinition outputColumn = data.DefineSourceColumn("Action", ColumnType.Nominal);
data.DefineSingleOutputOthersInput(outputColumn);
data.Analyze();

// Build neural net
var neuralNet = BuildNeuralNet();

// Train neural net
var train = new Backpropagation(neuralNet, data);
int epoch = 1;

do
{
train.Iteration();
Console.WriteLine(@"Epoch #" + epoch + @"  Error : " + train.Error);
epoch++;
} while (train.Error > errorThreshold);

这是我在执行期间得到的EncogError: “机器学习方法的输入长度为5,但训练数据为0.它们必须相同。”

private static BasicNetwork BuildNeuralNet()
{
        var net = new BasicNetwork();
        net.AddLayer(new BasicLayer(null, true, m_inputNodeCount)); // input layer
        net.AddLayer(new BasicLayer(new ActivationSigmoid(), true, m_hiddenNodeCount)); // #1 hidden layer
        net.AddLayer(new BasicLayer(new ActivationSigmoid(), false, m_outputNodeCount)); // output layer
        net.Structure.FinalizeStructure();
        net.Reset(); // initializes the weights of the neural net
        return net;
}

2 个答案:

答案 0 :(得分:0)

尝试以下内容。重点是后向传播的数据必须分为输入和理想

    // Load and prepare training data
    var dataSource = new CSVDataSource(@"C:\dev\SO\learning\encog\SO-Test\trainingData.csv", true, CSVFormat.DecimalPoint);
    var data = new VersatileMLDataSet(dataSource);
    data.DefineSourceColumn("EnemyHitPoints", ColumnType.Continuous);
    data.DefineSourceColumn("EnemyCount", ColumnType.Continuous);
    data.DefineSourceColumn("FriendlySquadHitPoints", ColumnType.Continuous);
    data.DefineSourceColumn("FriendlySquadCount", ColumnType.Continuous);
    data.DefineSourceColumn("LocalHitPoints", ColumnType.Continuous);
    //EnemyHitPoints,EnemyCount,FriendlySquadHitPoints,FriendlySquadCount,LocalHitPoints,Action
    ColumnDefinition outputColumn = data.DefineSourceColumn("Action", ColumnType.Nominal);
    data.DefineSingleOutputOthersInput(outputColumn);
    data.Analyze();

    EncogModel model = new EncogModel(data);
    model.SelectMethod(data, MLMethodFactory.TypeNEAT);

    // Now normalize the data. Encog will automatically determine the
    // correct normalization
    // type based on the model you chose in the last step.
    data.Normalize();
    model.SelectTrainingType(data);

    // Build neural net
    var neuralNet = BuildNeuralNet();


    var datainput = data.Select(x => new double[5] { x.Input[0], x.Input[1], x.Input[2],
    x.Input[3], x.Input[4] }).ToArray();
    var dataideal = data.Select(x => new double[1] { x.Ideal[0] }).ToArray();

        IMLDataSet trainingData = new BasicMLDataSet(datainput, dataideal);
        var train = new Backpropagation(neuralNet, trainingData);
        int epoch = 1;

        do
        {
            train.Iteration();
            Console.WriteLine(@"Epoch #" + epoch + @"  Error : " + train.Error);
            epoch++;
        } while (train.Error > errorThreshold);

答案 1 :(得分:0)

我只是将输出列拆分为三个新列(GoogleSheets)。而我所要做的就是像这样加载CSV:

var trainingSet = EncogUtility.LoadCSV2Memory("trainingData.csv", neuralNet.InputCount, neuralNet.OutputCount, true, CSVFormat.English, false);