【Pytorch】RNN for Name Classification

在这里插入图片描述

参考学习来自:

  • https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
  • RNN完成姓名分类
  • https://download.pytorch.org/tutorial/data.zip

导入库

import glob # 用于查找符合规则的文件名
import os
import unicodedata
import string
import torch
import torch.nn as nn
import torch.optim as optim
import random

GPU 配置

"Device configuration"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

导入数据

在这里插入图片描述

"导入数据"
def findFiles(path):return glob.glob(path)for i in findFiles('./data/names/*.txt'):print(i)

output

./data/names/Korean.txt
./data/names/Irish.txt
./data/names/Portuguese.txt
./data/names/Vietnamese.txt
./data/names/Czech.txt
./data/names/Russian.txt
./data/names/Scottish.txt
./data/names/German.txt
./data/names/Polish.txt
./data/names/Spanish.txt
./data/names/English.txt
./data/names/French.txt
./data/names/Japanese.txt
./data/names/Dutch.txt
./data/names/Greek.txt
./data/names/Chinese.txt
./data/names/Italian.txt
./data/names/Arabic.txt

查看名字所有字符

all_letters = string.ascii_letters + " .,;'" #所有的字母和标点
print(all_letters)  # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'n_letters = len(all_letters)
print(n_letters)  # 57

字符都转化为 ASCII 形式

# Turn a Unicode string to plain ASCII
# Thanks to https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):return ''.join(c for c in unicodedata.normalize('NFD', s)if unicodedata.category(c) != 'Mn'and c in all_letters)
print(unicodeToAscii('Ślusàrski'))  # Slusarskicategory_lines = {}  # 每个语言下的名字
all_categories = []  # 保存所有的语言的名字# 读文件, 返回文件中的每一个
def readLines(filename):# 读文件, 去空格, 按换行进行划分lines = open(filename, encoding='utf-8').read().strip().split('\n')return [unicodeToAscii(line) for line in lines]# 顺序读取每一个文件, 保存其中的姓名
for filename in findFiles('./data/names/*.txt'):# basename返回基础的文件名# splitext将文件名和后缀分开category = os.path.splitext(os.path.basename(filename))[0]all_categories.append(category)lines = readLines(filename)category_lines[category] = linesn_categories = len(all_categories)
# 一共有18种语言的名字
print(len(category_lines))  # 18# 查看某种语言的名字
print(category_lines.keys())
"""
dict_keys(['Korean', 'Irish', 'Portuguese', 'Vietnamese', 'Czech', 'Russian', 'Scottish', 'German', 'Polish', 
'Spanish', 'English', 'French', 'Japanese', 'Dutch', 'Greek', 'Chinese', 'Italian', 'Arabic'])
"""
print(category_lines['English'][:5])  # ['Abbas', 'Abbey', 'Abbott', 'Abdi', 'Abel']

名字转化为 Tensor

"将Name转为Tensor"
# Find letter index from all_letters, e.g. "a"=0
def letterToIndex(letter):# 返回字母在字母表中的位置# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'return all_letters.find(letter)# 将整个名字转为<line_length x 1 x n_letters>
def lineToTensor(line):tensor = torch.zeros(len(line), 1, n_letters)for li, letter in enumerate(line):tensor[li][0][letterToIndex(letter)] = 1return tensorprint(lineToTensor('Bryant').shape)  # torch.Size([6, 1, 57])
print(lineToTensor('Mike').shape)  # torch.Size([4, 1, 57])
print(lineToTensor('Mike'))

output

tensor([[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]]])

定义 RNN 分类网络

"定义网络"
class RNN_NAME(nn.Module):def __init__(self, input_size, hidden_size, output_size, layers, batch_size):super(RNN_NAME, self).__init__()# 这里的input_size相当于是特征的个数, 这里即是所有字母的个数57self.input_size = input_size# 这里相当于是rnn输出的维数self.hidden_size = hidden_size# 这里是分类的个数, 这里相当于是18, 一共有18类self.output_size = output_size# 这里是rnn的层数self.layers = layers# batchsize的个数self.batch_size = batch_sizeself.gru = nn.GRU(input_size = self.input_size, hidden_size = self.hidden_size, num_layers = self.layers)self.FC = nn.Linear(self.hidden_size, self.output_size)def init_hidden(self):return torch.zeros(self.layers, self.batch_size, self.hidden_size).to(device)def forward(self, x):self.batch_size = x.size(1)self.hidden = self.init_hidden() # 初始化memory的内容rnn_out, self.hidden = self.gru(x,self.hidden)out = self.FC(rnn_out[-1])return out

RNN 网络初始化

"初始化"
layers = 3
input_size = n_letters
hidden_size = 128
output_size = len(all_categories) # 18类
batch_size = 1  # 因为这里name的长度不相同, 所以将batch_size设为1
# 网络初始化
rnn_name = RNN_NAME(input_size, hidden_size, output_size, layers, batch_size).to(device)
# 测试网络输入输出
input_data = lineToTensor('Mike')
out_data = rnn_name(input_data.to(device))
print(out_data.size())
# torch.Size([1, 18])
print(out_data)
"""
tensor([[ 0.0051, -0.0081, -0.0906,  0.1054,  0.0679, -0.0187, -0.0497, -0.0349,-0.0466,  0.0004, -0.0099, -0.0940, -0.0201, -0.0694, -0.0542, -0.0807,0.0005,  0.1373]], grad_fn=<AddmmBackward0>)
"""

准备训练数据集

"准备训练数据"
# 打印最后显示的结果def categoryFromOutput(output):top_n, top_i = output.topk(1)category_i = top_i[0].item()return all_categories[category_i], category_i# Get a random training example
def randomChoice(l):# 随机返回一个l中的分类return l[random.randint(0, len(l)-1)]def randomTrainingExample():"""随机挑选一种语言的一个名字, 用来产生训练需要的数据"""category = randomChoice(all_categories)  # 随机选一个语言line = randomChoice(category_lines[category])  # 随机从这个语言里选一个名字category_tensor = torch.tensor([all_categories.index(category)]).long() # 将分类转为Tensorline_tensor = lineToTensor(line)  # 将名字转为Tensorreturn category, line, category_tensor, line_tensorfor i in range(10):category, line, category_tensor, line_tensor = randomTrainingExample()print('category = ', category, '/ line = ', line)
"""
category =  Scottish / line =  Miller
category =  Irish / line =  O'Mooney
category =  Dutch / line =  Kools
category =  French / line =  Bouchard
category =  French / line =  Masson
category =  English / line =  May
category =  Spanish / line =  Holguin
category =  Portuguese / line =  Moreno
category =  Greek / line =  Nomikos
category =  Czech / line =  Cernohous
"""

