Miller Rabin 判素算法

Miller-Rabin素数检测算法 今天看了一下Miller-Rabin素数检测的算法,总结了一下,希望这篇博客对你们有帮助。 先说几个理论基础: 1. 费马小定理:假如p是质数,a是整数,且a、p互质,那么a的(p-1)次方除以p的余数恒等于1,即:a^(p-1)≡1(mod p). 但是反过来却不一定成立,就是说,如果a、p互质,且a^(p-1)≡1(mod p),不能推出p是质数,比如Carmichael数。 2. 阅读详情

引入

给出 t    ( t ≤ 500 ) t~~(t\le500) t  (t≤500) 个正整数 x    ( x ≤ 2 63 − 1 ) x~~(x\le2^{63}-1) x  (x≤263−1),判断 x x x 是否为质数。


Miller Rabin 素性检验

显然在这个 x x x 的范围下,试除法判断质数是不行的。

M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 素性检验就是一种用来 判断大数是否为质数 的 随机性 算法。
它在计算机科学中的用途是用来判断工业质数 ( ≥ 2 1024 的 质 数 ) (\ge2^{1024}的质数) (≥21024的质数),在这个范围下它的判断是否正确存在随机性,但是在 O I OI OI 范围下 ( ≤ 2 63 − 1 ) (\le 2^{63}-1) (≤263−1) 确是一种 错误率极低 的 确定性 算法。

在 引入 的问题中,我们可以考虑使用 M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 素性检验来判断质数。
在 M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 素性检验中,我们要先了解它判断素数的理论基础:

  • 费马素性检验
  • 二次探测定理

费马素性检验

费马小定理:

若 p 是 质 数 , gcd ⁡ ( a , p ) = 1 , 则   a p − 1 ≡ 1 ( m o d p ) 若p是质数,\gcd(a,p)=1,则\ a^{p-1}\equiv1\pmod p 若p是质数,gcd(a,p)=1,则 ap−1≡1(modp)
证明:

考 虑 P = { m 1 , m 2 , m 3 , ⋯   , m p − 1 } , m i < p , 显 然 有 m i ≢ m j ( m o d p ) ∵ gcd ⁡ ( a , p ) = 1 ∴ 对 于 Q = { a ⋅ m 1 , a ⋅ m 2 , a ⋅ m 3 , ⋯   , a ⋅ m p − 1 } , 也 有 a ⋅ m i ≢ a ⋅ m j ( m o d p )   利 用 同 余 式 的 性 质 , 我 们 对 这 两 个 两 两 元 素 之 间 不 同 余 的 集 合 联 利 , 得 ∏ i = 1 p − 1 a ⋅ m i ≡ ∏ i = 1 p − 1 m i ( m o d p ) a p − 1 ∏ i = 1 p − 1 m i ≡ ∏ i = 1 p − 1 m i ( m o d p ) a p − 1 ≡ 1 ( m o d p ) 证 毕 考虑P=\{m_1,m_2,m_3,\cdots ,m_{p-1}\},m_i<p,显然有 m_i\not\equiv m_j\pmod p\\\because \gcd(a,p)=1\\\therefore 对于Q=\{a\cdot m_1,a\cdot m_2,a\cdot m_3,\cdots,a\cdot m_{p-1}\},也有 a\cdot m_i\not\equiv a\cdot m_j\pmod p\\~\\利用同余式的性质,我们对这两个两两元素之间不同余的集合联利,得\\\prod\limits_{i=1}^{p-1}a\cdot m_i\equiv \prod\limits_{i=1}^{p-1} m_i\pmod p\\a^{p-1}\prod\limits_{i=1}^{p-1} m_i\equiv \prod\limits_{i=1}^{p-1}m_i\pmod p\\a^{p-1}\equiv 1\pmod p\\证毕 考虑P={m1​,m2​,m3​,⋯,mp−1​},mi​<p,显然有mi​​≡mj​(modp)∵gcd(a,p)=1∴对于Q={a⋅m1​,a⋅m2​,a⋅m3​,⋯,a⋅mp−1​},也有a⋅mi​​≡a⋅mj​(modp) 利用同余式的性质,我们对这两个两两元素之间不同余的集合联利,得i=1∏p−1​a⋅mi​≡i=1∏p−1​mi​(modp)ap−1i=1∏p−1​mi​≡i=1∏p−1​mi​(modp)ap−1≡1(modp)证毕

若对证明中的同余性质仍有不理解,可以看这里

