Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            //if (args.length < 5)
            //{
            //    System.out
            //            .println("Usage: \n\t-train trainfile\n\t-test predictfile\n\t-sep separator, default:','\n\t-eta eta, default:0.5\n\t-iter iternum, default:5000\n\t-out outputfile");
            //    return;
            //}
            String trainfile  = @".\data\train.txt";
            String testfile   = @".\data\test.txt";
            String outputfile = "outputfile.txt";
            float  eta        = 0.05f;
            int    nIter      = 1000;
            List <CnnDemo.BP.DataNode> trainList = GetDataList(trainfile);
            List <CnnDemo.BP.DataNode> testList  = GetDataList(testfile);
            StreamWriter sw        = new StreamWriter(outputfile);
            int          typeCount = 3;
            Cnn          tmpCnn    = new Cnn();

            tmpCnn.AddCnnFullLayer(4, 14, CnnNode.ActivationFunctionTypes.Tanh, false);
            tmpCnn.AddCnnFullLayer(20, CnnNode.ActivationFunctionTypes.Tanh, false);
            tmpCnn.AddCnnFullLayer(10, CnnNode.ActivationFunctionTypes.Tanh, false);
            tmpCnn.AddCnnFullLayer(10, CnnNode.ActivationFunctionTypes.Tanh, false);
            tmpCnn.AddCnnFullLayer(3, CnnNode.ActivationFunctionTypes.Tanh, false);
            for (int i = 0; i < nIter; i++)
            {
                foreach (var t in trainList)
                {
                    tmpCnn.TrainFullLayer(t.getAttribList2().ToArray(), t.getTypes(), eta);
                }
            }
            for (int i = 0; i < testList.Count(); i++)
            {
                CnnDemo.BP.DataNode test    = testList[i];
                double[]            type    = tmpCnn.PredictFullLayer(test.getAttribList2().ToArray());
                List <float>        attribs = test.getAttribList();
                for (int n = 0; n < attribs.Count(); n++)
                {
                    sw.Write(attribs[n] + ",");
                }
                double maxtype = type[0], max = 0;
                for (int n = 0; n < 3; n++)
                {
                    if (maxtype < type[n])
                    {
                        max     = n;
                        maxtype = type[n];
                    }
                }
                sw.WriteLine(GetTypeName(max));
            }
            sw.Close();
        }
Exemplo n.º 2
0
        public int test(DataNode dn)
        {
            forward(dn.getAttribList());
            float result = 2;
            int   type   = 0;

            // 取最接近1的
            for (int i = 0; i < mOutputCount; i++)
            {
                if ((1 - mOutputNodes[i].getForwardOutputValue()) < result)
                {
                    result = 1 - mOutputNodes[i].getForwardOutputValue();
                    type   = i;
                }
            }
            return(type);
        }