spring事务说明

Spring 基础 事务说明 事务其实是一个并发控制单位,是用户定义的一个操作序列,这些操作要么全部完成,要不全部不完成,是一个不可分割的工作单位。 阅读详情
一、Propagation :

对于特定的方法或方法命名模式,代理的具体事务行为由事务属性驱动,如下面的例子所示:
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>

  key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:

  • PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
  • PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
  • PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
  • PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

      前六个策略类似于EJB CMT:常量名相同,因此,对EJB开发人员来说,应该立刻就感到熟悉。第七个(PROPAGATION_NESTED)是Spring所提供的一个特殊变量。它要求事务管理器或者使用JDBC 3.0 Savepoint API提供嵌套事务行为(如Spring的DataSourceTransactionManager),或者通过JTA支持嵌套事务。

    

二、Isolation Level(事务隔离等级):
1、Serializable:最严格的级别,事务串行执行,资源消耗最大;
2、REPEATABLE READ:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但是带来了更多的性能损失。
3、READ COMMITTED:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已修改但未提交的数据,避免了“脏读取”。该级别适用于大多数系统。
4、Read Uncommitted:保证了读取过程中不会读取到非法数据。

spring中的Isolation属性:
1、ISOLATION_DEFAULT :使用当前数据源的默认级别
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.

三、readOnly
事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这是一个最优化提示。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。

四、Timeout

      在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。

事务划分策略

1、推荐在业务层使用事务,这样可以允许业务层捕获导致rollback的异常,并抛出恰当的业务层异常;不在dao层使用事务是因为这会限制了dao重用其他事务需求,并且dao层没有实现业务逻辑,并且原子性也是业务层的概念。

spring声明性事务的划分:
1、有四个地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.




2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事务

以下为配置实例:

<!-- The DBCP DataSource -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">
    <property name="driverClassName">
      <value>${jdbc.driverClassName}</value>
    </property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
  </bean>
   
  <!-- The DAO class -->
  <bean id="dao" 
   class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean>
   
  <!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"   
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean>
   
  <!-- TransactionInterceptor -->
  <bean id="transactionInterceptor"     
   class="org.springframework.transaction.interceptor.TransactionInterceptor">    <property name="transactionManager">      <ref bean="transactionManager"/>    </property>    <property name="transactionAttributeSource">      <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED
      </value>
    </property>
  </bean>  
   
  <!-- Transactional proxy for the primary business object -->
  <bean id="boxOffice"         
   class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target">      <ref local="boxOfficeTarget"/>    </property>    <property name="proxyInterfaces">      <value>org.springframework.prospring.ticket.service.BoxOffice</value>    </property>    <property name="interceptorNames">      <value>transactionInterceptor</value>    </property>
  </bean>  
   
  <!-- Business Object -->
  <bean id="boxOfficeTarget" 
    class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
    <property name="boxOfficeDao">
      <ref local="dao"/>
    </property> 
  </bean>

3、使用TransactionProxyFactoryBean配置spring事务
以下为配置实例:

  <!-- The DBCP DataSource -->
  <bean id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close">
    <property name="driverClassName">
      <value>${jdbc.driverClassName}</value>
    </property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
  </bean>
   
  <!-- The DAO class -->
  <bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean>
   
  <!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean>
   
  <!-- Transactional proxy and the primary business object -->
  <bean id="boxOffice"    
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    <property name="transactionManager"><ref bean="transactionManager"/></property>    <property name="target">      <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
        <property name="boxOfficeDao">
          <ref local="dao"/>
        </property> 
      </bean>
    </property>
    <property name="transactionAttributes">      
      <props>        
         <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>        
         <prop key="allocate*">PROPAGATION_REQUIRED</prop>      
      </props>    
   </property>
  </bean>  

4、使用BeanNameAutoProxyCreator配置spring事务
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name给BeanNameAutoProxyCreator,spring就会个给该bean提供事务代理,配置实例如下:

  <!-- The DBCP DataSource -->
  <bean id="dataSource" 
   class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close">
    <property name="driverClassName">
      <value>${jdbc.driverClassName}</value>
    </property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
  </bean>
   
  <!-- The DAO class -->
  <bean id="dao"
   class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean>
   
  <!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"    
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property> 
  </bean> 
   
  <!-- TransactionInterceptor -->
  <bean id="transactionInterceptor"         class="org.springframework.transaction.interceptor.TransactionInterceptor">    <property name="transactionManager">      <ref bean="transactionManager"/>    </property>    <property name="transactionAttributeSource">      <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED      </value>    </property>
  </bean>  
   
  <!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator"    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  <property name="interceptorNames">    <value>transactionInterceptor</value>  </property>  <property name="beanNames">    <list>      <idref local="boxOffice"/>    </list>  </property>
</bean>  
   
<!-- Business Object -->
<bean id="boxOffice"
   class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
  <property name="boxOfficeDao">
    <ref local="dao"/>
  </property> 
