Atcoder Beginner Contest 309——D-F讲解

从数据到图片:手把手构建FMCW雷达人体行为识别数据集(行走、跌倒、喝水等6类动作) 本文详细解析了如何利用FMCW雷达构建人体行为识别数据集,涵盖行走、跌倒、喝水等6类动作。通过多普勒特征可视化、图像标准化处理和自动化流水线设计,将原始雷达信号转化为高质量时频图像数据集,为智能家居和健康监护等应用提供可靠数据支持。 阅读详情

前言

由于最近期末考试,所以之前几场都没打,给大家带了不便,非常抱歉。

这个暑假,我将会持续更新,并给大家带了更好理解的题解!希望大家多多支持。

由于, A ∼ C A\sim C A∼C 题比较简单,所以就不写了,如果大家有不会的,可以私信问我。


D - Add One Edge

1. Description

Problem Statement

We have an undirected graph with ( N 1 + N 2 ) (N_1+N_2) (N1​+N2​) vertices and M M M edges. For i = 1 , 2 , … , M i=1,2,\ldots,M i=1,2,…,M, the i i i-th edge connects vertex a i a_i ai​ and vertex b i b_i bi​.

The following properties are guaranteed:
Vertex u u u and vertex v v v are connected, for all integers u u u and v v v with 1 ≤ u , v ≤ N 1 1 \leq u,v \leq N_1 1≤u,v≤N1​.
Vertex u u u and vertex v v v are connected, for all integers u u u and v v v with N 1 + 1 ≤ u , v ≤ N 1 + N 2 N_1+1 \leq u,v \leq N_1+N_2 N1​+1≤u,v≤N1​+N2​.
Vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1​+N2​) are disconnected.
Consider performing the following operation exactly once:
choose an integer u u u with 1 ≤ u ≤ N 1 1 \leq u \leq N_1 1≤u≤N1​ and an integer v v v with N 1 + 1 ≤ v ≤ N 1 + N 2 N_1+1 \leq v \leq N_1+N_2 N1​+1≤v≤N1​+N2​, and add an edge connecting vertex u u u and vertex v v v.
We can show that vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1​+N2​) are always connected in the resulting graph; so let d d d be the minimum length (number of edges) of a path between vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1​+N2​).
Find the maximum possible d d d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices $u$ and $v$ of an undirected graph are said to be connected if and only if there is a path between vertex $u$ and vertex $v$. ### Constraints

1 ≤ N 1 , N 2 ≤ 1.5 × 1 0 5 1 \leq N_1,N_2 \leq 1.5 \times 10^5 1≤N1​,N2​≤1.5×105
0 ≤ M ≤ 3 × 1 0 5 0 \leq M \leq 3 \times 10^5 0≤M≤3×105
1 ≤ a i ≤ b i ≤ N 1 + N 2 1 \leq a_i \leq b_i \leq N_1+N_2 1≤ai​≤bi​≤N1​+N2​
( a i , b i ) ≠ ( a j , b j ) (a_i,b_i) \neq (a_j,b_j) (ai​,bi​)=(aj​,bj​) if i ≠ j i \neq j i=j.
Vertex u u u and vertex v v v are connected for all integers u u u and v v v such that 1 ≤ u , v ≤ N 1 1 \leq u,v \leq N_1 1≤u,v≤N1​.
Vertex u u u and vertex v v v are connected for all integers u u u and v v v such that N 1 + 1 ≤ u , v ≤ N 1 + N 2 N_1+1 \leq u,v \leq N_1+N_2 N1​+1≤u,v≤N1​+N2​.
Vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1​+N2​) are disconnected.
All input values are integers.

Input

The input is given from Standard Input in the following format:

N 1 N_1 N1​ N 2 N_2 N2​ M M M
a 1 a_1 a1​ b 1 b_1 b1​
⋮ \vdots ⋮
a M a_M aM​ b M b_M bM​

Output

Print the answer.

Sample Input 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

Sample Output 1

5

If we set u = 2 u=2 u=2 and v = 5 v=5 v=5, the operation yields d = 5 d=5 d=5, which is the maximum possible.

Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4

2. Solution


根据这个图以及题目大意,可以看出题目中所给的无向图一定是分成了两个连通块,并且起点 1 1 1,终点 N 1 + N 2 N_1+N_2 N1​+N2​一定不再同一个连通块内部。