利用费马小定理这个有力的工具,在判断 x x x 是否为质数时,我们或许会有这么一种思路:对于要判定的 x x x ,我们随机选择一个与 x x x 互质的底数 a a a ,若 a x − 1 ≡ 1 ( m o d x ) a^{x-1}\equiv 1\pmod x ax−1≡1(modx),则 x x x 为质数。

此方法被称为费马素性检验。

稍微尝试,好像真的可行诶!但是别着急,这个想法是错误的,因为存在这么一类伪质数 (卡迈克尔数) x x x,也有 a x − 1 ≡ 1 ( m o d x ) a^{x-1}\equiv 1\pmod x ax−1≡1(modx),但 x x x 是合数!

因此,单独使用费马素性检验,在小部分情况下会对合数进行误判,所以我们还需要一个关于质数的定理进行二次判断。


二次探测定理

考 虑 p 为 质 数 , a 与 p 互 质 , 若 a 2 ≡ 1 ( m o d p ) , 则 有 p ∣ ( a 2 − 1 ) p ∣ ( a + 1 ) ( a − 1 ) ∴ p ∣ ( a + 1 ) 或 p ∣ ( a − 1 ) ∴ a ≡ ± 1   ∴ 若 a < p , a = p − 1 或 a = 1 考虑 p 为质数,a 与 p 互质,若 a^2\equiv 1\pmod p,则有\\ p\mid(a^2-1)\\ p\mid (a+1)(a-1)\\ \therefore p\mid (a+1) 或 p\mid (a-1)\\ \therefore a\equiv \pm1\\ ~\\ \therefore 若 a<p,a=p-1或a=1 考虑p为质数,a与p互质,若a2≡1(modp),则有p∣(a2−1)p∣(a+1)(a−1)∴p∣(a+1)或p∣(a−1)∴a≡±1 ∴若a<p,a=p−1或a=1

因此,对于每个要判断的 x x x ,我们都随机选取一个底数 a < x a<x a<x,若 a 2 ≡ 1 ( m o d x ) a^2\equiv 1\pmod x a2≡1(modx),则判断 a a a 是否为 1 1 1 或 x − 1 x-1 x−1,是则 x x x 大概率是个质数,否则 x x x 一定不是个质数。

然而,二次探测定理也仍然会出现 “漏洞”,依旧存在一类合数 x x x 使得任意 a 2 ≡ 1 ( m o d x ) a^2\equiv 1\pmod x a2≡1(modx) 时 a ≠ 1 a\ne 1 a​=1 且 a ≠ x − 1 a\ne x-1 a​=x−1

那么,费马素性检验和二次探测定理都不可行,是否说明我们要找到第三种 确定性 的定理来判断质数呢?也不需要。

我们可以考虑将费马素性检验与二次探测定理一起使用,对这个数是否为质数进行双重判断。

那么,只有同时满足费马素性检验和二次探测定理的 x x x 是个质数。这是否一定正确呢?也是无法保证的,存在那么极少的合数,同时满足这两个定理。

但是显然,这种同时满足两个定理的合数,要比满足单一定理的合数要少的多。通俗的说,若一个数 x x x 满足同时满足两个定理,那么它是质数的概率可以达到 99 % 99\% 99%。而如果我们对同一个 x x x 都使用多个不同的底数 a a a 进行定理验证,那么这个误判的概率将进一步缩小,可以直接忽略。

因此,同时使用费马素性检验和二次探测定理 进行判素在 O I OI OI 数据范围内是个正确性保证的确定性算法,同时它也是 M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 素性检验的原理所在。


Miller Rabin 算法

M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 算法,即在程序上实现 M i l l e r   R a b i n \mathbf{Miller\ Rabin} Miller Rabin 素性检验,本质上也是在实现费马素性检验的判断中同时进行二次探测检验。

考虑判断 x x x 是否为质数,先将 x − 1 x-1 x−1 分解为 2 k ⋅ t , k , t ∈ N 2^k\cdot t,k,t\in \N 2k⋅t,k,t∈N
选择一个 a a a 作为费马和二次探测的底数,然后使 a t a^t at 不断平方,在这个过程中运用二次探测定理检验,即判断是否当 ( a t ) 2 ≡ 1 ( m o d x ) (a^t)^2\equiv 1\pmod x (at)2≡1(modx) 时 a t ≡ ± 1 ( m o d x ) a^t\equiv \pm 1\pmod x at≡±1(modx),否则 x x x 不是质数。
很显然平方的过程执行 k k k 次后, a a a 的指数将变为 x − 1 x-1 x−1,此时再运用费马素性检验判断是否 a x − 1 ≡ 1 ( m o d x ) a^{x-1}\equiv 1\pmod x ax−1≡1(modx),否则 x x x 不是质数

