In Accord.net, why does ResilientBackpropagationLearning.RunEpoch throw an IndexOutOfRangeException?

36 views Asked by At

I'm trying to get a basic example of Sentiment Analysis to work.
Why do I get a System.IndexOutOfRangeException: "Index was outside the bounds of the array." on teacher.RunEpoch(inputs, outputs);

How can this be fixed ?

namespace TestImapServer.ContentFilter
{

    using Accord;
    using Accord.MachineLearning;
    using Accord.Math;
    using Accord.IO;
    using Accord.Neuro;
    using Accord.Neuro.Learning;
    using Accord.Statistics.Models.Regression;

    using System.Linq;


    internal class SentimentWithAccordNet
    {

        public class SentimentData
        {
            public string Text { get; set; }
            public double Sentiment { get; set; }
        }

        public static void Test()
        {


            var sentence = "I love pizza. I hate the restaurant, though.";

            // Load sentiment analysis data
            var data = new System.Collections.Generic.List<SentimentData>();
            data.Add(new SentimentData { Text = "I love pizza", Sentiment = 1 });
            data.Add(new SentimentData { Text = "I hate the restaurant, though", Sentiment = -1 });

            // Create a bag of words
            var bow = new BagOfWords();
            bow.Learn(data.Select(x => x.Text).ToArray());

            // Create a matrix representation of the data
            var matrix = bow.Transform(data.Select(x => x.Text).ToArray());

            // Train a neural network
            var network = new ActivationNetwork(new SigmoidFunction(), bow.NumberOfInputs, 10, 1);
            var teacher = new ResilientBackpropagationLearning(network);
            double[][] inputs = matrix.ToJagged<double>();
            double[][] outputs = data.Select(x => new double[] { x.Sentiment }).ToArray();

            teacher.RunEpoch(inputs, outputs);


            // Analyze sentence
            // var input = bow.Transform(sentence).ToRow();
            // var predicted = network.Compute(input);
            double[] input = bow.Transform(new string[] { sentence });
            var predicted = network.Compute(input);

            // Print results
            System.Console.WriteLine($"Sentiment for food: {predicted[0]}");
            System.Console.WriteLine($"Sentiment for restaurant: {predicted[1]}");
        }


    }
}
0

There are 0 answers