Linear SVM和LR的区别

1、LInear SVM 和LR都是线性分类器
2、Linear SVM不直接依赖数据分布, 分类平面不受一类点影响;LR则受所有数据点的影响,类别不平衡的情况先对数据做balancing
3、Linear SVM依赖数据表达的距离测度,所以要对数据先做normalization;LR不受其影响;
4、Linear SVM依赖penalty的系数,实验中要做cross-validation选参数
5、Linear SVM和LR的performance都会受到outlier的影响;

feature scaling会是gradient descent收敛的更好
原文网址:https://www.zhihu.com/question/26768865/answer/34048357

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's an example implementation of a linear SVM model for binary classification using PyTorch and autograd for optimization: ``` import torch import torch.nn as nn import torch.optim as optim class LinearSVM(nn.Module): def __init__(self, input_dim): super(LinearSVM, self).__init__() self.weights = nn.Parameter(torch.randn(input_dim)) self.bias = nn.Parameter(torch.randn(1)) def forward(self, x): linear_output = torch.dot(x, self.weights) + self.bias return linear_output # Define training data and labels train_data = torch.tensor([[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]]) train_labels = torch.tensor([1, 1, -1, -1]) # Initialize model and optimizer svm = LinearSVM(input_dim=2) optimizer = optim.SGD(svm.parameters(), lr=0.01) # Define training loop num_epochs = 1000 for epoch in range(num_epochs): svm.train() optimizer.zero_grad() output = svm(train_data) loss = torch.mean(torch.clamp(1 - train_labels * output, min=0)) loss.backward() optimizer.step() # Evaluate model on test data test_data = torch.tensor([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]) svm.eval() test_predictions = torch.sign(svm(test_data)).detach().numpy() print(test_predictions) ``` In this example, we define a `LinearSVM` class that inherits from `nn.Module` and implements a linear SVM model with a single linear layer. We use `nn.Parameter` to define the model's weight and bias parameters, which are then optimized using the `optim.SGD` optimizer. In the training loop, we compute the SVM loss using the hinge loss function and backpropagate the gradients using autograd. We then update the model parameters using the optimizer's `step` method. Finally, we evaluate the trained model on some test data by passing it through the model and taking the sign of the output (since the SVM is a binary classifier). We use `detach().numpy()` to convert the output to a numpy array for easier interpretation. Note: This is just a simple example implementation of a linear SVM in PyTorch using autograd. In practice, you may want to use a more robust implementation or library for SVMs, such as LIBLINEAR or scikit-learn.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值