计算预测精度

"计算精度"
def get_accuracy(logit, target):corrects = (torch.max(logit, 1)[1].view(target.size()).data == target.data).sum()accuracy = 100.0 * corrects/batch_sizereturn accuracy.item()

网络训练

"训练"criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(rnn_name.parameters(),lr=0.005)
N_EPHOCS = 100
# 共有20074个数据
n_iters = 2000  # 训练n_iters个名字
print_every = 200  # 每print_every轮打印一次结果, 并传递一次误差, 更新系数
for epoch in range(N_EPHOCS):train_running_loss = 0.0loss = torch.tensor([0.0]).float().to(device)train_count = 0rnn_name.train()# trainging roundfor iter in range(1, n_iters+1):# 获取样本category, line, category_tensor, line_tensor = randomTrainingExample()category_tensor = category_tensor.to(device)line_tensor = line_tensor.to(device)# 进行训练optimizer.zero_grad()# reset hidden statesrnn_name.hidden = rnn_name.init_hidden()# forward+backward+optimizeoutputs = rnn_name(line_tensor)guess, guess_i = categoryFromOutput(outputs)if guess == category:train_count = train_count + 1# print(iter)loss = loss + criterion(outputs, category_tensor)if iter % print_every == 0:correct = '✓' if guess == category else '✗ ({})'.format(category)print('{} /{} {}'.format(line, guess, correct))# 进行反向传播(print_every次传一次误差)loss.backward()optimizer.step()train_running_loss = train_running_loss + loss.detach().item()loss = torch.tensor([0.0]).float().to(device)# 查看准确率train_acc = train_count / n_iters * 100print('=======')print('Epoch : {:0>2d} | Loss : {:<6.4f} | Train Accuracy : {:<6.2f}%'.format(epoch, train_running_loss/n_iters, train_acc))print('=======')"验证"
test_count = 0
test_num = 500 # 测试500个名字
rnn_name.eval()
for test_iter in range(1, test_num+1):category, line, category_tensor, line_tensor = randomTrainingExample()outputs = rnn_name(line_tensor.to(device))guess, guess_i = categoryFromOutput(outputs)if guess == category:test_count = test_count + 1
print('Test Accuracy : {:<6.4f}%'.format(test_count/test_num*100))
"""
=======
Epoch : 71 | Loss : 0.2698 | Train Accuracy : 92.35 %
=======
Konda /Japanese ✓
Seighin /Irish ✓
Sedmik /Czech ✓
Jestkov /Russian ✓
So /Korean ✓
Laar /Dutch ✓
Mazza /Italian ✓
Abl /Czech ✓
Doyle /Irish ✓
Skwor /Czech ✓
=======
Epoch : 72 | Loss : 0.2583 | Train Accuracy : 91.40 %
=======
Martoyas /Greek ✗ (Russian)
Hallett /English ✓
Bermudez /Spanish ✓
Bonhomme /French ✓
Lam /Chinese ✗ (Vietnamese)
Kourempes /Greek ✓
Matsuki /Japanese ✓
Sokolofsky /Polish ✓
Kuipers /Dutch ✓
Aller /Dutch ✓
=======
Epoch : 73 | Loss : 0.2564 | Train Accuracy : 91.65 %
=======
Xiao /Chinese ✓
Lian /Chinese ✓
Prill /Czech ✓
Glatter /Czech ✓
Bureau /French ✓
Vallance /French ✗ (English)
Johnstone /Scottish ✓
Kassab /Arabic ✓
Atiyeh /Arabic ✓
Luong /Vietnamese ✓
=======
Epoch : 74 | Loss : 0.2610 | Train Accuracy : 91.60 %
=======
Poirier /French ✓
Hahn /German ✓
Botros /Arabic ✓
Adrichem /Dutch ✓
Rios /Portuguese ✗ (Spanish)
Zangari /Italian ✓
Mccallum /Scottish ✓
Ferro /Portuguese ✓
Kim /Vietnamese ✗ (Korean)
Chang /Korean ✓
=======
Epoch : 75 | Loss : 0.2792 | Train Accuracy : 90.85 %
=======
Monte /Italian ✓
Han /Vietnamese ✗ (Korean)
Ying /Chinese ✓
O'Driscoll /Irish ✓
Poirier /French ✓
Tsuruya /Japanese ✓
Ralph /Czech ✗ (English)
Bonheur /French ✓
Romagna /Italian ✓
Krakowski /Polish ✓
=======
Epoch : 76 | Loss : 0.3000 | Train Accuracy : 90.85 %
=======
Fay /French ✓
Plisek /Czech ✓
Travere /French ✓
Moon /Korean ✓
Aalst /Dutch ✓
Sassa /Japanese ✓
Holzmann /German ✓
Kruessel /Czech ✓
Truong /Vietnamese ✓
Kassab /Arabic ✓
=======
Epoch : 77 | Loss : 0.2602 | Train Accuracy : 92.00 %
=======
Janda /Polish ✓
Doan /Vietnamese ✓
O'Reilly /Irish ✓
Torres /Portuguese ✓
Esipovich /Russian ✓
Johnston /Scottish ✓
Viteri /Spanish ✗ (Italian)
Vandroogenbroeck /Dutch ✓
Montero /Spanish ✓
Wan /Chinese ✓
=======
Epoch : 78 | Loss : 0.2460 | Train Accuracy : 92.35 %
=======
Ha /Korean ✗ (Vietnamese)
Christie /Scottish ✓
Pereira /Portuguese ✓
Kuiper /Dutch ✓
Bautista /Spanish ✓
Takewaki /Japanese ✓
Ying /Chinese ✓
Sokoloff /Polish ✓
Raghailligh /Irish ✓
Edgell /English ✓
=======
Epoch : 79 | Loss : 0.2640 | Train Accuracy : 91.60 %
=======
Neal /English ✓
Kafka /Czech ✓
Hay /Scottish ✓
Pelletier /French ✓
Wilchek /Czech ✓
Marchesi /Italian ✓
Michel /Spanish ✓
Ling /Chinese ✓
Zou /Chinese ✓
Freitas /Portuguese ✓
=======
Epoch : 80 | Loss : 0.2579 | Train Accuracy : 92.60 %
=======
Kelly /Scottish ✓
Chou /Korean ✓
Unsworth /English ✓
Larue /French ✓
Tron /Vietnamese ✓
Herodes /Czech ✓
Fremut /Czech ✓
Thai /Vietnamese ✓
Clark /Irish ✗ (Scottish)
Halenkov /Russian ✓
=======
Epoch : 81 | Loss : 0.2453 | Train Accuracy : 92.40 %
=======
Than /Vietnamese ✓
Pfeifer /Czech ✓
Kennedy /Scottish ✓
Klerkx /Dutch ✓
Freitas /Portuguese ✓
Miller /Scottish ✓
Deeb /Arabic ✓
Almetov /Russian ✓
Li /Korean ✓
Perez /Spanish ✓
=======
Epoch : 82 | Loss : 0.2579 | Train Accuracy : 91.85 %
=======
Horiatis /Greek ✓
Clowes /English ✓
Santos /Portuguese ✓
Mochan /Irish ✓
Sokolof /Polish ✓
Ogterop /Dutch ✓
Schwartz /Czech ✓
Abadi /Arabic ✓
Winogrodzki /Polish ✓
Bazzhin /Russian ✓
=======
Epoch : 83 | Loss : 0.2397 | Train Accuracy : 92.25 %
=======
Ybarra /Spanish ✓
Jacobson /English ✓
Shi /Chinese ✓
Berg /Dutch ✓
Araya /Spanish ✓
Banos /Greek ✓
Winograd /Polish ✓
Schneider /German ✗ (Czech)
Calligaris /Italian ✓
Hasbulatov /Russian ✓
=======
Epoch : 84 | Loss : 0.2466 | Train Accuracy : 92.25 %
=======
Felkerzam /Polish ✗ (Russian)
Gomolka /Polish ✓
Belo /Portuguese ✓
Santos /Portuguese ✓
Rim /Korean ✓
Solomon /French ✓
Agnellutti /Italian ✓
Santana /Portuguese ✓
Ramaker /Dutch ✓
Palmisano /Italian ✓
=======
Epoch : 85 | Loss : 0.2390 | Train Accuracy : 92.80 %
=======
Moraitopoulos /Greek ✓
Rosenberger /German ✓
Torres /Portuguese ✓
Sabbagh /Arabic ✓
Aggelen /Dutch ✓
Chieu /Chinese ✓
Ukhtomsky /Russian ✓
Kouros /Greek ✓
Portyansky /Russian ✓
Dinh /Vietnamese ✓
=======
Epoch : 86 | Loss : 0.2173 | Train Accuracy : 93.15 %
=======
Jaskulski /Polish ✓
Mendez /Spanish ✓
Smyth /English ✓
Mo /Korean ✓
Seegers /Dutch ✓
Elford /English ✓
Lau /Chinese ✓
Sargent /French ✓
Zeman /Czech ✓
Ohme /German ✓
=======
Epoch : 87 | Loss : 0.2189 | Train Accuracy : 93.00 %
=======
Gwang  /Korean ✓
Zhernakov /Russian ✓
Quraishi /Arabic ✓
Asker /Arabic ✓
Franco /Portuguese ✓
Bacon /Czech ✓
Weineltk /Czech ✓
Thuy /Vietnamese ✓
Close /Greek ✓
Xiang /Chinese ✓
=======
Epoch : 88 | Loss : 0.2497 | Train Accuracy : 91.95 %
=======
Basara /Arabic ✓
Esteves /Portuguese ✓
Metz /German ✓
Lopez /Spanish ✓
Bukoski /Polish ✓
Rao /Italian ✓
Klerk /Dutch ✓
Espinoza /Spanish ✓
Wehunt /German ✓
Giehl /German ✓
=======
Epoch : 89 | Loss : 0.2454 | Train Accuracy : 92.70 %
=======
Deniau /French ✓
Bover /Spanish ✗ (Italian)
Soho /Japanese ✓
Ron /Korean ✓
Dolezal /Czech ✓
Yamamura /Japanese ✓
Ramsay /Scottish ✓
Fromm /German ✓
Huerta /Spanish ✓
Colman /Irish ✓
=======
Epoch : 90 | Loss : 0.2432 | Train Accuracy : 92.55 %
=======
Fei /Chinese ✓
Dinh /Vietnamese ✓
Krawiec /Czech ✓
Urbanek /Czech ✓
Yan /Chinese ✓
Giehl /German ✓
Riain /Irish ✓
Zhestkov /Russian ✓
Almetiev /Russian ✓
Deniel /French ✓
=======
Epoch : 91 | Loss : 0.2318 | Train Accuracy : 92.45 %
=======
Macleod /Scottish ✓
Savatier /French ✓
Chong /Chinese ✗ (Korean)
Prchal /Czech ✓
Tsoumada /Greek ✓
Agosti /Italian ✓
Mojjis /Czech ✓
Mitsui /Japanese ✓
San nicolas /Spanish ✓
Kokoris /Greek ✓
=======
Epoch : 92 | Loss : 0.2163 | Train Accuracy : 92.25 %
=======
Marubeni /Japanese ✓
Suh /Korean ✓
Klerks /Dutch ✓
Roma /Spanish ✓
Lac /Vietnamese ✓
Khoury /Arabic ✓
Hoefler /German ✓
Gibson /Scottish ✓
Jasso /Spanish ✓
Oirschotten /Dutch ✓
=======
Epoch : 93 | Loss : 0.2329 | Train Accuracy : 92.90 %
=======
Kikuchi /Japanese ✓
Blecher /German ✓
Ying /Chinese ✓
Nahas /Arabic ✓
Araujo /Portuguese ✓
Koemans /Dutch ✓
Durant /French ✓
Martzenyuk /Russian ✓
Petru /Czech ✓
Wood /Scottish ✓
=======
Epoch : 94 | Loss : 0.2080 | Train Accuracy : 93.35 %
=======
Vespa /Italian ✓
Kowalczyk /Polish ✓
Nahas /Arabic ✓
Tresler /German ✓
Caivano /Italian ✓
Pho /Vietnamese ✓
Dael /Dutch ✓
Heidl /Czech ✓
Totolos /Greek ✓
Adlersflugel /German ✓
=======
Epoch : 95 | Loss : 0.2400 | Train Accuracy : 92.45 %
=======
Zwolenksy /Czech ✓
Chermak /Czech ✓
Sgro /Italian ✓
Lestrange /French ✓
Hwang /Korean ✓
Barros /Spanish ✗ (Portuguese)
Boyle /Scottish ✓
Schmeling /German ✓
Poplawski /Polish ✓
Pantelas /Greek ✓
=======
Epoch : 96 | Loss : 0.2150 | Train Accuracy : 93.10 %
=======
Asher /English ✓
Sai /Vietnamese ✓
Hakimi /Arabic ✓
Sheng /Chinese ✓
Lebedevich /Russian ✓
Castellano /Spanish ✓
Battaglia /Italian ✓
Cardozo /Portuguese ✓
Sinagra /Italian ✓
Bumgarner /German ✓
=======
Epoch : 97 | Loss : 0.2359 | Train Accuracy : 92.80 %
=======
Lyes /English ✓
Do /Vietnamese ✓
Busch /German ✓
Shimura /Japanese ✓
O'Connor /Irish ✓
Geiger /German ✓
Reier /German ✓
Paris /French ✓
Sokolof /Polish ✓
Limarev /Russian ✓
=======
Epoch : 98 | Loss : 0.2369 | Train Accuracy : 92.55 %
=======
Osladil /Czech ✓
Roy /French ✓
Ortega /Spanish ✓
Ruzzier /Italian ✓
Chou /Korean ✓
Mateus /Portuguese ✓
Ibanez /Spanish ✓
Cabello /Spanish ✓
Duguay /French ✓
Yi /Korean ✓
=======
Epoch : 99 | Loss : 0.2192 | Train Accuracy : 92.65 %
=======
Test Accuracy : 94.8000%
"""

