提交 fb649006 作者: 龙婷

update

上级 2d5207f5
# 前馈神经网络(多层感知机)
# 前馈神经网络(多层感知机)
课程ppt-带讲课备注
课程书籍:神经网络与深度学习(第四章)/深度学习花书(第六章)
课程参考视频:https://www.bilibili.com/video/BV13b4y1177W?p=29&vd_source=149ba9b3f35d06876e4f13eb30ed541e
实例:单隐藏层实现前馈多层感知机用来对Fashion-MNIST图像数据集进行分类
实例代码参考教学视频:https://www.bilibili.com/video/BV1hh411U7gn/?p=3&share_source=copy_web&vd_source=50692d688419fbaefda2a76f9eb6adbe
课程时间:2023年10月31日
主讲人:孙浩然
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
# Default ignored files
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
cnn_net.py
cnn_net.py
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="pytorch" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CNN.iml" filepath="$PROJECT_DIR$/.idea/CNN.iml" />
</modules>
</component>
</project>
\ No newline at end of file
import torch
import torch
from torch import nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.module = nn.Sequential(
nn.Conv2d(3,32,5,1,2),
# 对于所有的batch中的同一个channel的数据元素进行标准化处理
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32,32,5,1,2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32,64,5,1,2),
nn.BatchNorm2d(64),
nn.ReLU(),
#二维特征图展平
nn.Flatten(),
#64*8*8
nn.Linear(4096,1024),
nn.Linear(1024, 64),
nn.Linear(64,10),
nn.Softmax(dim=1)
)
def forward(self,input):
output = self.module(input)
return output
if __name__ == "__main__":
input = torch.ones((64,3,32,32))
net = Net()
output = net(input)
print(output.shape)
# This is a sample Python script.
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
from PIL import Image
from PIL import Image
import torch
from cnn_net import Net
from torchvision import transforms
img_path = "./dataset/img.png"
image = Image.open(img_path)
image = image.convert("RGB")
compose = transforms.Compose([transforms.Resize((32,32)),
transforms.ToTensor()
])
image = compose(image)
net = Net()
net.load_state_dict(torch.load("finaly_net100.pt"))
input = torch.reshape(image,[1,3,32,32])
#{'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
output = net(input)
print(output)
print(output.argmax(1))
import torch
import torch
import torchvision
import cnn_net
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
#32*32*3 10种类别
dataset_train = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform=torchvision.transforms.ToTensor(),download=True)
dataset_test = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloder_train = DataLoader(dataset_train,64)
dataloder_test = DataLoader(dataset_test,64)
train_len = dataset_train.__len__()
test_len = dataset_test.__len__()
print(f"训练集的大小为{train_len}")
print(f"测试集的大小为{test_len}")
net = cnn_net.Net()
if torch.cuda.is_available():
net = net.cuda()
#优化器
learningrate = 1e-2
#1e-2 = 1/10^-2
optim = torch.optim.SGD(net.parameters(),lr=learningrate)
#损失值
loss = torch.nn.CrossEntropyLoss()
if torch.cuda.is_available():
loss = loss.cuda()
#训练轮次
epoh = 100
#训练次数
num_train = 0
#测试次数
num_test = 0
#绘制图像
writer = SummaryWriter("finally")
best_loss = 0
for i in range(epoh):
#训练开始
print(f"------第{i+1}轮开始--------")
for data in dataloder_train:
img,targart = data
if torch.cuda.is_available():
img = img.cuda()
targart = targart.cuda()
output = net(img)
#优化模型
optim.zero_grad()
train_loss = loss(output,targart)
train_loss.backward()
optim.step()
#优化表示
num_train += 1
if num_train%100 == 0:
print(f"第{num_train}次训练,loss的值为{train_loss}")
writer.add_scalar("train",train_loss,num_train)
#进行测试
total_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in dataloder_test:
img,targart = data
if torch.cuda.is_available():
img = img.cuda()
targart = targart.cuda()
output = net(img)
total_loss += loss(output,targart)
#计算每轮分类正确的个数
total_accuracy += (output.argmax(1) == targart).sum()
num_test += 1
writer.add_scalar("test", total_loss, num_test)
print(f"第{num_test}轮时整体的loss值为{total_loss}")
print(f"第{num_test}轮时整体正确率为{total_accuracy/test_len}")
#每轮结束对模型进行保存
if i == 0 :
best_loss = total_loss
elif best_loss > total_loss:
best_loss = total_loss
torch.save(net.state_dict(), f"finaly_net100.pt")
writer.close()
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "7dba137d-5ccf-422c-8e05-2ecea198b1ff",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from torch import nn"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "97be88d1-1dfa-4f52-9600-ce62a66c821e",
"metadata": {},
"outputs": [],
"source": [
"def conv_block(in_channel, out_channel):\n",
" layer = nn.Sequential(\n",
" nn.BatchNorm2d(in_channel),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channel, out_channel, kernel_size=3, padding=1, bias=False)\n",
" )\n",
" return layer\n",
"\n",
"class dense_block(nn.Module):\n",
" def __init__(self, in_channel, growth_rate, num_layers):\n",
" super(dense_block, self).__init__()\n",
" block = []\n",
" channel = in_channel\n",
" for i in range(num_layers):\n",
" block.append(conv_block(channel, growth_rate))\n",
" channel += growth_rate\n",
" self.net = nn.Sequential(*block)\n",
" def forward(self, x):\n",
" for layer in self.net:\n",
" out = layer(x)\n",
" x = torch.cat((out, x), dim=1)\n",
" return x\n",
"def transition(in_channel, out_channel):\n",
" trans_layer = nn.Sequential(\n",
" nn.BatchNorm2d(in_channel),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channel, out_channel, 1),\n",
" nn.AvgPool2d(2, 2)\n",
" )\n",
" return trans_layer\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bf8568f2-7988-4ff0-a974-4ddb685e8886",
"metadata": {},
"outputs": [],
"source": [
"class densenet(nn.Module):\n",
" def __init__(self, in_channel, num_classes, growth_rate=32, block_layers=[6, 12, 24, 16]):\n",
" super(densenet, self).__init__()\n",
" self.block1 = nn.Sequential(\n",
" nn.Conv2d(in_channel, 64, 7, 2, 3),\n",
" nn.BatchNorm2d(64),\n",
" nn.ReLU(True),\n",
" nn.MaxPool2d(3, 2, padding=1)\n",
" )\n",
" self.DB1 = self._make_dense_block(64, growth_rate,num=block_layers[0])\n",
" self.TL1 = self._make_transition_layer(256)\n",
" self.DB2 = self._make_dense_block(128, growth_rate, num=block_layers[1])\n",
" self.TL2 = self._make_transition_layer(512)\n",
" self.DB3 = self._make_dense_block(256, growth_rate, num=block_layers[2])\n",
" self.TL3 = self._make_transition_layer(1024)\n",
" self.DB4 = self._make_dense_block(512, growth_rate, num=block_layers[3])\n",
" self.global_average = nn.Sequential(\n",
" nn.BatchNorm2d(1024),\n",
" nn.ReLU(),\n",
" nn.AdaptiveAvgPool2d((1,1)),\n",
" )\n",
" self.classifier = nn.Linear(1024, num_classes)\n",
" def forward(self, x):\n",
" x = self.block1(x)\n",
" x = self.DB1(x)\n",
" x = self.TL1(x)\n",
" x = self.DB2(x)\n",
" x = self.TL2(x)\n",
" x = self.DB3(x)\n",
" x = self.TL3(x)\n",
" x = self.DB4(x)\n",
" x = self.global_average(x)\n",
" x = x.view(x.shape[0], -1)\n",
" x = self.classifier(x)\n",
" return x\n",
"\n",
" def _make_dense_block(self,channels, growth_rate, num):\n",
" block = []\n",
" block.append(dense_block(channels, growth_rate, num))\n",
" channels += num * growth_rate\n",
"\n",
" return nn.Sequential(*block)\n",
" def _make_transition_layer(self,channels):\n",
" block = []\n",
" block.append(transition(channels, channels // 2))\n",
" return nn.Sequential(*block)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7e93c6c0-a485-4da0-a8d2-e21341b9890f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"block1 output shape: torch.Size([1, 64, 56, 56])\n",
"DB1 output shape: torch.Size([1, 256, 56, 56])\n",
"TL1 output shape: torch.Size([1, 128, 28, 28])\n",
"DB2 output shape: torch.Size([1, 512, 28, 28])\n",
"TL2 output shape: torch.Size([1, 256, 14, 14])\n",
"DB3 output shape: torch.Size([1, 1024, 14, 14])\n",
"TL3 output shape: torch.Size([1, 512, 7, 7])\n",
"DB4 output shape: torch.Size([1, 1024, 7, 7])\n",
"global_average output shape: torch.Size([1, 1024, 1, 1])\n",
"classifier output shape: torch.Size([1, 10])\n"
]
}
],
"source": [
"net = densenet(3,10)\n",
"x = torch.rand(1,3,224,224)\n",
"for name,layer in net.named_children():\n",
" if name != \"classifier\":\n",
" x = layer(x)\n",
" print(name, 'output shape:', x.shape)\n",
" else:\n",
" x = x.view(x.size(0), -1)\n",
" x = layer(x)\n",
" print(name, 'output shape:', x.shape)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4515798e-d5c8-4267-a11b-3291e0afd69c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
PPT部分文字不多,大家可以多看下演讲者备注里,有写很多细节。
PPT部分文字不多,大家可以多看下演讲者备注里,有写很多细节。
另外不懂的也可以单独来问我。
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
from .unet_model import UNet
from .unet_model import UNet
import torch
import torch
import torch.nn as nn
import torch.nn.functional as F
# 深层比较容易出现过拟合现象,因此在深层开启dropout规则
def DropOrPass(do_dropout):
if do_dropout:
return nn.Dropout2d()
return (lambda x, **kwargs: x)
# 添加了对输入维度的检查,确保输入为4维(样本x通道x图像)
class ContBatchNorm2d(nn.modules.batchnorm._BatchNorm):
def _check_input_dim(self, input):
if input.dim() != 4:
raise ValueError('expected 4D input (got {}D input)'
.format(input.dim()))
def forward(self, input):
self._check_input_dim(input)
return F.batch_norm(
input, self.running_mean, self.running_var, self.weight, self.bias,
True, self.momentum, self.eps)
# 单个卷积、批归一、非线性一条龙
class ConvLayer(nn.Module):
def __init__(self, nchan):
super(ConvLayer, self).__init__()
self.conv = nn.Conv2d(nchan, nchan, kernel_size=5, padding=2)
self.bn = ContBatchNorm2d(nchan)
self.relu = nn.ELU(inplace=True)
def forward(self, x):
out = self.relu(self.bn(self.conv(x)))
return out
# 连续进行depth个一条龙
def _ConvSeq(nchan, depth):
return nn.Sequential(*[ConvLayer(nchan) for _ in range(depth)])
# in_tr
class InTrans(nn.Module):
def __init__(self, ochan):
super(InTrans, self).__init__()
self.ochan = ochan
# kernel_size=5, padding=2 确保输入输出等宽可以直接相加
self.conv = nn.Conv2d(1, ochan, kernel_size=5, padding=2)
self.bn = ContBatchNorm2d(ochan)
self.relu = nn.ELU(inplace=True)
def forward(self, x):
fx = self.bn(self.conv(x))
x_res = torch.cat([x for _ in range(self.ochan)], 1)
out = self.relu(torch.add(fx, x_res))
return out
# down_tr 输出将分别送往下层的down_tr与同层的up_tr
class DownTrans(nn.Module):
def __init__(self, ichan, conv_depth, dropout = False):
super(DownTrans, self).__init__()
ochan = 2 * ichan
# 下采样部分
self.conv_down = nn.Conv2d(ichan, ochan, kernel_size=2, stride=2)
self.bn = ContBatchNorm2d(ochan)
self.relu1 = nn.ELU(inplace=True)
# 卷积序列部分
self.drop = DropOrPass(dropout)
self.conv_seq = _ConvSeq(ochan, conv_depth)
# 残差部分
self.relu2 = nn.ELU(inplace=True)
def forward(self, x):
x_down = self.relu1(self.bn(self.conv_down(x)))
fx = self.conv_seq(self.drop(x_down))
out = self.relu2(torch.add(fx, x_down))
return out
# up_tr 接受来自下层的up_tr与同层的down_tr输入
class UpTrans(nn.Module):
def __init__(self, ichan, ochan, conv_depth, dropout = False):
super(UpTrans, self).__init__()
schan = ochan // 2 # 同层skip输入通道数
# 上采样部分
self.conv_up = nn.ConvTranspose2d(ichan, schan, kernel_size=2, stride=2)
self.bn = ContBatchNorm2d(schan)
self.relu1 = nn.ELU(inplace = True)
# 跳跃连接部分
self.drop1 = DropOrPass(dropout)
self.drop2 = nn.Dropout2d() # 始终默认Dropout
# 卷积序列部分
self.conv_seq = _ConvSeq(ochan, conv_depth)
# 残差部分
self.relu2 = nn.ELU(inplace = True)
def forward(self, x, x_skip):
x_up = self.relu1(self.bn(self.conv_up(self.drop1(x)))) # 上采样
x_cat = torch.cat((x_up, self.drop2(x_skip)), 1) # 跳跃连接
fx = self.conv_seq(x_cat) # 卷积序列
out = self.relu2(torch.add(fx, x_cat)) # 残差
return out
# out_tr
class OutTrans(nn.Module):
def __init__(self, ichan, nll):
super(OutTrans, self).__init__()
self.conv1 = nn.Conv2d(ichan, 2, kernel_size=5, padding=2)
self.bn = ContBatchNorm2d(2)
self.conv2 = nn.Conv2d(2, 2, kernel_size = 1)
self.relu = nn.ELU(inplace = True)
self.softmax = F.log_softmax if nll else F.softmax
def forward(self, x):
out = self.conv2(self.relu(self.bn(self.conv1(x))))
# 将通道转置到最后一维
out = out.permute(0, 2, 3, 4, 1).contiguous()
out = out.view(out.numel() // 2, 2)
out = self.softmax(out)
return out
# out_tr_same
class OutTransSame(nn.Module):
def __init__(self, ichan, nll):
super(OutTransSame, self).__init__()
self.conv1 = nn.Conv2d(ichan, 1, kernel_size=5, padding=2)
self.bn = ContBatchNorm2d(1)
self.conv2 = nn.Conv2d(1, 1, kernel_size = 1)
self.relu = nn.ELU(inplace = True)
def forward(self, x):
out = self.conv2(self.relu(self.bn(self.conv1(x))))
return out
class UNet(nn.Module):
def __init__(self, nll = False):
super(UNet, self).__init__()
self.in_tr = InTrans(16) # 1->16
self.down_tr1 = DownTrans(16, 1) # 16->32
self.down_tr2 = DownTrans(32, 2) # 32->64
self.down_tr3 = DownTrans(64, 3, dropout = True) # 64->128
#self.down_tr4 = DownTrans(128, 2, dropout = True) # 128->256
#self.up_tr43 = UpTrans(256, 256, 2, dropout = True) # 256->128(+128)->256
#self.up_tr32 = UpTrans(256, 128, 2, dropout = True) # 256->64(+64)->128
self.up_tr32 = UpTrans(128, 128, 2, dropout = True) # 128->64(+64)->128
self.up_tr21 = UpTrans(128, 64, 1, dropout = True) # 128->32(+32)->64
self.up_tr10 = UpTrans(64, 32, 1) # 64->16(+16)->32
self.out_tr = OutTransSame(32, nll) # 32->1
def forward(self, x):
fx0 = self.in_tr(x)
fx1 = self.down_tr1(fx0)
fx2 = self.down_tr2(fx1)
fx3 = self.down_tr3(fx2)
#fx4 = self.down_tr4(fx3)
#out = self.up_tr43(fx4, fx3)
#out = self.up_tr32(out, fx2)
out = self.up_tr32(fx3, fx2)
out = self.up_tr21(out, fx1)
out = self.up_tr10(out, fx0)
out = self.out_tr(out)
return out
if __name__ == "__main__":
net = UNet(1)
print(net)
\ No newline at end of file
""" Full assembly of the parts to form the complete network """
""" Full assembly of the parts to form the complete network """
from unet_parts import *
class UNet(nn.Module):
def __init__(self, n_channels, n_classes):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.inc = DoubleConv(n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
self.down4 = Down(512, 1024)
self.up1 = Up(1024, 512)
self.up2 = Up(512, 256)
self.up3 = Up(256, 128)
self.up4 = Up(128, 64)
self.outc = OutConv(64, n_classes)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
final = self.outc(x)
return final
if __name__ == "__main__":
net = UNet(3,1)
print(net)
\ No newline at end of file
""" Parts of the U-Net model """
""" Parts of the U-Net model """
import torch
import torch.nn as nn
import torch.nn.functional as F
class DoubleConv(nn.Module):
"""(convolution => [BN] => ReLU) * 2"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.double_conv(x)
class Down(nn.Module):
"""Downscaling with maxpool then double conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
#下采样和双卷积
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
class Up(nn.Module):
"""Upscaling then double conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
# 使用转置卷积进行上采样
self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
# 双卷积操作
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
# 上采样
x1 = self.up(x1)
# 将 x2 和调整后的 x1 连接在一起
x = torch.cat([x2, x1], dim=1)
# 对连接后的张量进行双卷积操作
return self.conv(x)
class OutConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(OutConv, self).__init__()
#最终输出分割结果
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
def forward(self, x):
return self.conv(x)
https://www.naftaliharris.com/blog/visualizing-k-means-clustering/
https://www.naftaliharris.com/blog/visualizing-k-means-clustering/
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
引入
引入
过拟合 欠拟合
No Free Lunch
交叉验证
KNN(有监督)
决策树(有监督)
Kmeans(无监督)
PCA(无监督)
\ No newline at end of file
人工智能 机器学习 深度学习
人工智能 机器学习 深度学习
过拟合 欠拟合
交叉验证
KNN(虽然名字带有nn,但属于分类算法,K-Nearest-Neighbors,近朱者赤近墨者黑)
算法关键:
(1)样本的所有特征都要做可比较的量化。若是样本特征中存在非数值的类型,必须采取手段将其量化为数值。例如样本特征中包含颜色,可通过颜色转换为灰度值来实现距离计算。
(2)样本特征要做归一化处理。样本有多个参数,每一个参数都有自己的定义域和取值范围,他们对距离计算的影响不一样,如取值较大的影响力会盖过取值较小的参数。
(3)需要一个距离函数以计算两个样本之间的距离。欧氏距离和曼哈顿距离。
(4)确定K的值。太大容易欠拟合,太小容易过拟合,需交叉验证确定K值。
优点:易于理解,无需估计参数,无需训练。特别适合多分类问题。
缺点:当样本不平衡时,如一个类的样本容量很大,而其他类样本容量很小时,有可能导致当输入一个新样本时,该样本的K个邻居中大容量类的样本占多数,如下图所示。(可以采用权值的方法改进)
计算量较大,因为对每一个待分类的文本都要计算它到全体已知样本的距离,才能求得它的K个最近邻点。
算法步骤:(1)算距离;(2)找邻居;(3)做分类。
决策树(一种基本的分类与回归方法,主要讨论分类)
if-then集合
熵:熵是表示随机变量不确定性的度量(说白了就是物体内部的混乱程度,对比义乌和苹果专卖店)
信息增益大=熵值下降多
算法步骤:(1)特征选择;(2)决策树的生成;(3)决策树的修剪。
算法本质:从训练集中归纳出一组分类规则,或者说是由训练数据集估计条件概率模型。
注意:
(1)决策树对特征选择的顺序十分严格,如果顺序不一样,最后得到的分类结果会有误差
决策树的训练与测试
训练阶段:从给定的训练集构造出来一棵树(从根节点开始选择特征,如何进行特征切分)
测试阶段:根据构造出来的树模型从上到下去走一遍
一旦构造好了决策树,那么分类或者预测任务就很简单,只要走一遍就可以了,难点在于如何构造出一颗决策树,包括特征选择、阈值设置等等问题。
决策树的剪枝
为什么要剪枝?决策树过拟合风险很大,理论上可以完全分得开数据
剪枝策略:预剪枝,后剪枝
预剪枝:一边建立决策树一边进行剪枝的操作(一般都用预剪枝)。限制深度,叶子节点个数、叶子结点样本数、信息增益量等。
后剪枝:当建立完决策树后进行剪枝操作。叶子节点越多,损失越大。
PCA
PCA(Principal Component Analysis) 是一种常见的数据分析方式,常用于高维数据的降维,可用于提取数据的主要特征分量。
多变量大样本无疑会为研究和应用提供了丰富的信息,但也在一定程度上增加了数据采集的工作量,更重要的是在多数情况下,许多变量之间可能存在相关性,从而增加了问题分析的复杂性,同时对分析带来不便。如果分别对每个指标进行分析,分析往往是孤立的,而不是综合的。盲目减少指标会损失很多信息,容易产生错误的结论。
所以需要找到一个合理的方法,在减少需要分析的指标同时,尽量减少原指标包含信息的损失,以达到对所收集数据进行全面分析的目的。
去中心化不会影响样本的分布性质,但会简化PCA算法的推导过程。
PCA可以帮助提取数据的关键特征并简化数据集,从而降低数据的复杂度,加快机器学习算法的训练速度,并降低存储空间要求。
数据降维是怎么回事儿?假设三维空间中有一系列点,这些点分布在一个过原点的斜面上,如果你用自然坐标系x,y,z这三个轴来表示这组数据的话,需要使用三个维度,而事实上,这些点的分布仅仅是在一个二维的平面上,那么,问题出在哪里?如果你再仔细想想,能不能把x,y,z坐标系旋转一下,使数据所在平面与x,y平面重合?这就对了!如果把旋转后的坐标系记为x',y',z',那么这组数据的表示只用x'和y'两个维度表示即可!当然了,如果想恢复原来的表示方式,那就得把这两个坐标之间的变换矩阵存下来。这样就能把数据维度降下来了!
PCA的思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征。这k维特征称为主成分,是重新构造出来的k维特征,而不是简单地从n维特征中去除其余n-k维特征。
K-Menas
对初始种子很敏感
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论