(1)对于连通块 1 1 1,我们只需计算起点 1 1 1 到连通块内各个顶点的最短距离,取出最长的距离 r e s 1 res_1 res1​。
(2)对于连通块 2 2 2,我们只需计算终点 N 1 + N 2 N_1+N_2 N1​+N2​ 到连通块内各个顶点的最短距离,取出最长的距离 r e s 2 res_2 res2​。

最后的答案就是 r e s 1 + r e s 2 + 1 res_1+res_2+1 res1​+res2​+1,因为我们中间还要加一条边,所以距离还会多 1 1 1。


3.Code

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

const int N = 3e5 +10;

int n1, n2, m;
int u, v;
vector<int> g[N];
int dist[N], st[N];

void bfs(int start) //模版(不多说了,不会的可以问我)
{
	memset(dist, 0x3f, sizeof dist);
	memset(st, 0, sizeof st);
	queue<int> q;
	q.push(start);
	dist[start] = 0;
	
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		
		if (st[t]) continue;
		st[t] = 1;
		
		for (auto c : g[t])
			q.push(c), dist[c] = min(dist[c], dist[t] + 1);
	}
}

int main()
{
	cin >> n1 >> n2 >> m;
	
	while (m --)
	{
		cin >> u >> v;
		g[u].push_back(v), g[v].push_back(u);
	}
	
	bfs(1); //求连通块1内点1到其他点的最短距离
	int res1 = 0;
	for (int i = 1; i <= n1; i ++)
		res1 = max(res1, dist[i]);//取出到所有点中最短距离中最长的
		
	bfs(n1 + n2);//求连通块2内点N1+N2到其他点的最短距离
	int res2 = 0;
	for (int i = n1 + 1; i <= n1 + n2; i ++)
		res2 = max(res2, dist[i]); //取出到所有点中最短距离中最长的
		
	cout << res1 + res2 + 1 << endl;
	
	return 0;
}

4. Time Complexity: O ( N 1 + N 2 ) O(N_1+N_2) O(N1​+N2​)


E - Family and Insurance

1.Description

Problem Statement

There is a family consisting of person 1 1 1, person 2 2 2, … \ldots …, and person N N N. For i ≥ 2 i\geq 2 i≥2, person i i i’s parent is person p i p_i pi​.
They bought insurance M M M times. For i = 1 , 2 , … , M i=1,2,\ldots,M i=1,2,…,M, person x i x_i xi​ bought the i i i-th insurance, which covers that person and their descendants in the next y i y_i yi​ generations.
How many people are covered by at least one insurance?

Constraints

2 ≤ N ≤ 3 × 1 0 5 2 \leq N \leq 3 \times 10^5 2≤N≤3×105
1 ≤ M ≤ 3 × 1 0 5 1 \leq M \leq 3 \times 10^5 1≤M≤3×105
1 ≤ p i ≤ i − 1 1 \leq p_i \leq i-1 1≤pi​≤i−1
1 ≤ x i ≤ N 1 \leq x_i \leq N 1≤xi​≤N
1 ≤ y i ≤ 3 × 1 0 5 1 \leq y_i \leq 3 \times 10^5 1≤yi​≤3×105
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M
p 2 p_2 p2​ … \ldots … p N p_N pN​
x 1 x_1 x1​ y 1 y_1 y1​
⋮ \vdots ⋮
x M x_M xM​ y M y_M yM​

Output

Print the answer.

Sample Input 1

7 3
1 2 1 3 3 3
1 1
1 2
4 3

Sample Output 1

4

The 1 1 1-st insurance covers people 1 1 1, 2 2 2, and 4 4 4, because person 1 1 1’s 1 1 1-st generation descendants are people 2 2 2 and 4 4 4.

The 2 2 2-nd insurance covers people 1 1 1, 2 2 2, 3 3 3, and 4 4 4, because person 1 1 1’s 1 1 1-st generation descendants are people 2 2 2 and 4 4 4, and person 1 1 1’s 2 2 2-nd generation descendant is person 3 3 3.

The 3 3 3-rd insurance covers person 4 4 4, because person 4 4 4 has no 1 1 1-st, 2 2 2-nd, or 3 3 3-rd descendants.
Therefore, four people, people 1 1 1, 2 2 2, 3 3 3, and 4 4 4, are covered by at least one insurance.

Sample Input 2

10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1

Sample Output 2

10

2.Solution

在这里插入图片描述
这是样例中所对应的树。

这道题可以运用动态规划。
设 f i f_i fi​ 表示 i i i 号节点的保险还能延续到多少代。

