C#动态调用Web服务的3种方法

C#调用WebService的方法 上面使用静态引用的方式调用WebService,但是这种方式有一个缺点:如果发布的WebService地址改变,那么就要重新添加WebService的引用。在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中如何调用WebService。1.我们在配置文件里面添加配置,把WebService的地址、WebService提供的类名、要调用的方法名称,都写在配置文件里面。这种方式是通过添加静态引用的方式调用WebService。 阅读详情

我们在开发C# WinForm时,有时会调用Web服务,服务是本地的当前好办,只要在Project中的Web References中引入就可以在代码中直接创建一个Web服务对象来引用,其实其原理是C#帮你自动创建客户端代理类的方式调用WebService,但如果调用的服务是动态的,比如说在几个IIS中都有相同的一个服务,在运行时输入具体的IP才确定调用哪个服务,那要怎么样实现呢。

C#动态调用Web服务方法一: 手动的添加一个Web引用,然后修改下本地的代理类。最后实现Web Service的URI部署到配置文件里。 具体做法如下:

以下代码是显示如何配置动态的Web Service,以服务单元C(类名为Web_SVSGC)为例:

(1)首先在Web引用中的本地代理类中添加一个构造函数,这个构造函数是以Web Service的URL为参数的重载方法。

复制  保存

   
  1. Namespace Web_SVSGC  
  2.     '< remarks/> 
  3.     < System.Diagnostics.DebuggerStepThroughAttribute(),  _     System.ComponentModel.DesignerCategoryAttribute("code"),  _     System.Web.Services.WebServiceBindingAttribute(Name:="SVSGCSoap", [Namespace]:="http://tempuri.org/QYJSERVICE/SVSGC"),  _     System.Xml.Serialization.XmlIncludeAttribute(GetType(Attribute))>  _  
  4.     Public Class SVSGC  
  5.         Inherits System.Web.Services.Protocols.SoapHttpClientProtocol  
  6.     '< remarks/> 
  7.         Public Sub New()  
  8.             MyBase.New  
  9.             Me.Url = "http://localhost/QYJSERVICE/WEBSERVICE/SERVICE/SVSGC.asmx" 
  10.         End Sub  
  11.  
  12.         '添加一个带参数的构造函数。  
  13.         Public Sub New(ByVal strUrl As String)   
  14.             MyBase.New()   
  15.             Me.Url = strUrl   
  16.         End Sub  
  17.  

(2)将Web Service的url配置在调用Web Service的应用程序的配置文件中。(其中的value可以随时修改。)

复制  保存

   
  1. < configuration> 
  2.     < appSettings> 
  3.               < add key="SVSGA_URL" value="http://192.168.108.188/ QDN/SERVICE/SVSGA.asmx" QDN/SERVICE/SVSGA.asmx" /> 
  4.     < /appSettings> 
  5. < /configuration>< configuration> 
  6.     < appSettings> 
  7.               < add key="SVSGA_URL" value="http://192.168.108.188/ QDN/SERVICE/SVSGA.asmx" QDN/SERVICE/SVSGA.asmx" /> 
  8.     < /appSettings> 
  9. < /configuration> 

(3)调用时,根据配置文件的Url动态的生成Web Service。

复制  保存       

   
  1. '要调用的Web Service的URL  
  2.         Dim strWebSvsUrl As String  
  3.         '声明一个要调用的Web Service  
  4.         Dim objSVSGC As WebSvs_GC. SVSGC  
  5.         '调用Web Service的远程方法的返回值  
  6.         Dim strReturnValue As String  
  7.         Try  
  8.             '从配置文件中取得Web Service的URL  
  9.             strWebSvsUrl = _   
  10.             System.Configuration.ConfigurationSettings.AppSettings("SVSGC_URL")   
  11.             '生成一个Web Service实例  
  12.             objSVSGC = New WebSvs_GC.SVSGC (strWebSvsUrl)  
  13.             '调用这个Web Service里的远程方法  
  14.             strReturnValue = objSVSGC.HelloWorld()  
  15.         Catch ex As Exception  
  16.         End Try 