若 x x x 在上述过程中都没被 p a s s pass pass 掉,则 x x x 一定 大概率是个质数。

如果担心算法被卡,我们可以进一步降低误判的概率,使用多组底数 a a a 进行如上操作验证,只有当所用的所有的底数 a a a 都可以使得 x x x 满足两条定理时, x x x 才是质数。

底数 a a a 到底怎么取, w a n g r x \color{Red}wangrx\color{balck} wangrx 大佬给出了答案:

  • 当 x ≤ 2 32 x\le 2^{32} x≤232 时,选取 2 , 7 , 612 , 7 , 61 2,7,612,7,61 2,7,612,7,61 三个底数即可
  • 当 x ≤ 2 63 x\le 2^{63} x≤263 时,选取 2 , 325 , 9375 , 28178 , 450775 , 9780504 , 17952650222 , 325 , 9375 , 28178 , 450775 , 9780504 , 1795265022 2,325,9375,28178,450775,9780504,17952650222,325,9375,28178,450775,9780504,1795265022 2,325,9375,28178,450775,9780504,17952650222,325,9375,28178,450775,9780504,1795265022七个底数即可。

至此, M i l l e r R a b i n \mathbf{Miller Rabin} MillerRabin 素性检验本是一个带有不确定性的检验方法,但在 O I OI OI 数据范围内,我们可以通过增加底数的方式使其成为了一个确定性的算法,因此可以运用于实践当中。

CODE

#include <bits/stdc++.h>
#define int __int128
using namespace std;
long long t,x;
long long prime[9]={0,2,3,5,7,11,13,17,37}; //底数取值
int ksm(int a,int b,int mod)
{
	int sum=1;
	while(b)
	{
		if(b&1) sum=sum*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return sum%mod;
}
bool Miller_Rabin(int p,int a)
{
	int k=0,t=p-1,cnt;
	while(!(t&1)) t>>=1,k++;  //找到上述算法流程中的 t 和 k
	a=ksm(a,t,p);
	for(int i=1;i<=k;i++)
	{
		if(a*a%p==1)
		  if(a!=1&&a!=p-1) return false;  //二次探测定理
		 a=a*a%p;
	}
	if(a!=1) return false;  //费马素性检验判断
	return true;
}
bool isprime(int x)
{
	for(int i=1;i<9;i++)  //多次 Miller Rabin 验证
	{
		if(x==prime[i]) return true;
		if(x%prime[i]==0) return false;  //特判掉一些简单情况
		if(!Miller_Rabin(x,prime[i])) return false;
	}
	return true;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t)
	{
		t--;
		cin>>x;
		if(isprime(x)) cout<<"YES"<<'\n';
		else cout<<"NO"<<'\n';
	}
	return 0;
}
Miller–Rabin 素性检验算法 算法介绍 Miller-Rabin素数检验或Rabin-Miller素数检验是一种概率素数检验:一种确定给定数是否可能是素数的算法,类似于费马素数检验和Solovay-Strassen素数检验。作为实践中使用比较广泛的素性检验算法的一种, Miller-Rabin算法最早在1976年由Gary L. Miller提出(当时该算法是确定性的), 并在1980年由Michael O. Rabin改进为无条件的概率算法. 数学原理 算法的主要思想就是检验数是否满足特定的素数条件, 这些条件在Miller-Rabi 阅读详情

相关推荐

Miller_Rabin算法详解

提示:Miller-Rabin质数测试 小Hi:这种质数算法是基于费马小定理的一个扩展,首先我们要知道什么是费马小定理: 费马小定理:对于质数p和任意整数a,有a^p ≡ a(mod p)(同余)。反之,若满足a^p ≡ a(mod p),p也有很大概率为质数。 将两边同时约去一个a,则有a^(p-1) ≡ 1(mod p) 也即是说:假设我们要测试n是否为质数。我们可以随机选取一个数a,

老子(道家) 5225

Millar Rabin模板(米勒罗宾素数判断)

Millar Rabin

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 291

Miller-Rabin素数检测算法笔记

本文内容主要参考《程序员的数学思维修炼》一书中对素数和余数的讲解及这篇博文: Miller-Rabin素数测试学习笔记 以及这篇文章: 数论部分第一节:素数与素性测试 先介绍一个概念:互素(或叫互质) 如果a和b两个数的公约数只有1,那么a和b互质,记做(c,m)=1。 Fermat小定理 卡迈克尔数 Fermat素性测试 伪素数 流程图