精度不足 95%

下面写写测试流程

"单独测试"
input_data = lineToTensor('WMN').to(device)
out_data = rnn_name(input_data)
print(out_data)
print(categoryFromOutput(out_data))
"""
tensor([[ 5.7034,  1.0659, -8.1210,  0.5218, -2.5818,  0.1362,  7.4420,  3.9547,1.0824, -7.2981,  4.0036, -2.2944,  1.6835, -3.5377, -7.6178,  9.8920,-0.5789, -1.0545]], device='cuda:0', grad_fn=<AddmmBackward0>)
('Chinese', 15)
"""input_data = lineToTensor('Huang').to(device)
out_data = rnn_name(input_data)
print(out_data)
print(categoryFromOutput(out_data))
"""
tensor([[ 4.1755, -1.2160, -3.8454,  5.4587, -4.6412,  0.8137,  1.2896,  1.7429,-1.3145, -2.1634,  1.0224, -2.7052,  2.1592,  1.1909, -8.0874, 10.2988,-1.9252, -0.0729]], device='cuda:0', grad_fn=<AddmmBackward0>)
('Chinese', 15)
"""

完整代码

import glob # 用于查找符合规则的文件名
import os
import unicodedata
import string
import torch
import torch.nn as nn
import torch.optim as optim
import random"Device configuration"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")"导入数据"
def findFiles(path):return glob.glob(path)for i in findFiles('./data/names/*.txt'):print(i)"""
./data/names/Korean.txt
./data/names/Irish.txt
./data/names/Portuguese.txt
./data/names/Vietnamese.txt
./data/names/Czech.txt
./data/names/Russian.txt
./data/names/Scottish.txt
./data/names/German.txt
./data/names/Polish.txt
./data/names/Spanish.txt
./data/names/English.txt
./data/names/French.txt
./data/names/Japanese.txt
./data/names/Dutch.txt
./data/names/Greek.txt
./data/names/Chinese.txt
./data/names/Italian.txt
./data/names/Arabic.txt
"""all_letters = string.ascii_letters + " .,;'" #所有的字母和标点
print(all_letters)  # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'n_letters = len(all_letters)
print(n_letters)  # 57# Turn a Unicode string to plain ASCII
# Thanks to https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):return ''.join(c for c in unicodedata.normalize('NFD', s)if unicodedata.category(c) != 'Mn'and c in all_letters)
print(unicodeToAscii('Ślusàrski'))  # Slusarskicategory_lines = {}  # 每个语言下的名字
all_categories = []  # 保存所有的语言的名字# 读文件, 返回文件中的每一个
def readLines(filename):# 读文件, 去空格, 按换行进行划分lines = open(filename, encoding='utf-8').read().strip().split('\n')return [unicodeToAscii(line) for line in lines]# 顺序读取每一个文件, 保存其中的姓名
for filename in findFiles('./data/names/*.txt'):# basename返回基础的文件名# splitext将文件名和后缀分开category = os.path.splitext(os.path.basename(filename))[0]all_categories.append(category)lines = readLines(filename)category_lines[category] = linesn_categories = len(all_categories)
# 一共有18种语言的名字
print(len(category_lines))  # 18# 查看某种语言的名字
print(category_lines.keys())
"""
dict_keys(['Korean', 'Irish', 'Portuguese', 'Vietnamese', 'Czech', 'Russian', 'Scottish', 'German', 'Polish', 
'Spanish', 'English', 'French', 'Japanese', 'Dutch', 'Greek', 'Chinese', 'Italian', 'Arabic'])
"""
print(category_lines['English'][:5])  # ['Abbas', 'Abbey', 'Abbott', 'Abdi', 'Abel']"将Name转为Tensor"
# Find letter index from all_letters, e.g. "a"=0
def letterToIndex(letter):# 返回字母在字母表中的位置# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'return all_letters.find(letter)# 将整个名字转为<line_length x 1 x n_letters>
def lineToTensor(line):tensor = torch.zeros(len(line), 1, n_letters)for li, letter in enumerate(line):tensor[li][0][letterToIndex(letter)] = 1return tensorprint(lineToTensor('Bryant').shape)  # torch.Size([6, 1, 57])
print(lineToTensor('Mike').shape)  # torch.Size([4, 1, 57])
print(lineToTensor('Mike'))"""
tensor([[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]],[[0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0.]]])
""""定义网络"
class RNN_NAME(nn.Module):def __init__(self, input_size, hidden_size, output_size, layers, batch_size):super(RNN_NAME, self).__init__()# 这里的input_size相当于是特征的个数, 这里即是所有字母的个数57self.input_size = input_size# 这里相当于是rnn输出的维数self.hidden_size = hidden_size# 这里是分类的个数, 这里相当于是18, 一共有18类self.output_size = output_size# 这里是rnn的层数self.layers = layers# batchsize的个数self.batch_size = batch_sizeself.gru = nn.GRU(input_size = self.input_size, hidden_size = self.hidden_size, num_layers = self.layers)self.FC = nn.Linear(self.hidden_size, self.output_size)def init_hidden(self):return torch.zeros(self.layers, self.batch_size, self.hidden_size).to(device)def forward(self, x):self.batch_size = x.size(1)self.hidden = self.init_hidden() # 初始化memory的内容rnn_out, self.hidden = self.gru(x,self.hidden)out = self.FC(rnn_out[-1])return out"初始化"
layers = 3
input_size = n_letters
hidden_size = 128
output_size = len(all_categories) # 18类
batch_size = 1  # 因为这里name的长度不相同, 所以将batch_size设为1
# 网络初始化
rnn_name = RNN_NAME(input_size, hidden_size, output_size, layers, batch_size).to(device)
# 测试网络输入输出
input_data = lineToTensor('Mike')
out_data = rnn_name(input_data.to(device))
print(out_data.size())
# torch.Size([1, 18])
print(out_data)
"""
tensor([[ 0.0051, -0.0081, -0.0906,  0.1054,  0.0679, -0.0187, -0.0497, -0.0349,-0.0466,  0.0004, -0.0099, -0.0940, -0.0201, -0.0694, -0.0542, -0.0807,0.0005,  0.1373]], grad_fn=<AddmmBackward0>)
""""准备训练数据"
# 打印最后显示的结果def categoryFromOutput(output):top_n, top_i = output.topk(1)category_i = top_i[0].item()return all_categories[category_i], category_i# Get a random training example
def randomChoice(l):# 随机返回一个l中的分类return l[random.randint(0, len(l)-1)]def randomTrainingExample():"""随机挑选一种语言的一个名字, 用来产生训练需要的数据"""category = randomChoice(all_categories)  # 随机选一个语言line = randomChoice(category_lines[category])  # 随机从这个语言里选一个名字category_tensor = torch.tensor([all_categories.index(category)]).long() # 将分类转为Tensorline_tensor = lineToTensor(line)  # 将名字转为Tensorreturn category, line, category_tensor, line_tensorfor i in range(10):category, line, category_tensor, line_tensor = randomTrainingExample()print('category = ', category, '/ line = ', line)
"""
category =  Scottish / line =  Miller
category =  Irish / line =  O'Mooney
category =  Dutch / line =  Kools
category =  French / line =  Bouchard
category =  French / line =  Masson
category =  English / line =  May
category =  Spanish / line =  Holguin
category =  Portuguese / line =  Moreno
category =  Greek / line =  Nomikos
category =  Czech / line =  Cernohous
""""计算精度"
def get_accuracy(logit, target):corrects = (torch.max(logit, 1)[1].view(target.size()).data == target.data).sum()accuracy = 100.0 * corrects/batch_sizereturn accuracy.item()"训练"criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(rnn_name.parameters(),lr=0.005)
N_EPHOCS = 100
# 共有20074个数据
n_iters = 2000  # 训练n_iters个名字
print_every = 200  # 每print_every轮打印一次结果, 并传递一次误差, 更新系数
for epoch in range(N_EPHOCS):train_running_loss = 0.0loss = torch.tensor([0.0]).float().to(device)train_count = 0rnn_name.train()# trainging roundfor iter in range(1, n_iters+1):# 获取样本category, line, category_tensor, line_tensor = randomTrainingExample()category_tensor = category_tensor.to(device)line_tensor = line_tensor.to(device)# 进行训练optimizer.zero_grad()# reset hidden statesrnn_name.hidden = rnn_name.init_hidden()# forward+backward+optimizeoutputs = rnn_name(line_tensor)guess, guess_i = categoryFromOutput(outputs)if guess == category:train_count = train_count + 1# print(iter)loss = loss + criterion(outputs, category_tensor)if iter % print_every == 0:correct = '✓' if guess == category else '✗ ({})'.format(category)print('{} /{} {}'.format(line, guess, correct))# 进行反向传播(print_every次传一次误差)loss.backward()optimizer.step()train_running_loss = train_running_loss + loss.detach().item()loss = torch.tensor([0.0]).float().to(device)# 查看准确率train_acc = train_count / n_iters * 100print('=======')print('Epoch : {:0>2d} | Loss : {:<6.4f} | Train Accuracy : {:<6.2f}%'.format(epoch, train_running_loss/n_iters, train_acc))print('=======')"验证"
test_count = 0
test_num = 500 # 测试500个名字
rnn_name.eval()
for test_iter in range(1, test_num+1):category, line, category_tensor, line_tensor = randomTrainingExample()outputs = rnn_name(line_tensor.to(device))guess, guess_i = categoryFromOutput(outputs)if guess == category:test_count = test_count + 1
print('Test Accuracy : {:<6.4f}%'.format(test_count/test_num*100))
"""
=======
Epoch : 71 | Loss : 0.2698 | Train Accuracy : 92.35 %
=======
Konda /Japanese ✓
Seighin /Irish ✓
Sedmik /Czech ✓
Jestkov /Russian ✓
So /Korean ✓
Laar /Dutch ✓
Mazza /Italian ✓
Abl /Czech ✓
Doyle /Irish ✓
Skwor /Czech ✓
=======
Epoch : 72 | Loss : 0.2583 | Train Accuracy : 91.40 %
=======
Martoyas /Greek ✗ (Russian)
Hallett /English ✓
Bermudez /Spanish ✓
Bonhomme /French ✓
Lam /Chinese ✗ (Vietnamese)
Kourempes /Greek ✓
Matsuki /Japanese ✓
Sokolofsky /Polish ✓
Kuipers /Dutch ✓
Aller /Dutch ✓
=======
Epoch : 73 | Loss : 0.2564 | Train Accuracy : 91.65 %
=======
Xiao /Chinese ✓
Lian /Chinese ✓
Prill /Czech ✓
Glatter /Czech ✓
Bureau /French ✓
Vallance /French ✗ (English)
Johnstone /Scottish ✓
Kassab /Arabic ✓
Atiyeh /Arabic ✓
Luong /Vietnamese ✓
=======
Epoch : 74 | Loss : 0.2610 | Train Accuracy : 91.60 %
=======
Poirier /French ✓
Hahn /German ✓
Botros /Arabic ✓
Adrichem /Dutch ✓
Rios /Portuguese ✗ (Spanish)
Zangari /Italian ✓
Mccallum /Scottish ✓
Ferro /Portuguese ✓
Kim /Vietnamese ✗ (Korean)
Chang /Korean ✓
=======
Epoch : 75 | Loss : 0.2792 | Train Accuracy : 90.85 %
=======
Monte /Italian ✓
Han /Vietnamese ✗ (Korean)
Ying /Chinese ✓
O'Driscoll /Irish ✓
Poirier /French ✓
Tsuruya /Japanese ✓
Ralph /Czech ✗ (English)
Bonheur /French ✓
Romagna /Italian ✓
Krakowski /Polish ✓
=======
Epoch : 76 | Loss : 0.3000 | Train Accuracy : 90.85 %
=======
Fay /French ✓
Plisek /Czech ✓
Travere /French ✓
Moon /Korean ✓
Aalst /Dutch ✓
Sassa /Japanese ✓
Holzmann /German ✓
Kruessel /Czech ✓
Truong /Vietnamese ✓
Kassab /Arabic ✓
=======
Epoch : 77 | Loss : 0.2602 | Train Accuracy : 92.00 %
=======
Janda /Polish ✓
Doan /Vietnamese ✓
O'Reilly /Irish ✓
Torres /Portuguese ✓
Esipovich /Russian ✓
Johnston /Scottish ✓
Viteri /Spanish ✗ (Italian)
Vandroogenbroeck /Dutch ✓
Montero /Spanish ✓
Wan /Chinese ✓
=======
Epoch : 78 | Loss : 0.2460 | Train Accuracy : 92.35 %
=======
Ha /Korean ✗ (Vietnamese)
Christie /Scottish ✓
Pereira /Portuguese ✓
Kuiper /Dutch ✓
Bautista /Spanish ✓
Takewaki /Japanese ✓
Ying /Chinese ✓
Sokoloff /Polish ✓
Raghailligh /Irish ✓
Edgell /English ✓
=======
Epoch : 79 | Loss : 0.2640 | Train Accuracy : 91.60 %
=======
Neal /English ✓
Kafka /Czech ✓
Hay /Scottish ✓
Pelletier /French ✓
Wilchek /Czech ✓
Marchesi /Italian ✓
Michel /Spanish ✓
Ling /Chinese ✓
Zou /Chinese ✓
Freitas /Portuguese ✓
=======
Epoch : 80 | Loss : 0.2579 | Train Accuracy : 92.60 %
=======
Kelly /Scottish ✓
Chou /Korean ✓
Unsworth /English ✓
Larue /French ✓
Tron /Vietnamese ✓
Herodes /Czech ✓
Fremut /Czech ✓
Thai /Vietnamese ✓
Clark /Irish ✗ (Scottish)
Halenkov /Russian ✓
=======
Epoch : 81 | Loss : 0.2453 | Train Accuracy : 92.40 %
=======
Than /Vietnamese ✓
Pfeifer /Czech ✓
Kennedy /Scottish ✓
Klerkx /Dutch ✓
Freitas /Portuguese ✓
Miller /Scottish ✓
Deeb /Arabic ✓
Almetov /Russian ✓
Li /Korean ✓
Perez /Spanish ✓
=======
Epoch : 82 | Loss : 0.2579 | Train Accuracy : 91.85 %
=======
Horiatis /Greek ✓
Clowes /English ✓
Santos /Portuguese ✓
Mochan /Irish ✓
Sokolof /Polish ✓
Ogterop /Dutch ✓
Schwartz /Czech ✓
Abadi /Arabic ✓
Winogrodzki /Polish ✓
Bazzhin /Russian ✓
=======
Epoch : 83 | Loss : 0.2397 | Train Accuracy : 92.25 %
=======
Ybarra /Spanish ✓
Jacobson /English ✓
Shi /Chinese ✓
Berg /Dutch ✓
Araya /Spanish ✓
Banos /Greek ✓
Winograd /Polish ✓
Schneider /German ✗ (Czech)
Calligaris /Italian ✓
Hasbulatov /Russian ✓
=======
Epoch : 84 | Loss : 0.2466 | Train Accuracy : 92.25 %
=======
Felkerzam /Polish ✗ (Russian)
Gomolka /Polish ✓
Belo /Portuguese ✓
Santos /Portuguese ✓
Rim /Korean ✓
Solomon /French ✓
Agnellutti /Italian ✓
Santana /Portuguese ✓
Ramaker /Dutch ✓
Palmisano /Italian ✓
=======
Epoch : 85 | Loss : 0.2390 | Train Accuracy : 92.80 %
=======
Moraitopoulos /Greek ✓
Rosenberger /German ✓
Torres /Portuguese ✓
Sabbagh /Arabic ✓
Aggelen /Dutch ✓
Chieu /Chinese ✓
Ukhtomsky /Russian ✓
Kouros /Greek ✓
Portyansky /Russian ✓
Dinh /Vietnamese ✓
=======
Epoch : 86 | Loss : 0.2173 | Train Accuracy : 93.15 %
=======
Jaskulski /Polish ✓
Mendez /Spanish ✓
Smyth /English ✓
Mo /Korean ✓
Seegers /Dutch ✓
Elford /English ✓
Lau /Chinese ✓
Sargent /French ✓
Zeman /Czech ✓
Ohme /German ✓
=======
Epoch : 87 | Loss : 0.2189 | Train Accuracy : 93.00 %
=======
Gwang  /Korean ✓
Zhernakov /Russian ✓
Quraishi /Arabic ✓
Asker /Arabic ✓
Franco /Portuguese ✓
Bacon /Czech ✓
Weineltk /Czech ✓
Thuy /Vietnamese ✓
Close /Greek ✓
Xiang /Chinese ✓
=======
Epoch : 88 | Loss : 0.2497 | Train Accuracy : 91.95 %
=======
Basara /Arabic ✓
Esteves /Portuguese ✓
Metz /German ✓
Lopez /Spanish ✓
Bukoski /Polish ✓
Rao /Italian ✓
Klerk /Dutch ✓
Espinoza /Spanish ✓
Wehunt /German ✓
Giehl /German ✓
=======
Epoch : 89 | Loss : 0.2454 | Train Accuracy : 92.70 %
=======
Deniau /French ✓
Bover /Spanish ✗ (Italian)
Soho /Japanese ✓
Ron /Korean ✓
Dolezal /Czech ✓
Yamamura /Japanese ✓
Ramsay /Scottish ✓
Fromm /German ✓
Huerta /Spanish ✓
Colman /Irish ✓
=======
Epoch : 90 | Loss : 0.2432 | Train Accuracy : 92.55 %
=======
Fei /Chinese ✓
Dinh /Vietnamese ✓
Krawiec /Czech ✓
Urbanek /Czech ✓
Yan /Chinese ✓
Giehl /German ✓
Riain /Irish ✓
Zhestkov /Russian ✓
Almetiev /Russian ✓
Deniel /French ✓
=======
Epoch : 91 | Loss : 0.2318 | Train Accuracy : 92.45 %
=======
Macleod /Scottish ✓
Savatier /French ✓
Chong /Chinese ✗ (Korean)
Prchal /Czech ✓
Tsoumada /Greek ✓
Agosti /Italian ✓
Mojjis /Czech ✓
Mitsui /Japanese ✓
San nicolas /Spanish ✓
Kokoris /Greek ✓
=======
Epoch : 92 | Loss : 0.2163 | Train Accuracy : 92.25 %
=======
Marubeni /Japanese ✓
Suh /Korean ✓
Klerks /Dutch ✓
Roma /Spanish ✓
Lac /Vietnamese ✓
Khoury /Arabic ✓
Hoefler /German ✓
Gibson /Scottish ✓
Jasso /Spanish ✓
Oirschotten /Dutch ✓
=======
Epoch : 93 | Loss : 0.2329 | Train Accuracy : 92.90 %
=======
Kikuchi /Japanese ✓
Blecher /German ✓
Ying /Chinese ✓
Nahas /Arabic ✓
Araujo /Portuguese ✓
Koemans /Dutch ✓
Durant /French ✓
Martzenyuk /Russian ✓
Petru /Czech ✓
Wood /Scottish ✓
=======
Epoch : 94 | Loss : 0.2080 | Train Accuracy : 93.35 %
=======
Vespa /Italian ✓
Kowalczyk /Polish ✓
Nahas /Arabic ✓
Tresler /German ✓
Caivano /Italian ✓
Pho /Vietnamese ✓
Dael /Dutch ✓
Heidl /Czech ✓
Totolos /Greek ✓
Adlersflugel /German ✓
=======
Epoch : 95 | Loss : 0.2400 | Train Accuracy : 92.45 %
=======
Zwolenksy /Czech ✓
Chermak /Czech ✓
Sgro /Italian ✓
Lestrange /French ✓
Hwang /Korean ✓
Barros /Spanish ✗ (Portuguese)
Boyle /Scottish ✓
Schmeling /German ✓
Poplawski /Polish ✓
Pantelas /Greek ✓
=======
Epoch : 96 | Loss : 0.2150 | Train Accuracy : 93.10 %
=======
Asher /English ✓
Sai /Vietnamese ✓
Hakimi /Arabic ✓
Sheng /Chinese ✓
Lebedevich /Russian ✓
Castellano /Spanish ✓
Battaglia /Italian ✓
Cardozo /Portuguese ✓
Sinagra /Italian ✓
Bumgarner /German ✓
=======
Epoch : 97 | Loss : 0.2359 | Train Accuracy : 92.80 %
=======
Lyes /English ✓
Do /Vietnamese ✓
Busch /German ✓
Shimura /Japanese ✓
O'Connor /Irish ✓
Geiger /German ✓
Reier /German ✓
Paris /French ✓
Sokolof /Polish ✓
Limarev /Russian ✓
=======
Epoch : 98 | Loss : 0.2369 | Train Accuracy : 92.55 %
=======
Osladil /Czech ✓
Roy /French ✓
Ortega /Spanish ✓
Ruzzier /Italian ✓
Chou /Korean ✓
Mateus /Portuguese ✓
Ibanez /Spanish ✓
Cabello /Spanish ✓
Duguay /French ✓
Yi /Korean ✓
=======
Epoch : 99 | Loss : 0.2192 | Train Accuracy : 92.65 %
=======
Test Accuracy : 94.8000%
""""单独测试"
input_data = lineToTensor('WMN').to(device)
out_data = rnn_name(input_data)
print(out_data)
print(categoryFromOutput(out_data))
"""
tensor([[ 5.7034,  1.0659, -8.1210,  0.5218, -2.5818,  0.1362,  7.4420,  3.9547,1.0824, -7.2981,  4.0036, -2.2944,  1.6835, -3.5377, -7.6178,  9.8920,-0.5789, -1.0545]], device='cuda:0', grad_fn=<AddmmBackward0>)
('Chinese', 15)
"""input_data = lineToTensor('Huang').to(device)
out_data = rnn_name(input_data)
print(out_data)
print(categoryFromOutput(out_data))
"""
tensor([[ 4.1755, -1.2160, -3.8454,  5.4587, -4.6412,  0.8137,  1.2896,  1.7429,-1.3145, -2.1634,  1.0224, -2.7052,  2.1592,  1.1909, -8.0874, 10.2988,-1.9252, -0.0729]], device='cuda:0', grad_fn=<AddmmBackward0>)
('Chinese', 15)
"""

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/873879.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络参考模型与5G协议

