Beispiel #1
0
 /// <summary>
 /// Creates a <see cref="SupportVectorMachine"/> from the specified file.
 /// </summary>
 /// <param name="fileName">A string that contains the name of the file from which to create the <see cref="SupportVectorMachine"/>.</param>
 /// <returns>The <see cref="SupportVectorMachine"/> this method creates.</returns>
 public static SupportVectorMachine FromFile(string fileName) => SupportVectorMachine.FromString(File.ReadAllText(fileName, Encoding.UTF8));
Beispiel #2
0
 /// <summary>
 /// Creates a <see cref="SupportVectorMachine"/> from the specified byte array.
 /// </summary>
 /// <param name="buffer">The buffer to read the <see cref="SupportVectorMachine"/> from.</param>
 /// <returns>The <see cref="SupportVectorMachine"/> this method creates.</returns>
 public static SupportVectorMachine FromMemory(byte[] buffer) => SupportVectorMachine.FromString(UTF8Encoding.UTF8.GetString(buffer));
        /// <summary>
        /// Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        /// <param name="trainer">The learning algorithm.</param>
        /// <param name="numberOfClasses">The number of classes.</param>
        /// <param name="x">The input vectors <paramref name="x"/>.</param>
        /// <param name="y">The expected binary output <paramref name="y"/>.</param>
        /// <param name="weights">The <c>weight</c> of importance for each input vector (if supported by the learning algorithm).</param>
        /// <param name="cancellationToken">The cancellationToken token used to notify the machine that the operation should be canceled.</param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="trainer"/> is <b>null</b>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="x"/> is <b>null</b>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="y"/> is <b>null</b>.</para>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <para><paramref name="numberOfClasses"/> is less than 2.</para>
        /// <para>-or-</para>
        /// <para>The number of elements in <paramref name="y"/> does not match the number of elements in <paramref name="x"/>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="weights"/> is not <b>null</b> and the number of elements in <paramref name="weights"/> does not match the number of elements in <paramref name="x"/>.</para>
        /// </exception>
        /// <returns>
        /// The <see cref="OneVsAllSupportVectorMachine"/> learned by this method.
        /// A model that has learned how to produce <paramref name="y"/> given <paramref name="x"/>.
        /// </returns>
        public static OneVsAllSupportVectorMachine Learn(
            ISupportVectorMachineLearning trainer,
            int numberOfClasses,
            IList <float[]> x,
            IList <int> y,
            IList <float> weights,
            CancellationToken cancellationToken)
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }

            if (x == null)
            {
                throw new ArgumentNullException(nameof(x));
            }

            if (y == null)
            {
                throw new ArgumentNullException(nameof(y));
            }

            if (numberOfClasses < 2)
            {
                throw new ArgumentException("The machine must have at least two classes.", nameof(numberOfClasses));
            }

            if (y.Count != x.Count)
            {
                throw new ArgumentException("The number of output labels must match the number of input vectors.", nameof(y));
            }

            // create the machines
            SupportVectorMachine[] machines = new SupportVectorMachine[numberOfClasses];

            // train each machine
            int sampleCount = x.Count;

            CommonParallel.For(
                0,
                machines.Length,
                (a, b) =>
            {
                for (int i = a; i < b; i++)
                {
                    bool[] expected = new bool[sampleCount];
                    for (int j = 0; j < sampleCount; j++)
                    {
                        expected[j] = y[j] == i;
                    }

                    machines[i] = SupportVectorMachine.Learn(trainer, x, expected, weights, cancellationToken);
                }
            },
                new ParallelOptions()
            {
                CancellationToken = cancellationToken,
            });

            return(new OneVsAllSupportVectorMachine(machines));
        }