缓存 SpringModules Cache ( spring 和 ehcache的整合 ) spring提供缓存bean方案

springmodules-cache.xsd&springmodules-ehcache.xsd.rar 解决web.xml中 UTF-8报错。错误提示: cvc-complex-type.2.4.a: Invalid content was found starting with element 'page-encoding'. One of '{"http:// java.sun.com/xml/ns/javaee":include-prelude, "http://java.sun.com/xml/ns/javaee":include-coda, "http://java.sun.com/ xml/ns/javaee":deferred-syntax-allowed-as-literal, "http://java.sun.com/xml/ns/javaee":trim-directive-whitespaces, "http://java.sun.com/xml/ns/javaee":default-content-type, "http://java.sun.com/xml/ns/javaee":buffer, "http:// java.sun.com/xml/ns/javaee":error-on-undeclared-namespace}' is expected. 立即下载

下面格式整理有些混乱,spring3.1如何使用cache 缓存请参照:spring cache http://blog.csdn.net/partner4java/article/details/8600666

引自:<SPRING IN ACTION 2>

 

 

      Spring程序有一种更优雅的缓存解决方案。Spring Modules项目(http://springmodules.dev.java.net)通过切面提供了缓存,它把通知应用于Bean方法来透明地对其结果进行缓存,而不是明确地指定要被缓存的方法。

     Spring Modules对于缓存的支持涉及到一个代理,它拦截对Spring管理的Bean一个或多个的调用。当一个被代理的方法被调用时,Spring Modules Cache首先查阅一个缓存来判断这个方法是否已经被使用同样参数调用过,如果是,它会返回缓存里的值,实际的方法并不会被调用;否则,实际方法会被调用,其返回值会被保存到缓存里,以备方法下一次被调用时使用。

     虽然Spring Modules会提供一个代理来拦截方法并把结果保存到缓存,它并没有提供一个实际的缓存解决方案,而是要依赖于第三方的缓存方案。可以使用的方案有多个,包括:

 EHCache

 GigaSpaces

 JBoss Cache

 JCS

 OpenSymphony 的 OSCache

 Tangosol 的 Coherence

 我们为RoadRantz程序选择EHCache,主要是因为我以前使用它的经验及能够从www.ibibio.org的Maven仓库轻易获得。无论使用哪个缓存方案,对于Spring Modules Cache的配置基本上都是一样的。

  首先要做的是新建一个Spring配置文件来声明缓存。虽然可以把Spring Modules Cache配置放到RoadRantz程序加载的任意一个Spring上下文配置文件里,但最好还是把他们分开,所以我们要创建roadrantz-cache.xml来保存缓存的配置。

  与Spring上下文配置文件一样,randrantz-cache.xml也以<beans>元素为根。但为了利用Spring Modules 对EHCache的支持,我们要让<beans>元素能够识别ehcache命名空间:

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:ehcache="http://www.springframework.org/schema/ehcache"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/ehcache  http://www.springframework.org/schema/cache/springmodules-ehcache.xsd">
 

  我们为RoadRantz程序选择的是EhCache,如果想使用其他缓存方案,需要把Spring Modules命名究竟和规划声明修改为相应的内容。

  URI和规划URI

 

 命名空间 命名空间URI 规划URI
 ehcache http://www.springframework.org/schema/ehcache http://www.springframework.org/schema/cache/springmodules-ehcache.xsd
 gigaspaces http://www.springframework.org/schema/gigaspaces http://www.springframework.org/schema/springmodules-gigaspaces.xsd
 jboss http://www.springframework.org/schema/jboss http://www.springframework.org/schema/springmodules-jboss.xsd
 jcs http://www.springframework.org/schema/jcs http://www.springframework.org/schema/springmodules-jcs.xsd
 oscache http://www.springframework.org/schema/oscache http://www.springframework.org/schema/springmodules-oscache.xsd
 tangosol http://www.springframework.org/schema/tangosol http://www.springframework.org/schema/springmodules-tangosol.xsd

 

  无论选择哪种缓存,都可以使用一些Spring配置元素在Sping里对缓存进行配置。

Spring Modules的配置元素

 配置元素 用途
 <namespace:annotations> 以Java5注解来声明被缓存的方式
 <namespace:commons-attributes> 以Jakarta通用属性元素数据来声明被缓存的方式
<namespace:config> 在Spring XML里配置缓存方案 
<namespace:proxy> 在Spring XML里声明一个代理来声明被缓存的方式 

 

 在使用EHCache作为缓存方案时,需要告诉Spring到哪里寻找EHCache配置文件,这正是<ehcache:config>元素的用途所在:

<ehcache:config configLocation="classpath:ehcache.xml">

在此对configLocation属性的设置告诉Spring从程序类路径的根位置加载EHCache的配置。

 

配置EHCache

 

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires.
                            i.e. The maximum amount of time between accesses before an element expires
                            Is only used if the element is not eternal.
                            Optional attribute. A value of 0 means that an Element can idle for infinity
        timeToLiveSeconds - Sets the time to live for an element before it expires.
                            i.e. The maximum time between creation time and when an element expires.
                            Is only used if the element is not eternal.
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
       
    <cache name="rantzCache"
        maxElementsInMemory="300"
        eternal="false"
        timeToIdleSeconds="500"
        timeToLiveSeconds="500"
        overflowToDisk="true"
        />
       
</ehcache>

 


<defulatCache>元素是必须的,描述了在没有找到其他缓存情况下所使用的缓存。<cache>缓存定义了另一个缓存,可以在ehcache.xml里出现0次或多次(每次针对定义的一个缓存) 

 

EHCache的缓存配置属性

 属性 用于指定
 diskExpiryThreadIntervalSeconds 磁盘过期线程运行的频率(以秒为单位),也就是磁盘持久的缓存清理过期项目的频率(默认是120秒)
 diskPersistent 磁盘缓存在VM重新启动时是否保持(默认为false)
 eternal元素是否永恒。如果是永恒的,就永远不会过期(必须设置) 
maxElementsInMemory 内存能够被缓存的最大元素数量(必须设置) 
memoryStoreEvictionPolicy 当达到maxElementsInMemory时,如何强制进行驱逐。默认使用“最近使用(LRU)”策略,还可以使用“先入先出(FIFO)”和“较少使用(LFU)”策略。(默认是LRU) 
name 缓存的名称(对于<cache>必须设置) 
 overflowToDisk当内存缓存达到maxElementsInMemory时,是否可以溢出到磁盘(必须设置) 
timeToIdleSeconds 导致元素过期的访问间接(以秒为单位)。设置为0标识元素可以永远空闲(默认值为0) 
timeToLiveSeconds 元素在缓存里可以存在的时间(以秒为单位)。设置为0标识元素可以在缓存里永远存在而不过期(默认值是0) 

 

     对于RoadRantz程序,我们配置了一个默认缓存(这是EHCache要求的),还配置了一个名为rantzCache的缓存作为主缓存。两个缓存都设置为最多可以容纳500个元素(不过期),访问频率最低的元素会被踢出,不允许磁盘溢出。

    在Spring程序上下文里配置的EHCache之后,就可以声明哪个Bean和方法应该对结果进行缓存。首先,我们来声明一个代理来缓存RoadRantz DAO层里方法的返回值。

 

 

 缓存的代理Bean

 

 我们已经知道HibernateRantDao里的getRantsForDay()方法很适合进行缓存。再回到Spring上下文定义,我们要使用<ehcache:proxy>元素把一个代理包裹到HibernateRantDao,从而缓存从getRantsForDay()返回的全部内容:

<ehcache:proxy id="rantDao" refId="rantDaoTarget">

   <ehcache:caching methodName="getRantsForDay" cacheName="rantzCache"/>

</ehcache:proxy>

 

<ehcache:caching>元素声明哪个方法要被拦截、其返回值要保存到哪个缓存。本例中,methodName被设置为getRantsForDay(),要使用的缓存是rantzCache。

我们可以根据需要在<ehcache:proxy>里声明多个<ehcache:caching>来描述Bean方法的缓存。我们可以让一个<ehcache:caching>用于所有被缓存的方法,也可以使用通配符为一个<ehcache:caching>元素指定多个方法。比如下面的<ehcache:caching>元素会代理缓存全部名称由get开头的方法:

<ehcache:caching method="get*" cacheName="rantzCache"/>

 把数据放到缓存里只完成了一半的工作。在经过一段时间之后,缓存里一定会包含大量数据,其中很多没有意义的数据。最后,这些数据应该被清理,数据缓存周期重新开始。

 

 刷新缓存

 

 <ehcache:caching>元素声明的是要向缓存中添加数据的方法,而<ehcache:fluching>元素声明了会清空缓存的方法。举例来说,假设我们想在saveRant()方法被调用时清空rantzCache缓存,那么就应该使用如下的<ehcache:flushing>元素:

<ehcache:flushing methodName="saveRant" cacheName="rantzCache"/>

在默认情况下,cacheName属性里指定的缓存会在methodName被调用之后清空,但利用when属性可以指定清空的时机:

<ehcache:flushing methodName="saveRant" cacheName="rantzCache" when="before"/>

把when属性设置为before可以让缓存在saveRant()被调用之前清空。

 

声明一个被代替的内部Bean

 注意<ehcache:proxy>的id和refld属性。由<ehcache:proxy>生成的代理的id是rantDao,然后这是HibernateRantDao Bean的id,因此,我们需要把这个真正的Bean重命名为rantDaoTarget。

如果觉得id/refId组合有些奇怪,我们还可以把目标Bean声明为<ehcache:proxy>的内部Bean:

<ehcache:proxy id="rantDao">

<bean class="....">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

<ehcahe:caching methodName="getRantsForDay" cacheName="rantzCache"/>

</ehcache:proxy>

即使使用了内部Bean,我们仍然需要为每个要代理的Bean声明一个<ehcache:proxy>元素,为方法声明一个或多个<ehcache:caching>元素。

 

 

 

这个东东对我最大的吸引是,不再像以前,紧紧是hibernate对ehcache的支持了。也就是说,现在不论我用任何形式,我只是针对service或者Dao进行缓存就OK了。更易用

 

Demo1:

 

 

Demo2:

创建配置文件:

ehcache2.xml

 bean-jdbc.xml

 

 beans-jdbc.xml

 

 一个用户我后面单元测试的基础类:

 

 

我的实体bean:

 

接口:

 

实现类:

我一般写完service,都会先测试一下,我的Junit:

 

我简单写了个jsp测试一下:

 

 

但是怎么测试呢?呵呵,我就是些了一个列表,然后打开,查看,比如现在十条数据,我打开了一个mysql管理工具,删了几条数据,刷新这个界面还是十条数据,没少。就验证了缓存这点。

 

还有另外一种不利用spring支持的xml配置标签和注解的形式,利用切面编程,改天再整理出来,不过,我还是很喜欢这种,所以第一时间就先搞的这个,但是有一点注意的是,包,这个利用的是springmodules项目,所以要单独引入springmodules相关的ehcahe,的却难找。还有,ehcache的核心包,和核心包需要的相关包。

 

spring-modules-cache-0.8.jar

ehcache-core-2.4.0.jar。。。

 

 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjrt.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjweaver.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/c3p0-0.9.1.2.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-dbcp-1.2.2.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-pool-1.3.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.7-bin.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr-2.7.6.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-2.2.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.1.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.9.0.GA.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jta-1.1.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.15.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.6.1.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-jdk14-1.6.1.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ehcache-core-2.4.0.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-modules-cache-0.8.jar"/>

 

 

 

 

 

 

springmodules-cache与springmodules-ehcache的xsd.rar springmodules-cache.xsd与springmodules-ehcache.xsd 解决pom.xml web.xml 等xml中的标签报错等问题 立即下载

相关推荐

spring-modules-cache

spring-modules-cache-0.8.jar

JDBC连接池、JDBCTemplate

JDBC连接池、JDBCTemplate 一、JDBC连接池 1、概念和好处 A.概念: 就是一个集合,集合当中存放有多个数据库的连接对象 Connection的对象.(可借可还) B.好处: a.节约系统资源 b.提升连接效率 2、常见的连接池 A.标准连接池: javax.sql.DataSource 通用的方法 a.getConnect...

weixin_43917392的博客 466

Arduino应用——PWM控制直流电机风扇

Arduino应用——PWM控制直流电机风扇PWM原理实验器件功能描述如何改变文本的样式插入链接与图片实现代码生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 最近在使用arduino UNO开发板完成课程小作业。其中一个要求设计一个电风扇控制系统,要求能够通过PWM调节风扇的转速。期间搜索了许多例程和方法,但受限于器件,效果不

hypecycle的博客 2万+

spring管理的ehcache缓存没有起做用的原因

Dao代码如下: @Repositorybr/>@EnableCachingclass UserDao(jdbcTemplate: JdbcTemplate) { @Cacheable(keyGenerator = "cacheKeyGenerator", cacheNames = Array("merInfo"))private def findByName(name: String):...

weixin_33753845的博客 265

手把手教你用手机号注册的Apple ID成功登录Developer账户(避坑指南)

本文详细解析了使用手机号注册的Apple ID登录开发者账户时常见的'不符合使用条件'报错问题,并提供了从添加可验证邮箱到设置主账号的四步解决方案。特别针对Apple Developer账户的验证机制,帮助开发者避免因邮箱缺失导致的登录失败,提升账户激活成功率。

z5a6b的博客 1252

【项目实战】功能安全目标有四个ASIL等级:A、B、C、D,ASIL D是最高的等级

功能安全是一种关注产品在异常情况下如何避免造成伤害的概念功能安全在汽车电子行业中非常重要。功能安全涉及到系统、电子和软件三个部分。(1)系统部分主要涉及概念设计(2)而具体的实施则是在电子和软件部分完成。

奶爸的编程之路,也就一周冷个三天 2839

Spring AOP根据JdbcTemplate方法名动态设置数据源

为什么80%的码农都做不了架构师?>>> ...

weixin_34010566的博客 354

ABAP Secure Storage 在事务码 SM59 中的应用

系统管理人员在进行维护时,应关注相关日志信息,及时发现可能出现的安全异常,并针对不同安全等级的操作制定相应的备份与恢复策略,以降低潜在风险。该区域在设计上具备高度隔离性,仅允许经过授权的组件访问。保存后,系统会将密码存储在 Secure Storage 中,这种处理方式充分体现了 SAP 标准程序在安全设计上的考量。对于系统管理员而言,这种机制在日常维护与审计中提供了有力的支持,可以通过比对存储数据的哈希值或其他完整性校验手段,及时发现异常变动,保障系统长期稳定运行。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 695

spring 和 ehcache的整合

<br />缓存 SpringModules Cache ( spring 和 ehcache的整合 ) spring提供缓存bean方案 收藏 <br />引自:<SPRING IN ACTION 2><br />      Spring程序有一种更优雅的缓存解决方案。Spring Modules项目(http://springmodules.dev.java.net)通过切面提供了缓存,它把通知应用于Bean方法来透明地对其结果进行缓存,而不是明确地指定要被缓存的方法。<br />     Spring

zhou_8181的专栏 1032

WPF实现圆形菜单动态展开折叠效果

实现效果如下: 思路:根据子菜单个数计算旋转角度动态生成子菜单,并设置展开折叠动画。 步骤: 1、自定义按钮MyButton,建立展示图像属性DisplayImage public class MyButton : Button { public static readonly DependencyProperty DisplayImageProperty = DependencyProperty.Register("DisplayImage", typeof(

dnazhd的博客 3154

YOLOv11改进策略【模型轻量化】| 替换骨干网络 CVPR-2024 RepViT 轻量级的Vision Transformers架构

本文记录的是基于RepVit的YOLOv11轻量化改进方法研究。RepVit的网络结构借鉴ViT的设计理念,通过分离的token mixe和减少推理时的计算和内存成本,同时减少扩展比率并增加宽度,降低延迟,并通过来弥补参数大幅减少的问题,提高了准确性。本文在替换骨干网络中配置了原论文中的和五种模型,以满足不同的需求。

Limiiiing的博客 2981

缓存 SpringModules Cache ( spring 和 ehcache的整合 ) spring提供缓存bean方案(转载)

<br />引自:<SPRING IN ACTION 2><br />      Spring程序有一种更优雅的缓存解决方案。Spring Modules项目(http://springmodules.dev.java.net)通过切面提供了缓存,它把通知应用于Bean方法来透明地对其结果进行缓存,而不是明确地指定要被缓存的方法。<br />     Spring Modules对于缓存的支持涉及到一个代理,它拦截对Spring管理的Bean一个或多个的调用。当一个被代理的方法被调用时,Spring Mod

wangxifeng920的专栏 1966

cartographer运行错误集合

播放bag的时候错误 Check failed: sensor_to_tracking->translation().norm() < 1e-5 The IMU frame must be colocated with the tracking frame. Transforming linear acceleration into the tracking frame will otherwise be imprecise tracking frame不是imu的link 修改lua文件中的.

Studying…… 2039

缓存 SpringModules Cache ( spring 和 ehcache的整合 )

spring提供缓存bean方案 springbeancachecachingpath 下面格式整理有些混乱,spring3.1如何使用cache 缓存请参照:spring cache http://blog.csdn.net/partner4java/article/details/8600666 引自:<SPRING IN ACTION 2> ...

dianti5444的博客 164

SAP Fiori Elements 扩展实战:UI 层扩展、OData 服务扩展、CDS 视图增强及全流程案例

UI 层扩展主要关注在不修改标准生成界面的基础上,自定义用户界面元素和交互逻辑。典型需求包括:在现有 Fiori Elements 界面上添加新的字段(如表格列或表单项)、增加自定义按钮并绑定操作、以及通过控制器扩展加入事件处理等。无论在 RAP 还是传统 BOPF 架构下,前端 UI 扩展通常采用 SAPUI5 提供的**扩展点(Extension Points)和控制器扩展(Controller Extension)**机制来安全地插入自定义代码。下面分别说明两种后端架构下 UI 扩展的实现方式。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 914

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Spring集成ehcache缓存配置文件模板

Spring集成自带ehcache缓存配置文件模板及过程

weixin_43920527的博客 1141

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Spring Module---cache

项目需要缓存,google了一下,发现spring module有现成的东西可用,随即拿来看看。发现还是比较好用的。 改cache采用了aop的方式进行cache的写入和刷出。使用spring风格,上手容易。 系统负载不高,因此对于该缓存方法的性能等等不做评价,目前满足需要。 使用方法: 有这么几个概念需要提前知道1.XXCacheFacade,比如如果是oscache,那么XX就是oscache...

paulwong 244

深度学习工具包 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'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('DeepLearnToolbox'));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('state',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'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',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 = 'sigm';%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, 'Too big error');```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('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';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)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';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, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% 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'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 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, 'Too big error');```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('state',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, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',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, 'Too big error');%% ex3 neural net with dropoutrand('state',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, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % 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, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % 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, 'Too big error');%% 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('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % 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, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

spring-modules-cache的问题

当前的spring-modules-cache 0.8版本对于oscache不支持flushEntry,只支持flushGroups和flushAll。原因是其没有记录cacheKeyGenerator,无法得到cache的key,通过修改,应该是可以得到cache的key,并让其支持flushEntry的。...

CurrentJ 351

springmodules ehcache异常求解

网上找到和相同错误的实例如下: http://forum.springframework.org/showthread.php?t=44244 大概的错误应该是指http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd地址找不到,要指向本地springmodules-ehcache.jar吧?我按...

用心学java 586

web.xml配置文件中 async-supported true /async-supported 报错的解决方案

     web.xml配置文件中&lt;async-supported&gt;true&lt;/async-supported&gt;报错的解决方案   转载:https://blog.csdn.net/u013215018/article/details/54290521 &lt;async-supported&gt;true&lt;/async-supported&gt;报错的解决方案...

stay hungry ! stay foolish! 3429

使用spring-modules-0.9实现注解缓存

关于spring实现ehcache有很多方法,很多都是利用aop来实现,我认为采用注解的方式更灵活,配置也更简洁。下面就是我利用spring-modules-0.9实现的注解缓存。配置文件如下:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001

gaoligaoli的专栏 5284

基于springmodules的缓存方案

简介           通常在系统开发中,必不可少的要使用到缓存(Cache),如用户信息、字典信息都会使用缓存来提高性能;但是如何使用好缓存是个需要深入研究的话题,缓存方案没有通用性,针对不同的应用层面,缓存的设计通常也是千差万别的!这里只是介绍了一种比较轻量级、无侵入的缓存方案,该方案基于Spring+SpringModules。目的方法级别的缓存 声明式、无侵入 不

黄瓜和土豆的专栏 3002

Spring AOP事务管理(使用切面把事务管理起来)

在《Spring Transaction 分析事务属性(事务的基本概念、配置)》基础上  1、使用基于注解的AOP事务管理      探索tx:annotation-driven标签:  标签是注解驱动的事务管理支持的核心。  标签的属性:  transaction-manager:指定到现有的PlatformTransactionManager bean的引

java相关技术(partner4java专栏)希望对您的学习有所帮助 6万+
上一篇: junit 简单介绍(入门)
下一篇: 项目环境问题 java.lang.UnsupportedClassVersionError: Bad version number in .class file
partner4java
博客等级 码龄17年 481粉丝 183原创
评论 13
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址