目录 OSI七层参考模型OSI模型vsTCP/IP模型TCP/IP协议族的组成 OSI七层参考模型 分层功能应用层网络服务与最终用户的一个接口表示层数据的表示,安全,压缩会话层建立,管理,终止会话传输层定义传输数据的协议端口号,以及流控和差错校验网络层进行逻辑地址寻址,实现不同网路之间的…

【QT】窗口|菜单栏|菜单项|工具栏|状态栏|浮动窗口

目录 ​编辑 QT窗口 窗口分类 菜单栏 创建菜单栏&#xff0c;菜单&#xff0c;菜单项 菜单栏 菜单 菜单项 添加分割线 添加槽函数 ​编辑 添加快捷键 工具栏 停靠位置 浮动属性 移动属性 添加快捷项 ​编辑 状态栏 ​编辑 添加标签 添加进度表 ​编辑…

TIA博途中FBD功能块图和LAD梯形图进行相互转换的具体方法示例

TIA博途中FBD功能块图和LAD梯形图进行相互转换的具体方法示例 如下图所示&#xff0c;添加一个FC块&#xff0c;编程语言选择FBD&#xff0c;简单的编写一段程序&#xff0c; 如下图所示&#xff0c;右击该FC块&#xff0c;选择属性&#xff0c;进入&#xff0c; 如下图所示&am…