</bean>
详解机器人标定 这里是机器人在取料之前,先把自己的角度补正到与 物料当前角度一致,到(X’,Y’)位置处,然后移动 CDx,Cdy,与物料位置也重合,然后去取料。可以使用实物标定,机器人抓住工件摆放9个位置,得到9个机械坐标,相机也得到9个像素坐标,(机器人每次需要回到固定位置拍照),然后标定。(X0,Y0)为旋转中心, (X1,Y1)及(X2,Y2)为工件在视野中旋转角度a的前后坐标,(Xt,Yt)为两点连线的延长。(rx0, ry0)为旋转中心,( x, y)为被旋转的点,(x0,y0)旋转后的点。 阅读详情

相关推荐

Deep Reinforcement Learning - 1. DDPG原理和算法

Deep Reinforcement Learning - 1. DDPG原理和算法 背景描述 DDPG的定义和应用场景 DDPG算法相关基本概念定义 DDPG实现框架和算法 DDPG对于DPG的关键改进 下一篇 以下用RL作为Reinforcement Learning 的简称。背景描述概括来说,RL要解决的问

kenneth_yu的博客 14万+

java 事物 notsupport_Spring事务传播属性介绍(二).mandatory、not_supported、never、supports...

Required、Required_New传播属性分析传送门:https://www.cnblogs.com/lvbinbin2yujie/p/10259897.htmlNested传播属性分析传送门:https://www.cnblogs.com/lvbinbin2yujie/p/10260066.html我的Spring事务传播属性介绍比较传送门:https://files.cnblogs.c...

weixin_33268205的博客 1055

Arduino Uno R3开发板系列学习教程(番外篇:USB驱动安装与疑难排解)

本文详细介绍了Arduino Uno R3开发板USB驱动安装与疑难排解的完整指南,特别针对国产克隆版常见的CH340驱动问题提供解决方案。从识别开发板类型到Windows/Mac系统驱动安装步骤,再到常见问题排查与高级排错技巧,帮助开发者快速解决连接问题,确保程序烧录顺利进行。

weixin_29182481的博客 581

Spring 事务七大传播机制 —— REQUIRED、SUPPORTS、MANDATORY、REQUIRES_NEW、NOT_SUPPORTED、NEVER、NESTED

首先简单了解一下 Spring 中事务传播行为是什么?听起来很高端,但是真正用起来的时候,稍有不慎,就会让自己陷入困境之中,所以在使用之前,我们必须要十分耐心认真的学习它。从名字理解起来,事务传播行为,既然为传播就肯定发生在两个实体之间,否则单个实体又如何发生行为呢。通俗点讲就是“一个巴掌拍不响”。下面是定义。事务传播行为主要用来描述由某一个事务传播行为修饰的方法被嵌套进另一个方法的事务中,该事务如何传播。这个概述可能不好理解,换句话就是当一个事务方法被另一个事务方法调用时,这个事务方法应该如何进行。

流华追梦的专栏 5040

AIDD-人工智能药物设计-构建基于机器学习方法的在线预测平台高准确预测药物诱导骨毒性

在此基础上,提取多种药物分子特征,包括分子指纹(如MACCS、Morgan、RDKit、TopologicalTorsion、AtomPairs与Descriptors)及分子图谱,通过Transformer编码器与神经网络进行机器学习模型训练。最后,将训练好的模型部署为在线Web服务器(BoneToxPD),供用户快速预测药物的骨毒性。本研究不仅在骨毒性预测的准确性方面取得了突破,还在模型解释性方面提供了新的视角,通过注意力机制精准识别出影响骨毒性的关键化学基团,推动药物研发领域更安全、更高效的发展。

itwangyang520的博客 1079

Python+gensim【中文LDA】简洁模型

文章目录完整代码过程详解words_lsdictionarydictionary.doc2bow函数corpuslda 完整代码 from gensim import corpora, models import jieba.posseg as jp # 待分析文本集 text1 = '美国教练坦言,没输给中国女排,是输给了郎平' text2 = '中国女排世界排名第一?真实水平如何,听听巴西和美...

ArYe 2万+

Spring-事务管理-加强

Spring-事务管理-加强

汤键的博客 590

(10)Proteus 8.9上的STM32仿真系列(HAL)---freertos

实验设计:通过cubemxide生成freertos工程,然后创建一个 pc13 led灯间隔200ms闪烁的任务。非常遗憾的是proteus8.9仿真失败,实物上没有问题,但还是把流程过一遍。 一、原理图 实物是淘宝上stm32f103 c8t6的核心板,proteus上没有这个芯片,这里图上大概表示一下,用的是核心板自带的pc13引脚的led灯。 二、cubemxide配置 1.RCC如之前工程一样配置 2、配置SYS timebase Source不要使用systick,自己选择一个定时器,我这

qq_35442550的博客 7342

Spring事务处理

Spring中事务的定义: 一、Propagation : 对于特定的方法或方法命名模式,代理的具体事务行为由事务属性驱动,如下面的例子所示: &lt;prop key="load*"&gt;&lt;/prop&gt; PROPAGATION_REQUIRED,readOnly &lt;prop key="store*"&gt;&lt;/prop&gt; PROPAGATION_REQUIRED...