那么转移就非常简单了:
f i = max ⁡ ( f i , f p i − 1 ) f_i = \max (f_i, f_{p_i} - 1) fi​=max(fi​,fpi​​−1) (注: p i p_i pi​ 即题目中所说的含义)。

之后,就进行对每一个点判断:
如果 f i ≥ 0 f_i\ge0 fi​≥0,那么 r e s + + res++ res++。

最后的答案就是 r e s res res。(不太明白的话,可以看看代码~~~)


3.Code

#include <iostream>
#include <cstring>

using namespace std;

const int N = 3e5 + 10;

int n, m;
int f[N], p[N];

int main()
{
	cin >> n >> m;
	
	p[1] = 1;
	for (int i = 2; i <= n; i ++)
		cin >> p[i];
	
	memset(f, -1, sizeof f);
		
	while (m --)
	{
		int x, y;
		
		cin >> x >> y;
		
		f[x] = max(f[x], y); //进行最初的赋值,表示x节点可以延续y代
	}
	
	for (int i = 1; i <= n; i ++)
		f[i] = max(f[i], f[p[i]] - 1);
		
	int res = 0;
	for (int i = 1; i <= n; i ++)
		if (f[i] >= 0)
			res ++;
			
	cout << res << endl;
}

4.Time Complexity: O ( N ) O(N) O(N)


F - Box in Box

1. Description

Problem Statement

There are N N N boxes. The i i i-th box has a shape of a rectangular cuboid whose height, width, and depth are h i , w i h_i,w_i hi​,wi​, and d i d_i di​, respectively.
Determine if there are two boxes such that one’s height, width, and depth are strictly greater than those of the other after rotating them if necessary.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2≤N≤2×105
1 ≤ h i , w i , d i ≤ 1 0 9 1 \leq h_i,w_i,d_i \leq 10^9 1≤hi​,wi​,di​≤109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
h 1 h_1 h1​ w 1 w_1 w1​ d 1 d_1 d1​
⋮ \vdots ⋮
h N h_N hN​ w N w_N wN​ d N d_N dN​

Output

Print Yes if there are two boxes such that one’s height, width, and depth are strictly greater than those of the other after rotating them if necessary; print No otherwise.

Sample Input 1

3
19 8 22
10 24 12
15 25 11

Sample Output 1

Yes

If you rotate the 2 2 2-nd box to swap its height and depth, the 3 3 3-rd box will have greater height, depth, and width.

Sample Input 2

3
19 8 22
10 25 12
15 24 11

Sample Output 2

No

Sample Input 3

2
1 1 2
1 2 2

Sample Output 3

No

2. Solution

都放在视频里拉~~~,不懂得话可以再来问我。

ABC 309 F题

线段树没有学的同学可以点这里!


3. Code

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;

int n;
struct Node1
{
	int h, w, d;
	void change() //保证h, w, d升序
	{
		if (h > w) swap(h, w);
		if (w > d) swap(w, d);
		if (h > w) swap(h, w);
	}
	bool operator< (const Node1 &t)const //重载小于号
	{
		if (t.h == h)
			return w > t.w;
		return h < t.h;
	}
}cube[N];
vector<int> discrete;
struct Node2
{
	int l, r;
	int mn;
}seg[8 * N]; //*8的原因是我们后面插入的时候为了离散化的方便,将Wi - 1也做成了节点

int find(int x) //离散化
{
	return lower_bound(discrete.begin(), discrete.end(), x) - discrete.begin() + 1;
}

void pushup(int u)
{
	seg[u].mn = min(seg[u << 1].mn, seg[u << 1 | 1].mn);
}

void build(int u, int l, int r) //建树
{
	if (l == r)
		seg[u] = {l, l, 0x3f3f3f3f};
	else
	{
		seg[u] = {l, r};
		int mid = l + r >> 1;
		build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
		pushup(u);
	}
}

void modify(int u, int x, int d) //单点修改
{
	if (seg[u].l == x && seg[u].r == x)
		seg[u].mn = min(seg[u].mn, d);
	else
	{
		int mid = seg[u].l + seg[u].r >> 1;
		if (x <= mid) modify(u << 1, x, d);
		else modify(u << 1 | 1, x, d);
		pushup(u);
	}
}