python运维实战-ssh工具

1. 功能描述 1.1 系统具有的功能描述 &#xff08;1&#xff09;连接服务器&#xff1a;用户可以通过系统连接到远程服务器&#xff0c;系统支持多个服务器配置&#xff0c;并且可以方便地管理这些配置。 &#xff08;2&#xff09;执行命令&#xff1a;用户可以在连接成功后…

RabbitMQ发布确认及交换机类型

RabbitMQ发布确认机制详解 一、引言 在消息队列&#xff08;MQ&#xff09;技术中&#xff0c;RabbitMQ因其稳定性、可靠性和易用性而受到广泛欢迎。为了确保消息的可靠传递&#xff0c;RabbitMQ提供了一系列高级特性&#xff0c;其中发布确认&#xff08;Publisher Confirms&…

配置kali 的apt命令在线安装包的源为国内源

目录 一、安装VMware Tools 二、配置apt国内源 一、安装VMware Tools 点击安装 VMware Tools 后&#xff0c;会加载一个虚拟光驱&#xff0c;里面包含 VMware Tools 的安装包 鼠标右键单击 VMware Tools 的安装包&#xff0c;点击复制到 点击 主目录&#xff0c;再点击选择…

C语言 ——— 写一个函数,调整 整型数组 中 奇数偶数的顺序

