如何使用 pytorch 创建一个神经网络
构建神经网络
1 导入所需包
import osimport torchfrom torch import nnfrom torch.utils.data import DataLoaderfrom torchvision import datasets, transforms
2 检查GPU是否可用
device = ( "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")print(f"Using {device} device")print(torch.__version__)print(torch.cuda.is_available())print(torch.version.cuda)
Using cuda device
2.2.2+cu121
True
12.1
如果发现GPU不可用,可能是因为torch版本 ...
DL-notes:目录
目录
深度学习基础:线性神经网络、多层感知机
卷积神经网络:LeNet、AlexNet、VGG、Inception、ResNet
循环神经网络:RNN、GRU、LSTM、seq2seq
注意力机制:Attention、Transformer
优化算法:SGD、Momentum、Adam
高性能计算:并行、多GPU、分布式
计算机视觉:目标检测、语义分割
自然语言处理:词嵌入、BERT
杂谈
pytorch 环境安装
如何使用 pytorch 创建一个神经网络
参考
李宏毅深度学习教程LeeDL-Tutorial
邱锡鹏神经网络与深度学习NNDL
《动手学深度学习》 — 动手学深度学习 2.0.0 documentation (d2l.ai)
LeetCodeCookBook 刷题记录
C++ STL 刷题手册
作者的bb:一个月没动静了,赶紧活动起来啊!!!
有些简单介绍也不重复,参考cplusplus.com就可以了,我在这里就是记录一些笔记,方便刷题。
1. vector
size 和 capacity 的区别,如何动态删除元素、释放内存?
指针和引用的作用是什么?
shrink_to_fit 和 resize 有什么作用?
如何截取片段?
2. stack
3. queue
4. deque
5. priority_queue
6. map
K-V 键值对
7. set
K 集合
s.begin() //返回指向第一个元素的迭代器s.end() //返回指向最后一个元素的迭代器s.clear() //清除所有元素s.count() //返回某个值元素的个数s.empty() //如果集合为空,返回true,否则返回falses.equal_range() //返回集合中与给定值相等的上下限的两个迭代器s.erase() //删除集合中的元素s.find(k) //返回一个指向被查找到元素的 ...
ML-notes:SVM
6 SVM
单细胞数据分析
单细胞数据分析
自2013年被选为年度方法以来,单细胞技术已经足够成熟,可以为复杂的研究问题提供答案。随着单细胞分析技术的发展,从单细胞分析中收集的数据也显著增加,导致处理这些庞大而复杂的数据集的计算挑战。
单细胞RNA测序-数据分析
scanpy 是一个用于分析单细胞转录组(single cell rna sequencing) 数据的python库,文章2018发表在Genome Biology。它和seurat几乎大差不差,但是以Python的生态,完全可以认为其具有更大的扩展潜力。
安装环境(Scanpy单细胞测序学习-环境配置)
公共单细胞数据集
10X Genomics免费提供的外周血单核细胞(PBMC)数据集
Preprocessing and clustering 3k PBMCs (legacy workflow) — scanpy-tutorials 0.1.dev50+g06018e6 documentation
scanpy提供的公开数据集
Datasets — scanpy
开始
1. 载入包
# 载入包import numpy as npimport pan ...
ML-notes:人工神经网络
5 人工神经网络
本章讨论现阶段比较热门的一个监督学习算法————人工神经网络(artificial Neural Network)
神经网络是由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系统对真实世界物体所做的交互反应。
5.1 神经元模型 Neuron
神经网络中最基本的成分便是神经元(Neuron)模型,也就是上面说的适应性简单单元。在神经网络中,每个神经元都与其他神经元相连,当它“兴奋”时,都会向相连的神经元发送化学物质,改变相连的神经元内的电位;如果神经元电位超过了一个“阈值”(threshold),那么该神经元就会兴奋,所以整个神经网络就是通过兴奋和电位来传播信息。
5.1.1 M-P神经元模型
1943年一直沿用至今的 “M-P神经元模型” 便是对这个过程的抽象。
在这个模型中,
神经元收到了来自其他 n 个神经元传递过来的输入信号 xi
而这些输入信号通过带有权重的连接(connection),这些权重又叫连接权(connection weight)。
然后来到细胞体的前部分,它负责计算总输入值(输入信号的加权求和,累积电平)
然后到达后部 ...
ML-notes:决策树
4 决策树
4.1 基本概念
4.1.1 举例子
多分类问题实质上通过划分的方法转化为多个二分类问题进行求解。这次我们将讨论另一种被广泛使用的分类算法–决策树(Decision Tree)。
比如 一个相亲——母女对话:
女儿:多大年纪了?
母亲:26。
女儿:长的帅不帅?
母亲:挺帅的。
女儿:收入高不?
母亲:不算很高,中等情况。
女儿:是公务员不?
母亲:是,在税务局上班呢。
女儿:那好,我去见见。
此例子纯属虚构,不代表广大女性同胞的择偶标准。如有雷同纯属巧合。
我们就可以通过这段对话,画出一个决策树。
4.1.2 决策树
决策树(decision tree):是构建出的一个基于属性的树形
分类器。
每个非叶节点表示一个特征属性上的测试(分割)(判断)
每个分支代表这个特征属性在某个值域上的输出(分支)
每个叶节点存放一个类别(结果)
使用决策树进行决策的过程就是从根节点开始,测试待分类项中相应的特征属性,并按照其值选择输出分支,直到到达叶子节点,将叶子节点存放的类别作为决策结果。
4.2 决策树的构建
决策树的构建采用分治法的思想(递归)。而结束递归的条件如 ...
ML-notes:线性模型
3 线性模型
由于时间原因,这里只讲解部分内容
3.3 对数几率回归
虽然只讲这个,但是我们还是要提一提一些概念。
3.3.1 前情提要
线性模型:其实我们很早就已经与它打过交道,比如我们熟知的“最小二乘法”。这就是线性模型的经典算法之一:根据给定的(x,y)点对,求出一条与这些点拟合效果最好的直线y=ax+b。
线性回归:就是试图学到一个线性模型尽可能准确地预测新样本的输出值。
监督学习:1.绪论的方法分类有提到。
回归与分类:我们可以通过线性回归的思想来解决一些分类任务,比如二分类问题。
直观上说,可以规定直线上方的点为正样本(Positive) ,直线下方的点为负样本(Negative) 。本质上说,我们是需要把连续实数值转化为离散值的(例如: 𝟎, 𝟏):
比如:对于二分类任务,线性模型预测出来的是 连续值 z = wx + b,所以我们需要将 z 转换为 0/1 值,最理想的就是单位阶跃函数:
直观就是我们可以使用一个线性分类器𝒇(𝒙),当𝒙为正类样本,𝒇 (𝒙) > 𝟎,反之, 𝒙 为负类样本,则 𝒇 (� ...
ML-notes:模型评估与选择
2 模型评估与选择
1. 经验误差与过拟合
1.1 经验 Experiences
Experience = The data we have for training the machine learning model.
对于特定机器学习任务,已存在的可利用数据即是解决该机器学习任务的经验。而在这个大数据时代,大数据=丰富经验=训练更好的机器学习模型。
1.2 数据划分
通常我们会对获取到的数据进行数据划分,也就是我们绪论提到的一些学术用语:
训练集 Training Set:用来训练模型或确定参数。
测试集 Testing Set:测试已经训练好的模型的推广能力。
验证集 Validation Set(可选):是模型训练过程中单独留出的样本集,它可以用于调整模型的超参数和用于对模型的能力进行初步评估。用来做模型选择(Model Selection),即模型的最终优化及选择。
1.3 误差与精度
误差 Error
我们将学习器对样本的实际预测结果与样本的真实值之间的差异称之为误差(error)。而误差包含三类:
训练误差 training error 或 经验误差 e ...
ML-notes:绪论
1 绪论
1. 概念
机器学习是从人工智能中产生的一个重要学科分支,是实现智能化的关键。
机器学习是一类算法的总称,这些算法企图从大量历史数据中挖掘出其中隐含的规律,并用于预测或者分类,更具体的说,机器学习可以看作是寻找一个函数,输入是样本数据,输出是期望的结果,只是这个函数过于复杂,以至于不太方便形式化表达。也就是经典定义:利用经验改善系统自身的性能。(从 经验到数据 的过程)
在计算机系统中,经验通常以数据形式存在,因此,机器学习研究的主要内容,是关于在计算机上从数据中产生模型的算法,即学习算法。
“Learning is any process by which a system improves performance from experience.”
– Herbert Simon
Machine learning aims to study Approaches which improve the Performance of a machine at a specific Task with Experiences.
也就是说 机器学习 = 任务 + 方法 + ...