示例#1
0
      static void saveFFLayer(string dir, FeedForwardLayer layer)
      {
          Directory.CreateDirectory(dir);
          //make infoFile
          string linearityType = "";

          if (layer._f is LinearUnit)
          {
              linearityType = "LinearUnit";
          }
          if (layer._f is RectifiedLinearUnit)
          {
              linearityType = "RectifiedLinearUnit";
          }
          if (layer._f is SigmoidUnit)
          {
              linearityType = "SigmoidUnit";
          }
          if (layer._f is SineUnit)
          {
              linearityType = "SineUnit";
          }
          if (layer._f is TanhUnit)
          {
              linearityType = "TanhUnit";
          }
          using (StreamWriter sw = new StreamWriter(Path.Combine(dir, "info.csv")))
          {
              sw.WriteLine(linearityType);
          }
          //saving matrixes
          saveMatrix(Path.Combine(dir, "W.csv"), layer._w);
          saveMatrix(Path.Combine(dir, "B.csv"), layer._b);
      }
        public void XORNetworkTest()
        {
            Matrix inputMatrix00 = new Matrix(new double[, ]
            {
                { 0.0 },
                { 0.0 },
            });

            FeedForwardLayer inputLayer  = new FeedForwardLayer(inputMatrix00);
            FeedForwardLayer hiddenLayer = new FeedForwardLayer(inputLayer, 2, new BinaryStepActivation());
            FeedForwardLayer outputLayer = new FeedForwardLayer(hiddenLayer, 1, new BinaryStepActivation());

            hiddenLayer.Weights = new Matrix(new double[, ]
            {
                { 1.0, 1.0 },
                { -1.0, -1.0 }
            });
            hiddenLayer.Bias = new Matrix(new double[, ]
            {
                { -0.5 },
                { 1.5 }
            });
            outputLayer.Weights = new Matrix(new double[, ]
            {
                { 1.0, 1.0 }
            });
            outputLayer.Bias = new Matrix(new double[, ]
            {
                { -1.5 }
            });
            Matrix outputMatrix00 = outputLayer.Compute();

            Matrix inputMatrix01 = new Matrix(new double[, ]
            {
                { 0.0 },
                { 1.0 },
            });

            inputLayer.Weights = inputMatrix01;
            Matrix outputMatrix01 = outputLayer.Compute();

            Matrix inputMatrix10 = new Matrix(new double[, ]
            {
                { 1.0 },
                { 0.0 },
            });

            inputLayer.Weights = inputMatrix01;
            Matrix outputMatrix10 = outputLayer.Compute();

            Matrix inputMatrix11 = new Matrix(new double[, ]
            {
                { 1.0 },
                { 1.0 },
            });

            inputLayer.Weights = inputMatrix11;
            Matrix outputMatrix11 = outputLayer.Compute();

            Assert.AreEqual(-1, outputMatrix00[0, 0]);
            Assert.AreEqual(1, outputMatrix10[0, 0]);
            Assert.AreEqual(1, outputMatrix01[0, 0]);
            Assert.AreEqual(-1, outputMatrix11[0, 0]);
        }