不知道是谁的博客 3990

MillerRabin素性检测

1)先特判筛掉3以下的数与偶数。2)将待检验数 n, n−1 化为3)选取多个底数,分别对… 进行检验,判断其解是否全为1,或在非最后一个数的情况下出现 p−1,然后之后全是1。4)如果都满足,我们就认为这个数是素数。

qq_61971570的博客 1731

[数论] Miller-Rabin素性检验/Pollard-Rho算法分解质因数

试除法分解质因数跑的太慢! 咋办?

DATA_joel的博客 1207

素数判定的一些讨论(Miller-Rabin算法)

很久没有写博客了。。。最近军训加开学,感觉刷题速度有降低,要补一补。 回归正题,正式进入数论阶段,讨论一下关于素数判定的那些事。 一类问题: 判定一个整数n(n>1)是否为素数。 算法1: 直接根据素数的定义枚举ii从22到(n−1)(n-1),如果n%i==0nn为合数。 时间复杂度:O(n)O(n) bool is_prime(int n) { int i;

Sunshine_cfbsl 4350

MillarRabin模版 随机测大素数

支持long long #include typedef long long ll; ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll MultiMod(ll a, ll b, ll n) { // a * b % n ll res = 0; MillarRabin a %= n; while (b > 0) {

九野的博客 1345

Python miller rabin米勒-拉宾素性检验算法详解及源码

Miller-Rabin算法是一种素性检验算法,用于判断一个大数是否为素数。它的基本原理是基于费马小定理和随机化算法,通过多次随机选择底数进行检验。

希望我的博客,能帮上你解决学习中工作中所遇到的问题 884

bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 【题解】 参考:http://www.matrix67.com/blog/archives/234 Millar-Rabin质数检验方法: 根据费马小定理,如果p是素数,a<p,那么有a^(p-1) mod p = 1。 直观想法我们直接取若干个a,如果都有一...

aklm45097的博客 205

GAN网络(论文阅读):Generative Adversarial Nets

当判别器效果比较好时,D(G(z))值为0,则log(1-D(G(z)))值为0,当判别器效果不好,会输出一个大于0 的数,则log(1-D(G(z)))值就为一个负数,在极端情况下,会得到负无穷大,此时判别失误。在价值函数的第二项中存在问题,在早期G比较弱的时,生成的数据和真实数据相差很大,D就容易训练的比较好,很容易区分,就会导致log(1-D(G(z)))变成0 ,这样很难求梯度来优化G,所以更新G时,将优化函数改为log(D(G(z)))。最终使G赢,即G生成的数据D判别不出来。

m0_54248968的博客 2290

OBS技术深度剖析:从开源直播工具到专业制作引擎的进化之路

本文深入剖析了开源直播软件OBS的技术架构与发展历程。从2012年诞生至今,OBS已成长为直播领域的事实标准,其成功源于模块化设计和开源协作模式。文章详细解析了OBS的核心技术:1) 分层架构与插件系统,2) GPU加速的渲染管线,3) 智能音频处理,4) 多平台编码优化。同时探讨了虚拟摄像头、浏览器源等高级功能的实现原理,并提供了性能优化方案和硬件配置建议。随着AI、WebRTC等新技术发展,OBS将持续引领实时内容创作工具的创新,为开发者和创作者提供更强大的技术支持。

程序猿老樊的博客 324

【Miller-Rabin随机判素数算法】

实用性介绍:████████████████████████████████ 科学性证明:████████████████████████████████ ·在运算中尝试使用quick-pow和quick-mul加速。但实际告诉我们,在多次判素数的情况下,使用quick-mul反而会减速,原因可能是原装%运算消耗了大量时间。(复判重复数T取在4以上,不然很危险) ·大米饼目前只能将它作为“...

weixin_34150503的博客 182

Maxwell参数化建模

MAXWELL 2019 R3 参数化建模 参数化建模的优势 所谓参数化,就是在电机本体设计中涉及到的各类参数变量化,而每个变量之间又存在一定的制约关系,这样只需修改其中某个或者某些变量就可以做到对整个电机模型合理地改动。 参数化举例 以表贴式电机为例,先画电机定子,如上所示,依次进行后会弹出下面的选项框 上图就是描述电机尺寸的各类参数,接下来进行参数化。 回车ENTER,并给变量ds赋值。 赋值好后,回车ENTER 左下角properties里面就有我们新建立的变量ds了。 继续将剩余的参数

qq_41387280的博客 1万+

素数专辑(超详细超完善版本)

