环境安装

windows

PyTorch环境配置及安装

1
conda install pytorch torchvision torchaudio torchtext cudatoolkit=10.1 -c pytorch

windows下安装anaconda+pytorch1.0+cuda10+配置pycharm

windows10安装cuda

显卡驱动版本一定要大于cudatoolkit版本
Ubuntu16.04下cuda和cudnn的卸载和升级和环境配置
用pip卸载,conda卸载不干净
查看pytorch对应的cuda版本

Mac:

Mac版本安装Anaconda及使用教程

Macbook air m1安装python/anaconda全过程(图文)

Mac M1安装anaconda与pycharm与pytorch与jupyter

torch.version.cuda

基础知识

batch_size的理解

PyTorch之前向传播函数forward

参考资料

简书笔记01

接口

torch.unsqueeze()

Dataloader重要参数与内部机制

torch.nn

PyTorch之前向传播函数forward

touch.nn.module

forward

torch.nn.Sequential()

torch.nn.Parameter()

就是将Tensor变成Variable

nn.Embedding

输入维度:batchSize * SeqLen

输出维度:batchSize * SeqLen * EmbeddingDim

torch.nn.LSTM()详解

输入维度

PyTorch中的nn.Conv1d与nn.Conv2d

torch.optim.lr_scheduler

lr_scheduler.StepLR调整学习率机制

torch.optim.lr_scheduler:调整学习率

优化器

简单认识Adam优化器

loss

nllloss

Pytorch详解NLLLoss和CrossEntropyLoss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import torch, torch.nn as nn
import torch.nn.functional as F

torch.random.manual_seed(2020)
input = torch.randn(4,3)
print(input)

sm = nn.Softmax(dim =0)
x0=sm(input)
print(x0)

sm = nn.Softmax(dim =1)
x1=sm(input)
print(x1)

x2 = torch.log(x1)
print(x2)

target = torch.tensor([0,2,1,2])
print(-(x2[0,0]+x2[1,2]+x2[2,1]+x2[3,2])/4)
print(F.nll_loss(x2, target))
print(F.nll_loss(x2, target, size_average = False))
print(F.nll_loss(x2, target, reduction = 'sum'))

# CrossEntropyLoss就是把以上Softmax–Log–NLLLoss合并成一步
loss = nn.CrossEntropyLoss()
print(loss(input, target))

transformers

官方文档

Pytorch:transforms的二十二个方法

Stanford parser

NLP工具——stanford Parser使用手册

2

(Java)利用Stanford parser与多线程获取语句中名词集合工具实现

Stanford Parser 标记含义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from stanfordcorenlp import StanfordCoreNLP
path = '../stanford-corenlp-4.2.2'
nlp = StanfordCoreNLP(path, lang='en')
s = 'Stanford University is located in California. It is a great university, founded in 1891.'

token = nlp.word_tokenize(s)
postag = nlp.pos_tag(s)
ner = nlp.ner(s)
parse = nlp.parse(s)
dependencyParse = nlp.dependency_parse(s)

print(' '.join(token))
print('|'.join([','.join(i) for i in postag]))
print('|'.join([','.join(i) for i in ner]))
print(parse)
for i, begin, end in dependencyParse:
print(i, '-'.join([str(begin), token[begin-1]]), '-'.join([str(end),token[end-1]]))

nlp.close()

torchcrf

pytorch-crf的使用

1
2
from torchcrf import CRF
pip install pytorch-crf==0.4.0

训练

torch.nn.DataParallel

Cuda out of memory

1
os.environ["CUDA_VISIBLE_DEVICES"] = '3,4,5'

Pytorch(五)入门:DataLoader 和 Dataset

多GPU训练

Batch

batch size过小,花费时间多,同时梯度震荡严重,不利于收敛;batch size过大,不同batch的梯度方向没有任何变化,容易陷入局部极小值。

经典模型

评估

NLP(二十三)序列标注算法评估模块seqeval的使用 - 山阴少年 - 博客园 (cnblogs.com)

Macro-F1: macro f1需要先计算出每一个类别的准召及其f1 score,然后通过求均值得到在整个样本上的f1 score.

Micro-F1: micro f1不需要区分类别,直接使用总体样本的准召计算f1 score.

Weighted-F1: 在macro f1基础上,考虑 每一类别 的个数,进行加权平均。

micro-f1 & macro-f1 & weighted-f1

常用

Anaconda设置CUDA版本和系统默认版本共存

pandas

获取当前目录路径和文件

python标准库之glob介绍

读取excel文件库xlrd

写excel库xlwt

Python操作excel:用xlwt设置excel单元格背景颜色,给字体加粗

读写csv

1
2
3
4
5
6
7
8
9
10
11
12
import csv
ine_num=2
file_path = ""
with open(file_path, 'r') as f:
reader = csv.reader(f)
print(type(reader))
i=0
for row in reader:
i+=1
print(row)
if i==ine_num:
break

TEMP

torch.Tensor.detach()

torch.cat()

从ReLU到GELU,一文概览神经网络的激活函数

项目实战

文本分类

NLP 找门:用朴素贝叶斯进行文本分类

BERT文本分类

PyTorch环境下对BERT进行Fine-tuning

文本分类tensorflow

Others