目录 题目要求 代码实现 题目要求 创建一个整型数组 自定义函数实现&#xff1a;调整该数组中数字的顺序&#xff0c;使得数组中所有的奇数位于数组的前半部分&#xff0c;数组中所有的偶数位于数组的后半部分 举例&#xff1a; 输入的整型数组为&#xff1a;[234,24,45,…

TCP协议的相关特性

目录 首先先了解TCP和UDP协议的特点 TCP&#xff08;Transmission Control Protocol&#xff0c;传输控制协议&#xff09; UDP&#xff08;User Datagram Protocol&#xff0c;用户数据报协议&#xff09; TCP的六个标志位 确认应答 超时重传 连接管理(三次握手,四次挥…

Python爬虫(基本流程)

1. 确定目标和范围 明确需求&#xff1a;确定你需要从哪些网站抓取哪些数据。合法性&#xff1a;检查目标网站的robots.txt文件&#xff0c;了解哪些内容可以被抓取。数据范围&#xff1a;确定爬取数据的起始和结束点&#xff0c;比如时间范围、页面数量等。 2. 选择合适的工…

科研绘图系列:R语言微生物堆积图(stacked barplot)

介绍 堆叠条形图是一种数据可视化图表,它通过将每个条形分割成多个部分来展示不同类别的数值。每个条形代表一个总体数据,而条形内的每个部分则代表该总体数据中不同子类别的数值。这种图表特别适合展示整体与部分的关系,以及各部分在整体中的比例。 特点: 多部分条形:每…

