Example #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();
        }
Example #2
0
        private List <CnnDemo.BP.DataNode> GetDataList(string path)
        {
            List <CnnDemo.BP.DataNode> tmpListDataNode = new List <BP.DataNode>();
            StreamReader sr   = new StreamReader(path);
            string       line = sr.ReadLine();

            while (!String.IsNullOrEmpty(line))
            {
                string[]            values = line.Split(',');
                CnnDemo.BP.DataNode data   = new BP.DataNode();
                for (int i = 0; i < values.Length; i++)
                {
                    if (i < values.Length - 1)
                    {
                        data.addAttrib(Convert.ToSingle(values[i]));
                    }
                    else
                    {
                        switch (values[i])
                        {
                        case "Iris-versicolor":
                            data.setType(0);
                            break;

                        case "Iris-setosa":
                            data.setType(1);
                            break;

                        case "Iris-virginica":
                            data.setType(2);
                            break;
                        }
                    }
                }
                tmpListDataNode.Add(data);
                line = sr.ReadLine();
            }
            sr.Close();
            return(tmpListDataNode);
        }