1

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
2
  • 1
    Try rows.Add(line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray()); Commented Feb 17, 2014 at 9:00
  • Learn to use a debugger. Commented Feb 17, 2014 at 9:06

2 Answers 2

1

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());  
              }                
            }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.