素数的使用 素数以及素数性质 素数筛法 素数判断法 素数常见使用点 总结 1.素数以及素数性质 1.1素数概念 素数,也称质数。是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。 1.2素数性质 质数具有许多独特的性质: (1)质数p的约数只有两个:1和p。 (2)初等数学基本定理:任一大于1的自然数,要么本身是质数,要么可以分解为几个质数之积,且这种分解是唯一的。 (3)质数的个数是无限的。 (4)质数的个数公式 是不减函数。 (5)若n为正整数,在 到 之间至少有一个质数。 (6)若

安辰的博客 547

【信道编码/Channel Coding】线性分组码

这是本专栏信道编码/Channel Coding的第二站,想对信道编码有一个系统性的认识可以看本专栏的信道编码的整体框架一文。而在本篇文章中,将介绍线性分组码的基本原理。

Tomatomatodo的博客 9242

vivado 编译ADI AD9361 TCL脚本生成hdl工程

(4)输入"source ../../scripts/adi_make.tcl"命令(仅双引号内部命令,不包含双引号),个人理解:该命令用来加载当前所需要编译工程所需要各功能模块的信息,因为每个工程需要不同的功能模块,比如有的需要gpio模块,有的不需要gpio模块,这些模块上前面路径的library也同样以TCL脚本形式存在,当有需要时才进行编译,怎么知道当前工程需要什么模块呢就是通过"source ../../scripts/adi_make.tcl"(仅双引号内部命令,不包含双引号)命令实现。

weixin_40586423的博客 807

Miller-Rabin素数测试算法

知识点系列之---Miller-Rabin素数测试

forever_dreams的博客 3万+

BMW Trojan 样本分析

仅用于链接已经删却但是需要方便查阅的原文http://blog.youkuaiyun.com/zirconsdu/article/details/7997470 转载请注明出处http://blog.youkuaiyun.com/zirconsdu/article/details/8153813。 一些具体数据结构和具体路径隐去。   BMW木马样本分析 摘要 2011年底和某Hunter的谈话涉及到当时新检

zirconsdu的专栏 3910

素数判定Miller_Rabin 算法详解

素数判定Miller_Rabin  算法详解 上次说好的要把素数判定和大数分解(见另一篇博文)的快速随机化算法解决了,于是乎今天就来解决,不得不说理解起来真的有困难。我只能大概的将思路理一下,若有错漏还请担待。

JUST CODE 2万+

SKUA-GOCAD入门教程-第二节

软件启动及项目设置

江洋大葱的博客 1146

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

Miller-Rabin算法素检测介绍及实现(含c++和python实现代码)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一、pandas是什么? 二、使用步骤 1.引入库 2.读入数据 总结 前言 提示:以下是本篇文章正文内容,下面案例可供参考 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码如下(示例): import numpy as np import pandas as pd im...

希望日记的博客 8732

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

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

Miller-Rabin算法

算法背景 米勒-拉宾素性检验(Miller Rabin算法),是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数。卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义黎曼猜想的确定性算法,由于广义黎曼猜想并没有被证明,其后由以色列耶路撒冷希伯来大学的Michael O. Rabin教授作出修改,提出了不依赖于该假设的随机化算法。 涉及知识点 费马小定理 费马小...

qq_43227036的博客 9675

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

素性检测(Miller-Rabin)

一.素性检测(MIller-Rabin) 1.一些基础知识点 (1)位运算 n&1 这里 n&1 就是——判断n是否为奇数 因为n为奇数时,对应的二进制数最低位一定为1,n&1的结果就是1 n为偶数时,相应的最低位为0,n&1的结果就是0. 1&n 这里的 1&n 就是: 检查二进制n的最低位,若为1,则1&n的结果就是1, 若不为1,则1&n的结果就是0. 按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算。 运算规则

qq_45031509的博客 6909

Miller-Rabin素性测试算法详解

看了一些别人的博客,发现里面涉及到的公式没有证明,于是就打算自己写一篇比较详细的讲解。 先看两个引理及其证明(建议把证明搞懂)。 PS:以下图片均为作者用wps制作,如想使用请附上作者博客链接,谢谢O(∩_∩)O。 看完了上面的引理,那就可以正式开始Miller-Rabin算法的讲解了。 背景:素性测试(即测试给定的数是否为素数)是近代密码学中的一个非常重要的课题。虽然Wilson

每天都要进步哦~ 2万+
上一篇: SP2901 GEOPROB - One Geometry Problem 题解
下一篇: largrange 插值学习笔记
.SAI.
博客等级 码龄5年 835粉丝 63原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值