C#动态调用Web服务方法二:完全动态处理,传入服务服务网址,方法名和参数即可.

   
  1. using System;   
  2. using System.Net;   
  3. using System.IO;   
  4. using System.CodeDom;   
  5. using Microsoft.CSharp;   
  6. using System.CodeDom.Compiler;   
  7. using System.Web.Services.Description;   
  8. using System.Web.Services.Protocols;   
  9.  
  10. namespace HB.Common   
  11. {   
  12.     /* 调用方式   
  13.      *   string url = "http://www.webservicex.net/globalweather.asmx" ;   
  14.      *   string[] args = new string[2] ;   
  15.      *   args[0] = "Hangzhou";   
  16.      *   args[1] = "China" ;   
  17.      *   object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;   
  18.      *   Response.Write(result.ToString());   
  19.      */   
  20.     public class WebServiceHelper   
  21.     {   
  22.         #region InvokeWebService   
  23.         /// < summary>   
  24.         /// 动态调用web服务   
  25.         /// < /summary>   
  26.         /// < param name="url">WSDL服务地址< /param>   
  27.         /// < param name="methodname">方法名< /param>   
  28.         /// < param name="args">参数< /param>   
  29.         /// < returns>< /returns>   
  30.         public static object InvokeWebService(string url, string methodname, object[] args)   
  31.         {   
  32.             return WebServiceHelper.InvokeWebService(url, null, methodname, args);   
  33.         }   
  34.  
  35.         /// < summary>   
  36.         /// 动态调用web服务   
  37.         /// < /summary>   
  38.         /// < param name="url">WSDL服务地址< /param>   
  39.         /// < param name="classname">类名< /param>   
  40.         /// < param name="methodname">方法名< /param>   
  41.         /// < param name="args">参数< /param>   
  42.         /// < returns>< /returns>   
  43.         public static object InvokeWebService(string url, string classname, string methodname, object[] args)   
  44.         {   
  45.             string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";   
  46.             if ((classname == null) || (classname == ""))   
  47.             {   
  48.                 classname = WebServiceHelper.GetWsClassName(url);   
  49.             }   
  50.  
  51.             try   
  52.             {   
  53.                 //获取WSDL   
  54.                 WebClient wc = new WebClient();   
  55.                 Stream stream = wc.OpenRead(url + "?WSDL");   
  56.                 ServiceDescription sd = ServiceDescription.Read(stream);   
  57.                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();   
  58.                 sdi.AddServiceDescription(sd, "", "");   
  59.                 CodeNamespace cn = new CodeNamespace(@namespace);   
  60.  
  61.                 //生成客户端代理类代码   
  62.                 CodeCompileUnit ccu = new CodeCompileUnit();   
  63.                 ccu.Namespaces.Add(cn);   
  64.                 sdi.Import(cn, ccu);   
  65.                 CSharpCodeProvider icc = new CSharpCodeProvider();   
  66.  
  67.                 //设定编译参数   
  68.                 CompilerParameters cplist = new CompilerParameters();   
  69.                 cplist.GenerateExecutable = false;   
  70.                 cplist.GenerateInMemory = true;   
  71.                 cplist.ReferencedAssemblies.Add("System.dll");   
  72.                 cplist.ReferencedAssemblies.Add("System.XML.dll");   
  73.                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");   
  74.                 cplist.ReferencedAssemblies.Add("System.Data.dll");   
  75.  
  76.                 //编译代理类   
  77.                 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);   
  78.                 if (true == cr.Errors.HasErrors)   
  79.                 {   
  80.                     System.Text.StringBuilder sb = new System.Text.StringBuilder();   
  81.                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)   
  82.                     {   
  83.                         sb.Append(ce.ToString());   
  84.                         sb.Append(System.Environment.NewLine);   
  85.                     }   
  86.                     throw new Exception(sb.ToString());   
  87.                 }   
  88.  
  89.                 //生成代理实例,并调用方法   
  90.                 System.Reflection.Assembly assembly = cr.CompiledAssembly;   
  91.                 Type t = assembly.GetType(@namespace + "." + classname, true, true);   
  92.                 object obj = Activator.CreateInstance(t);   
  93.                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);   
  94.  
  95.                 return mi.Invoke(obj, args);   
  96.  
  97.                 /*   
  98.                 PropertyInfo propertyInfo = type.GetProperty(propertyname);   
  99.                 return propertyInfo.GetValue(obj, null);   
  100.                 */   
  101.             }   
  102.             catch (Exception ex)   
  103.             {   
  104.                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));   
  105.             }   
  106.         }   
  107.  
  108.         private static string GetWsClassName(string wsUrl)   
  109.         {   
  110.             string[] parts = wsUrl.Split('/');   
  111.             string[] pps = parts[parts.Length - 1].Split('.');   
  112.  
  113.             return pps[0];   
  114.         }   
  115.         #endregion   
  116.     }   
  117. } 

返回时如果不是字符串,即强制转换,如返回是DataSet,则

   
  1. string url = "http://www.webservicex.net/globalweather.asmx" ;   
  2. string[] args = new string[2] ;   
  3. args[0] = "Hangzhou";   
  4. args[1] = "China" ;   
  5. object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;   
  6. DataSet DSRe=(DataSet)result;  

