注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Bioinformatics home

 
 
 

日志

 
 

c# 调用神经网络  

2009-12-27 14:08:14|  分类: 生物信息编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

static void Main(string[] args)
  {
            try
            {
                Console.Write("\n");
                Console.Write("C# XOR Sample - only works if fannKernel has been started...\n");

                Console.Write("Connect to fannKernel\n");
                fannProxy.fannKernel fann = new fannProxy.fannKernel();
                Console.Write("Create a neural network context\n");
                int net = fann.CreateContext();

                Console.Write("Create the neural network and set its parameters\n");
                int[] layers = {2, 3, 1};
                fann.Create(net, fannProxy.NetworkType.Layer, layers, 1.0f);
                fann.SetLearningRate(net, 0.7f);
                fann.SetActivationSteepnessHidden(net, 1.0);
                fann.SetActivationSteepnessOutput(net, 1.0);
                fann.SetActivationFunctionHidden(net,  fannProxy.ActivationFunction.SigmoidSymmetricStepwise);
                fann.SetActivationFunctionOutput(net, fannProxy.ActivationFunction.SigmoidSymmetricStepwise);
                //fann.PrintParameters(net); // Displays the network parameters in fannKernel

                Console.Write("Read training data from the xor.train sample file\n");
                fann.ReadTrainFromFile(net, "xor.train");

                Console.Write("Initialize and train the neural network\n");
                fann.InitWeights(net);
                double[][] mse = fann.TrainOnData(net, 300, 30, 0.001f);

                Console.Write("Output the mean square error information\n");
                Console.Write("\tEpoch\t\tMSE\n");
                for (int i = 0; i < mse.Length; ++i)
                {
                    Console.Write("\t");
                    Console.Write(mse[i][0]);
                    Console.Write("\t\t");
                    Console.Write(mse[i][1]);
                    Console.Write("\n");
                }

                Console.Write("Execute the neural network on test data\n");
                double[] test0 = {-1, -1};
                double[] test1 = {-1, 1};
                double[] test2 = {1, -1};
                double[] test3 = {1, 1};
                double[][] data = {test0, test1, test2, test3};
                double[][] result = fann.RunData(net, data);

                Console.Write("Output the test data and results\n");
                Console.Write("\tData\t\tResult\n");
                for (int i = 0; i < result[0].Length; ++i)
                {
                    Console.Write("\t");
                    Console.Write("{0,2} xor {1,2}", data[i][0], data[i][1]);
                    Console.Write("\t");
                    Console.Write(result[0][i]);
                    Console.Write("\n");
                }

                Console.Write("Remove the neural network from fannKernel\n");
                fann.DestroyContext(net);
            }
            catch (System.Exception fault)
            {
                Console.Write("C# XOR Sample Error: " + fault.Message);
            }
  }

  评论这张
 
阅读(1920)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017