int query(int u, int l, int r) //区间查询
{
	if (seg[u].l >= l && seg[u].r <= r)
		return seg[u].mn;
		
	int mid = seg[u].l + seg[u].r >> 1, res = 0x3f3f3f3f;
	if (l <= mid) res = query(u << 1, l, r);
	if (r > mid) res = min(res, query(u << 1 | 1, l, r));
	
	return res;	
}

int main()
{
	cin >> n;
	
	for (int i = 1; i <= n; i ++)
		cin >> cube[i].h >> cube[i].w >> cube[i].d, cube[i].change();
		
	sort(cube + 1, cube + 1 + n);
	
	for (int i = 1; i <= n; i ++) discrete.push_back(cube[i].w), discrete.push_back(cube[i].w - 1);
	sort(discrete.begin(), discrete.end());
	discrete.erase(unique(discrete.begin(), discrete.end()), discrete.end());
	
	build(1, 0, discrete.size());
	
	for (int i = 1; i <= n; i ++)
	{
		int ans = query(1, 0, find(cube[i].w - 1));
		
		if (ans < cube[i].d)
		{
			cout << "Yes" << endl;
			return 0;
		}
		
		modify(1, find(cube[i].w), cube[i].d);
	}
	
	cout << "No" << endl;
}
东望苍苍,西望茫茫,叶落花凋泪满裳
从“能跑”到“好用”:一名 Python 开发者的工程化进阶之路【Python心得篇】 Python 早已不只是写脚本、跑数据的工具。后端服务AI Agent 编排层自动化平台核心语言数据与业务之间的“中枢层”区别只在于:你是用脚本方式,还是工程方式在写。如果你已经能写“能跑的 Python”,下一步,就是让它长期可跑、稳定可演进。Python 的真正价值不在于“写得快”,而在于能否支撑持续演进的工程实践。当代码从一次性脚本走向长期运行的系统,开发者必须从结构设计、职责拆分、性能意识和类型约束等方面进行系统性思考。 阅读详情

相关推荐

电容式触摸抗干扰设计:工业环境下的实践方案

针对工业环境中电容式touch器件易受干扰的问题,分享实用的硬件布局与软件滤波方案,提升touch响应稳定性和可靠性。

weixin_35755562的博客 847

Atcoder 309

Atcoder(abc309)

qq_62249357的博客 258

silvaco——GaN HEMT探索

上图为输出特性曲线 ,分别是在栅压为1v、3v、5v的情况下,可以看出在1v时器件处于截止状态,只有非常小近似于0的电流通过,随着栅压逐渐增大,沟道开启,产生漏极电流。上图为栅极漏电流曲线和转移特性曲线以及其提取的参数,可以看到阈值电压为=1.30249V,当栅压为0v时,Id=8.01032e-006,当栅压为5V时,Ig=3.11627e-006。albrct.n,低场迁移率模型,bn.albrct,an.albrct是albrct模型中的参数,可以由用户自己设定。lat.temp,晶格自热模型;

weixin_61913839的博客 7628

Atcoder Beginner 093 C 解题报告

这场是一场手速场...就是各位大佬A得飞快,我这种辣鸡...就很辣鸡...首先本人代码能力并不高...做题又慢...唉...最讨厌手速场了...说多了都是泪...T_T尽管A掉了三题,却排到700+...只涨了3分..........姑且说下C题吧...其实我C题一点没卡,一发AC...B题数组越界查bug查了一万年,A题开多组挫掉,抹掉多组秒A...唉,还是我能力差的缘故...不过相比去年,还算...

FrozenAllen的博客 312

股票交易接口TDX量化下单接口

对于需要同时操作多只股票的投资者而言,这一功能可以一次性处理多个交易指令,大大提高了交易的效率。通过回测,投资者可以发现策略中的不足之处,进而进行优化和调整,提高策略的可靠性。根据通达信提供的安装指南,下载相应的安装程序并进行安装。这包括设置交易账户的相关信息,如账户名、密码等,以及连接到正确的交易服务器。不同的情况可能会有不同的标准,有的可能要求账户使用达到一定的时长,有的可能对账户内的资产金额有最低要求。可以通过接口提供的监控功能或者第三方工具,实时查看交易状态、交易数据等信息,确保交易按照预期进行。

2401_88470113的博客 1292

025、Zephyr RTOS设备树覆盖与修改

