예제 #1
0
        public void FeedForwardNeuralNetwork_LoadModelDenseMatrix()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix <double>(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new ArrayMathMatrix <double>(i, j),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[-1.0, 0.0, 1.0, -0.5, 0.5]",
                new ArrayVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, ILongMathMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModel(
                model);
            this.AssertTargetFromMatrix(
                model,
                target);
        }
예제 #2
0
        public void FeedForwardNeuralNetwork_LoadModelComplexDenseMatrix()
        {
            var schema = new[] { 5L, 3L, 4L, 2L, 5L };
            var target = new FeedForwardNeuralNetwork <double>(
                schema);

            var model = this.GetComplexTestModel();

            target.LoadModel <CoordinateSparseMathMatrix <double>, ArrayMathVector <double> >(
                model);

            // Verificação do carregamento
            this.AssertTargetFromMatrix(
                model,
                target);
        }
예제 #3
0
        public void FeedFrowardNeuralNetwork_InternalComputeOutputs()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new SparseDictionaryMatrix <double>(i, j, 0),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[0.5, 0.5, 0.5, 0.5, 0.5]",
                new SparseDictionaryMathVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, SparseDictionaryMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModel(model);

            var outputMatrix = target.InternalReserveOutput();

            target.InternalComputeLayerOutputs(
                new ArrayMathVector <double>(new[] { 1.0, -1.0 }),
                outputMatrix,
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            });

            Assert.AreEqual(target.Schema.LongCount() - 1L, outputMatrix.LongLength);

            var currOut = outputMatrix[0];

            Assert.AreEqual(0.0, currOut[0]);
            Assert.AreEqual(1.0, currOut[1]);
            Assert.AreEqual(0.0, currOut[2]);

            currOut = outputMatrix[1];
            Assert.AreEqual(0.0, currOut[0]);
            Assert.AreEqual(0.0, currOut[1]);
        }
예제 #4
0
        public void FeedFrowardNeuralNetwork_RunSimpleMatrixTest()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new SparseDictionaryMatrix <double>(i, j, 0),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[0.5, 0.5, 0.5, 0.5, 0.5]",
                new SparseDictionaryMathVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, SparseDictionaryMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModel(model);

            var actual = target.Run(
                new[] { 1.0, 0.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            var expected = new[] { 0.0, 0.0 };

            CollectionAssert.AreEqual(expected, actual);

            actual = target.Run(
                new[] { 0.0, 1.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            expected = new[] { 0.0, 1.0 };

            CollectionAssert.AreEqual(expected, actual);

            actual = target.Run(
                new[] { 1.0, -1.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            expected = new[] { 0.0, 0.0 };

            CollectionAssert.AreEqual(expected, actual);
        }