框架设计MVP

重点&#xff1a; 1.presenter里面包含view和model 2.和MVC不同的是View和Model之间不进行交互 3.view包含presenter,view通过presenter反向处理逻辑。 链接&#xff1a; MVP简单实现_安卓mvp presenter-CSDN博客 Model.h #pragma once //Model数据处理器 class Model { …

动漫风格动漫404网站维护HTML源码

源码介绍 动漫风格动漫404网站维护HTML源码&#xff0c;源码由HTMLCSSJS组成&#xff0c;记事本打开源码文件可以进行内容文字之类的修改&#xff0c;双击html文件可以本地运行效果&#xff0c;也可以上传到服务器里面 效果预览 源码下载 动漫风格动漫404网站维护HTML源码

GSAP动画魔法:解锁网页设计的无限创意与动感

GSAP&#xff08;GreenSock Animation Platform&#xff09;是一个强大的JavaScript动画库&#xff0c;用于在Web应用程序中创建高性能和复杂的动画效果&#xff0c;其提供了一套丰富的API和工具&#xff0c;使开发者能够轻松地创建流畅、交互式的动画&#xff0c;涵盖了从简单…

Shader入门精要总结(一)渲染流水线

渲染流水线的最终目的在于生成或者说是渲染一张二维纹理&#xff0c;即我们在电脑屏幕上看到的所有效果。 流水线主要分成三个阶段&#xff1a;应用阶段&#xff0c;几何阶段&#xff0c;光栅化阶段。&#xff08;概念&#xff09; 一.概述 1. 应用阶段 这个阶段由应用主导&am…

