Question

I know that there are more threads on this topic here, but none of them actually helped me.

I will provide whole code:

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();
        }
    }
}

Here is what I am trying to load to double[][] input:

166 163 180 228

165 162 160 226

166 163 180 228

166 164 180 228

171 162 111 225

Here is what I am trying to load to double[][] output:

1 0 0

1 0 0

0 1 0

0 1 0

1 0 0
Was it helpful?

Solution

Problem : You have the Extra EmptyLines after every Line in your text file. when Split() function encounters a new Line it returns as it is becaue there is nothing to split and Add() function throws exception as empty line is not a valid Double.

Solution1 : You can Use StringSplitOptions.RemoveEmptyEntries as second argument to Split() function to ignore the EmptyLines.

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

Solution 2: you can check wether line is Empty or not by using 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());  
              }                
            }

OTHER TIPS

I had a similar problem not long ago. Using StringSplitOptions.RemoveEmptyEntries solved it for me. So it becomes, lines.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)

It helps because you are getting empty lines, so RemoveEmptyEntries removes those lines from the result of the Split method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top