zengjinliang的专栏 202

手把手教你用CODESYS将旧笔记本改造成软PLC(含证书配置避坑指南)

本文提供了一份详尽的指南,教你如何利用CODESYS平台将闲置旧笔记本改造为一台功能完整的软PLC。内容涵盖从硬件评估、系统实时性优化、软件安装与驱动配置,到关键的证书授权管理避坑指南,旨在帮助自动化工程师和爱好者低成本搭建稳定可靠的学习与开发平台。

weixin_29190573的博客 279

基于STM32和MPU6050的手势控制小车设计与实现

MPU6050传感器:用于检测手势变化,采集加速度和陀螺仪数据。STM32微控制器:处理MPU6050传感器的数据,根据手势变化控制小车的运动。电机驱动模块:接收来自STM32的控制信号,驱动小车的电机,实现小车的前进、后退、左转和右转。小车底盘:作为整个系统的机械部分,提供移动平台。MPU6050是一款集成了3轴加速度计和3轴陀螺仪的传感器,具有高精度、低功耗和小体积等特点。它通过I2C接口与微控制器通信,可以实时采集加速度和角速度数据,广泛应用于运动检测、姿态控制和手势识别等领域。

m0_57781768的博客 2309

jdbc管理事务

JDBC控制事务 事务: 一个包含多个步骤的业务操作。如果这个业务操作被事务管理,那么这些操作要么同时成功要么同时失败 操作 1。开启事务 2.提交事务 3.回滚事务 使用Connection 对象来管理事务 开启事务 : setAutCommit(boolean autoCommit):调用该方法 设置参数为false ,即开启事务 在操作之前开启事务 例:conn.setAutCommit(boolean autoCommit) 提交事务: commit() conn.commit() 回滚事务: ro

weixin_45701447的博客 434

ANSYS WORKBENCH——参数化建模以及参数优化(结果导出为Excel)

目录 1、打开软件workbench 2、找到static structure,双击打开 3、选择材料 4、参数化建模 ​ 注:印记面的创建 5、模型属性设置 注:设置优化目标 6、回主界面,选择direct optimization模块 1、打开软件workbench 2、找到static structure,双击打开 3、选择材料 4、参数化建模 至此参数化建模就已经完成啦 注:印记面的创建 印记面一般是一个虚拟的区域,参与实际的网格划分,但是只是由于载.

python_AI_fans的博客 1万+

Spring 对于事务上的应用的详细说明

什么是事务在一个业务流程当中,通常需要多条DML(insert delete update) 语句共同联合才能完成,这多条DML语句必须同时成功,或者同时失败,这样才能保证数据的安全。多条DML要么同时成功,要么同时失败,这叫做事务。事务(Transaction)第一步:开启事务(start transaction)第二步:执行核心业务代码第三步:提交事务(如果核心业务处理过程中没有出现异常)(commit transaction)

2501_92608043的博客 975

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Spring AOP及事务说明

事务的传播行为我们只需要掌握两个:REQUIRED、REQUIRES_NEW。REQUIRED :大部分情况下都是用该传播行为即可。REQUIRES_NEW :当我们不希望事务之间相互影响时,可以使用该传播行为。比如:下订单前需要记录日志,不论订单保存成功与否,都需要保证日志记录能够记录成功。什么是AOP?* AOP英文全称:Aspect Oriented Programming(面向切面编程、面向方面编程),其实说白了,面向切面编程就是面向特定方法编程。

m0_72167535的博客 1716

pscad IEEE标准系统搭建说明

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

Spring中的事务简介说明

Spring中的事务简介说明

qq_25073223的博客 250

深度学习工具包 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")

spring的两种常用事务传播属性说明

常用的两种传播属性: 1.REQUIRED 如果存在当前事务则用当前事务,如果不存在当前事务,则新建一个事务 2.REQUIRES_NEW 如果当前存在事务则挂起当前事务,开启一个新事务,新事务执行完毕后,唤醒之前挂起的事务,继续执行。如果不存在当前事务,则新建一个事务 第一种情况: 执行controller以后事务会回滚,两条数都插不进去。(propagation如果不写默认的是...

qianxiaopeng的专栏 1万+

Spring事务说明与自实现

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33809981的博客 90

Spring事务说明

最近有朋友问到spring中事务的配置,以下就是比较常见的配置   配置文件如下:&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beans xmlns="http://www.springframework.org/schema/beans "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ...

130

spring 事务说明

在 spring 中一共定义了六种事务传播属性, 如果你觉得看起来不够直观, 那么我来转贴一个满大街都有的翻译 引用 l          PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 l          PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。 l    

风行天下 881

spring mvc 事务配置,spring 事务配置说明

新建项目记录下springmvc和spring的事务配置,解决事务不回滚的问题。 问题说明: Spring容器优先加载由ServletContextListener(applicationContext.xml)产生的父容器,而SpringMVC(spring-mvc.xm...

chijunmei7041的博客 172
上一篇: Oracle中Number(p,s)的意义
下一篇: js中return的用法
javaerSir
博客等级 码龄15年 1粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值