本文分享了在Zephyr RTOS中使用设备树覆盖(overlay)的实战经验。作者通过一个工业数据采集项目中GPIO无法拉高的调试案例,揭示了设备树覆盖的"叠加"本质而非简单替换。文章详细介绍了覆盖文件的正确编写方式,包括节点引用、pinctrl配置等技巧,并总结了常见陷阱如状态设置不完整、别名冲突等问题。特别强调了条件覆盖的用法和调试方法,最后提出了覆盖文件管理的五项实用原则,强调设备树覆盖是硬件与软件之间的重要契约,需要谨慎对待。全文以实际项目为背景,提供了可操作性强的技术建议。

m0_50546716的博客 37

AtCoder Beginner Contest 317-332

AtCoder Beginner Contest 317-332

青少年趣味编程 884

RK3588 平台imx334 驱动调试记录

rk3588调试imx334

xiaoning132的专栏 1794

AtcoderABC309场

Snuke医生给Takahashi开了N种药物,从第一天(开药当天)开始,每天需要服用相应种类的药物。需要找到第一天或之后,他需要服用K颗或更少药丸的日期。给定一个N×N的方格矩阵,每个方格上放置一个整数。顺时针对最外边界方格进行移动1格操作,而内部的方格保持不变。可以使用一个二维数组来表示方格矩阵,然后遍历每个方格,判断是否为边界方格,如果是,则按规则进行移动,否则保持不变。创建一个大小为N×N的二维数组ans,用于存储移动后的方格矩阵。创建一个大小为N×N的二维数组a,用于存储输入的方格矩阵。

m0_73618658的博客 313

JavaScript实现边缘检测Canny(附完整源码)

JavaScript实现边缘检测Canny(附完整源码)

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

yolov11添加P2小目标检测头与添加P6超大目标检测头

本文介绍了YOLO目标检测模型中上下采样的原理及应用。上采样通过插值算法放大特征图,YOLO通常采用最近邻插值;下采样则通过带步长的卷积缩小特征图提取高层语义特征。文章详细阐述了这两种操作在YOLO中的实现方式、结构特点及对称性问题,指出下采样后会使用C3模块进行特征整合,上采样后通过concat抑制分割感。此外,还分析了不同分辨率特征图适合检测不同尺寸目标的原因,并提出添加4倍小目标检测头的方法,即对上采样后的特征进行concat和C3模块处理。最后给出了YOLOv5原模型结构和添加4倍检测头后的结构对比

m0_64661770的博客 824

[ABC309D] Add One Edge 题解

所在的连通块的最长路,答案即为两者之和加一。显然三个区域并不关联,我们贪心地让三个最大。由于需要跑 BFS 最短路,时间复杂度为。我们考虑以下一条最短路的构成,一定是。的边(即我们添加的那条边),一条。所在的连通块的最长路,再找一条。也就是说吗,我们找一条。

DengDuck蒟蒻之家 254

【第33话:车辆控制】自动驾驶车辆运动学模型推导及详解

自动驾驶车辆的运动学模型以自行车模型为核心,通过状态方程。

一碗白开水一 834

AtCoder Beginner Contest 309

AtCoder Beginner Contest 309

青少年趣味编程 994

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

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

AtCoder Beginner Contest 301

AtCoder Beginner Contest 301

青少年趣味编程 1576

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

AtCoder Beginner Contest 288

AtCoder Beginner Contest 288

青少年趣味编程 1626

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

AtCoder Beginner Contest 276

AtCoder Beginner Contest 276

青少年趣味编程 2131

AtCoder Beginner Contest 282

AtCoder Beginner Contest 282

青少年趣味编程 1554

AtCoder Beginner Contest 326

AtCoder Beginner Contest 326

青少年趣味编程 1775

AtCoder Beginner Contest 328

AtCoder Beginner Contest 328

青少年趣味编程 1885

AtCoder Beginner Contest 287

AtCoder Beginner Contest 287

青少年趣味编程 1865

AtCoder Beginner Contest 145

AtCoder Beginner Contest 145

青少年趣味编程 1833

AtCoder Beginner Contest 278

AtCoder Beginner Contest 278

青少年趣味编程 1736

AtCoder Beginner Contest 399

AtCoder Beginner Contest 399

青少年趣味编程 958

AtCoder Beginner Contest 174

AtCoder Beginner Contest 174

青少年趣味编程 1388

AtCoder Beginner Contest 397

AtCoder Beginner Contest 397

青少年趣味编程 1008
上一篇: Luogu P1280.尼克的任务
下一篇: AtCoder Regular Contest 164—— A~C题讲解
阿史大杯茶
博客等级 码龄5年 1267粉丝 158原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址