C#动态调用Web服务方法三:URL Behavior 属性

如果知道服务的方法和参数,只是调用的URL网址会随时变化,那么可以手工创建一个服务,添加上对应的的方法和传入参数,然后引入到项目中,就可以直接开发,在创建服务的实例化时,才修改对应的URL即可.

例如服务中有个方法叫GetTax,那么就可以这样改:

   
  1. GetTax.GetTax GetTax1 = new GetTax.GetTax();   
  2. GetTax1.Url = "http://" + WebIp1 + "/pub_wa_gspsp1/gettax.asmx";        //动态引入服务器   
  3.                    
  4. DataSet DS1 = GetTax1.GetTaxMx(Bm1, OldBz, Fpl, SLx, StaDa, EndDa);   //调用服务器返回开票数据 
C# 动态调用WebService C#调用WebService的方式有很多: 1、直接在项目中添加WebService引用,这种方式比较简单,但不是动态的,即每次服务地址或者内容变了之后都要重新添加引用。 2、用Soap,这种方式需要把添加WebService引用生成的Reference.cs类中服务接口,集成进自己定义的服务调用类中,如果服务 接口发生了改变,需要修改服务调用类。 3、动态调用,代码如下: 阅读详情

相关推荐

C#_.NET_动态调用webservice的三种方式

C#_.NET_动态调用webservice的三种方式

C# 动态获取webservice的接口

【代码】C# 动态获取webservice的接口。

ilikehigame的专栏 1113

C#动态webservice调用接口