Debian Linux下rclone挂载谷歌云盘碰到的坑

可能是明月好久没有使用境外服务器挂载境外的云盘缘故吧,今天一个代维客户需要他的Linux服务器挂载谷歌云盘好进行云备份,本来是个很简单的事儿,没想到在rclone连接谷歌云盘的时候卡壳了,可是把明月给难为坏了,搜索到的简体中文教程倒是很多,但没有一个提到这个“坑”,最…

秋招突击——7/20——科大讯飞笔试相关——提前{图像叠加、世界杯积分问题、直角三角形个数}——正式{持续输出中位数,二次幂和三次幂的序列表示}

文章目录 引言提前图像叠加世界杯积分问题个人实现参考实现 直角三角形个数个人实现 正式等待时间期望值中位数输出个人实现 二次幂和三次幂序列个人实现 总结 引言 昨天用了牛客网做了一下作业帮的笔试&#xff0c;做的一塌糊涂&#xff0c;但是提醒我要练习使用牛客网的环境…

基于Matlab的数据可视化

基于Matlab的数据可视化 一、二维图形的绘制&#xff08;一&#xff09;基本图形函数&#xff08;1&#xff09;plot函数&#xff08;2&#xff09;fplot函数&#xff08;3&#xff09;其他坐标系的二维曲线 &#xff08;二&#xff09;图形属性设置&#xff08;1&#xff09;线…

Linux网络:应用层协议HTTP(一)

一、什么是HTTP协议 虽然我们说, 应用层协议是我们程序猿自己定的. 但实际上, 已经有大佬们定义了一些现成的, 又非常好用的应用层协议, 供我们直接参考使用. HTTP(超文本传输协议)就是其中之一。 在互联网世界中&#xff0c;HTTP&#xff08;HyperText Transfer Protocol&…

C#中的栈和队列

什么是栈 栈和队列是非常重要的两种数据结构,在软件设计中应用很多。栈和队列也是线性结构,线性表、栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到眼制。栈的操作只能在表的一端进行,队列的插入操作在表…

安卓篇-unidbg初步测试

前言:目前app的进度是c层的md5魔改 已经做好,又套了一层java层的加密。刚好可以再熟悉一下unidbg 最新版的app ,作为demo测试一下。 分析先看下app 的反编译的结果,很明显,源码很清晰 ,之前的混淆方式一点用没有。 然后跳过去 /* loaded from: classes4.dex */ public c…