1

我知道这里有更多关于这个主题的主题,但它们都没有真正帮助我。

我将提供完整的代码:

namespace ConsoleApplication1
{
    public static class Load
    {
        public static double[][] FromFile(string path)
        {
            var rows = new List<double[]>();
            foreach (var line in File.ReadAllLines(path))
            {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' }).Select(double.Parse).ToArray());  
            }
            return rows.ToArray();
        }
    }

    public class Program
    {

        static void Main( string [ ] args )
        {
            string cestain = @"E:\vstup.txt";
            double[][] innput = Load.FromFile(cestain);

            string cestaout = @"E:\vystup.txt";
            double[][] ooutput = Load.FromFile(cestaout);


            // c r e a t e a neural network
            var network = new BasicNetwork ( ) ;
            network . AddLayer (new BasicLayer ( null , true , 2) ) ;
            network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , true , 3) ) ;
            network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , false , 1) ) ;
            network . Structure . FinalizeStructure ( ) ;
            network . Reset( );

            // c r e a t e t r a i n i n g data
            IMLDataSet trainingSet = new BasicMLDataSet (innput , ooutput ) ;
            // t r a i n the neural network
            IMLTrain train = new ResilientPropagation (network , trainingSet) ;
            int epoch = 1 ;
            do
            {
                train.Iteration( ) ;
                Console.WriteLine(@"Epoch #" + epoch + @" Error : " + train.Error );
                epoch++;
            } while ( train.Error> 0.01 ) ;

            Console.ReadLine();
        }
    }
}

这是我要加载到double[][]输入的内容:

166 163 180 228

165 162 160 226

166 163 180 228

166 164 180 228

171 162 111 225

这是我要加载到double[][]输出的内容:

1 0 0

1 0 0

0 1 0

0 1 0

1 0 0
4

2 回答 2

1

问题:文本文件中的每一行之后都有额外的 EmptyLines。当 Split() 函数遇到新 Line 时,它​​会返回,因为没有要拆分的内容,并且Add()函数会抛出异常,因为空行不是有效的Double

解决方案 1:您可以StringSplitOptions.RemoveEmptyEntries用作函数的第二个参数Split()来忽略 EmptyLines。

        foreach (var line in File.ReadAllLines(path))
            {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());  
            }

解决方案2:您可以通过使用检查行是否为空String.IsNullOrWhiteSpace()

          foreach (var line in File.ReadAllLines(path))
            {
              if(!String.IsNullOrWhiteSpace(line))
              {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());  
              }                
            }
于 2014-02-17T09:09:35.897 回答
1

不久前我遇到了类似的问题。使用StringSplitOptions.RemoveEmptyEntries为我解决了它。于是就变成了,lines.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)

这很有帮助,因为您得到的是空行,因此RemoveEmptyEntries从 Split 方法的结果中删除了这些行。

于 2014-02-17T09:10:47.670 回答