C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using System.Text; using System.Xml; using System.Xml.Serialization; namespace Hishop.Plugins { /// /// 利用WebRequest/WebResponse进行WebService调用的类 /// public class WebServiceCall

C#动态调用webServices(详细)

首先我们要创建好webServices项目文件可以是一个项目也可以是一个单独的文件放在其他项目中。 创建webServices文件的头部,引用依赖将AJAX调用此web服务取消注释。 /// <summary> /// AddApplyOnline 的摘要说明 /// </summary> [WebService(Namespace = "http://192.168.1.48:3030/AddApplyOnline.asmx/AddApplyOnlin

Y2210151431的博客 4657

Latext安装(二)WinEdt安装+激活教程

常用的LaTeX安装方案是TeXLive + WinEdt。TeXLive 是LaTeX的运行环境,而 WinEdt 则是编写LaTeX文档的编辑器。安装好TexLive之后,再安装WinEdt。这里介绍的是WinEdt的安装和激活教程。

j_l_sheng的博客 4万+

计算机三级考试嵌入式系统重点精简汇总

第一章 嵌入式系统概论 1.1.1嵌入式系统 (1)特点:(1)专用性;(2)隐蔽性;(3)资源受限;(4)高可靠性;(5)实时性;(6)软件固化。 (2)应用:(1)冰箱、洗衣机、电饭煲、空调、摄影机、DVD播放器等;(2)数控机床、飞机、汽车、电梯、X光机、血压计等;(3)电话机、传真机、打印机、pos机、GPS导航仪、路由器等。 1.1.2嵌入式系统组成与分类 (1)逻辑组成:硬件和软件,分为处理器、存储器、I|0设备与I|O接口,数据总线; (2)处理器:分为主处理器(CPU)和协处

chemkrik的博客 1万+

C#如何动态调用Web服务

  我们在开发C# WinForm时,有时会调用Web服务,服务是本地的当前好办,只要在Project中的Web References中引入就可以在代码中直接创建一个Web服务对象来引用,其实其原理是C#帮你自动创建客户端代理类的方式调用WebService,但如果调用的服务是动态的,比如说在几个IIS中都有相同的一个服务,在运行时输入具体的IP才确定调用哪个服务,那要怎么样实现呢。方法一

同一天空下 5167

别再死记硬背了!用LTspice仿真,5分钟搞懂MOS管SOA曲线到底怎么用

本文通过LTspice仿真详细解析MOS管SOA曲线的应用方法,帮助工程师快速掌握安全操作区域的验证技巧。从理论到实战,涵盖导通损耗、开关损耗等关键参数分析,提供完整的六步验证流程和Buck电路案例,助力硬件设计更可靠高效。

weixin_30517215的博客 361

C# Winform动态调用Web Service

写项目的时候,忽然遇到一个小问题,在Winform里需要调用Web Service(以前都是调用WCF的,封装好的DLL就可以,比较简单) 上网了解了一下,找到一位师兄写的 http://www.csharpwin.com/csharpspace/10261r1810.shtml(这个地址不知道是转帖还是原创),他采用了动态编译的方式。 我把代码套上试验了一下,能用是肯定的,但是貌似Win

成长的步伐 5523

Windows域账户无管理员权限也能装软件?3步搞定UAC设置(附安全提醒)

本文探讨了在Windows域环境中,如何通过配置用户账户控制(UAC)实现临时提权,让无管理员权限的域账户也能安装软件。文章详细介绍了三步走策略:临时加入本地管理员组、调整UAC行为以及至关重要的权限回收,并提供了脚本化部署方案与安全实践指南,旨在平衡企业IT管理的效率与安全。

fox11的博客 768

三菱MC协议(MC/TCP):避坑指南|C#实现三菱MC协议:软元件(X/Y/M/D)读写与帧格式解析

核心避坑逻辑:MC协议的坑80%集中在“PLC配置”“地址编码”“字节序”,先解决这三点,通信就成功了一半;实战优先级:新手优先用开源库(Mitsubishi.MCProtocol),避免手动拼接帧格式的复杂坑;进阶再研究帧格式解析;排坑技巧:先Ping通→用调试助手验证→写C#代码,分层排查,不要混在一起找问题。掌握这些避坑技巧后,你可以轻松实现C#与三菱全系列PLC的稳定通信,满足工业场景中数据采集、设备控制等核心需求。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 2216

C#动态调用web服务 远程调用技术WebService

一、课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上。所以在本次分享课开课之前,我们要“约法三章”不谈论以下几个比较“严肃”和“敏感”的话题:WebService已经过时了啦,学习它干什么用啊!为什么要用WebService,而不用基于当前流行的RestFul ASP.NET WebAPI ? 对于上面的问题阿笨的回答很简单:因为它就是它,不一样烟火的WebService! 本.

cao919的专栏Net 2500

Oracle数据库表空间使用情况查询

在Oracle数据库中,表空间(Tablespace)是用于存储表、索引和其他数据库对象的逻辑存储单元。了解表空间的使用情况对于数据库管理员和开发人员来说是非常重要的,因为它可以帮助他们监控数据库的空间使用情况并进行容量规划。本文将介绍如何查询Oracle数据库中的表空间使用情况。通过执行以上SQL查询语句,你可以获得Oracle数据库中表空间的使用情况、文件大小和表空间大小等信息。这些信息可以帮助你了解数据库的存储情况,并进行适当的优化和容量规划。查看表空间的数据文件和表空间大小。查看表空间的使用情况。

CsmbPascal的博客 866

C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用...

  1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL。这个比较常见也很简单 即有完整的webservice文件目录如下图所示, 也可以在本地IIS根据webservice文件目录新发布一个webservice,然后程序动态调用,修改Url public new string Url { set; ...

weixin_33849215的博客 5145

详解Xilinx FPGA高速串行收发器GTX/GTP(1)--SerDes和GTX的关系

详解Xilinx FPGA高速串行收发器GTX/GTP(1)--SerDes和GTX的关系

孤独的单刀 4364

SoC 内的 CPU、GPU、DSP、NPU 协同执行结构图解析:多异构协同的最新实战体系

随着移动端 AI 能力迅速演进,SoC(System on Chip)内部的多种异构计算单元(CPU、GPU、DSP、NPU)之间的协同执行能力成为衡量芯片智能算力的关键指标。本文基于截至 2025 年 5 月最新主流国产芯片(如海思昇腾、地平线旭日、联发科天玑、高通骁龙 8 Gen 系、寒武纪思元等)在实际落地应用中的公开结构与技术资料,深入解析 CPU-GPU-DSP-NPU 协同执行的体系结构、执行路径与调度逻辑。文章结合 Android NNAPI、Vulkan Compute、自定义调度器等工程实

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 8718

C#中Winform动态调用Webservice的方法

一般情况下winform调用webservice时步骤 1添加服务引用---高级----添加web引用------填写url--添加web引用即可完成对webservice的引用 让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务。 如果需要动态调用WebService,要实现这样的功能: publicstaticobjectInvokeWebService(stringur

xinxin_cheer的专栏 4790

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

C#编程,调用(静态、动态) Web Service 的一种方法。

  WebService 的调用分为静态调用和动态调用两种。 静态:根据写死的服务地址调用服务。 动态:根据传入的不同地址调用对应的服务。 1、静态调用 静态调用的方式是通过“Add Service Reference...”创建客户端代理类。 这种方式让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务。 这样是使工作简单了,但是却将提供Web服务的URL、方法名、...

君一席 2994

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

上一篇: 使用SqlDependency时提示当前数据库未启用Service broker
下一篇: js 常用类
andycode
博客等级 码龄17年 12粉丝 80原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址