机器学习 - 创建一个PyTorch classification model

紧接上一篇文章机器学习-神经网络分类 继续描述

先得将数据从 numpy arrays 移到 PyTorch tensor 里。

import torch 

# 将数据从numpy移到PyTorch tensors里
X = torch.from_numpy(X).type(torch.float)
y = torch.from_numpy(y).type(torch.float)

之后,将数据分成训练集和测试集

from sklearn.model_selection import train_test_split 

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size = 0.2,
                                                    random_state = 42)

print(f"X_train 的长度: {len(X_train)}")
print(f"X_test 的长度: {len(X_test)}")
print(f"y_train 的长度: {len(y_train)}")
print(f"y_test 的长度: {len(y_test)}")

# 结果如下
X_train 的长度: 800
X_test 的长度: 200
y_train 的长度: 800
y_test 的长度: 200

现在就可以创建模型

# 创建一个模型
import torch 
from torch import nn 

# Construct a model class that subclasses nn.Module
class CircleModelV0(nn.Module):
  def __init__(self):
    super().__init__()
    # Create 2 nn.Linear layers capable of handling X and y input and output shapes
    self.layer_1 = nn.Linear(in_features = 2, out_features = 5)  # takes in 2 features (X), produces 5 features. # 这一部分中的5称为 hidden units 或者 neurons
    self.layer_2 = nn.Linear(in_features = 5, out_features = 1) # takes in 5 features, produces 1 feature (y)
  
  # 3. Define a forward method containing the forward pass computation
  def forward(self, x):
    # Return the output of layer_2, a single feature, the same shape as y
    return self.layer_2(self.layer_1(x))  # computation goes through layer_1 first then the output of layer_1 goes through layer_2

model_0 = CircleModelV0().to("cpu")
print(model_0)

# 结果如下
CircleModelV0(
  (layer_1): Linear(in_features=2, out_features=5, bias=True)
  (layer_2): Linear(in_features=5, out_features=1, bias=True)
)

The number of hidden units you can use in neural network layers is a hyperparameter (a value you can set yourself) and there’s no set in stone value you have to use.

Generally more hidden units is better but there’s also such a thing as too much. The amount you choose will depend on your model type and dataset you’re working with.


设置 loss function 和 Optimizer

Loss function/Optimizer Problem type PyTorch Code
Stochastic Gradient Descent (SGD) optimizer Classification, regression, many others. torch.optim.SGD()
Adam Optimizer Classification regression, many others. torch.optim.Adam()
Binary cross entropy loss Binary classification torch.nn.BCELossWithLogits or torch.nn.BCELoss
Cross entropy loss Multi-class classification torch.nn.CrossEntropyLoss
Mean absolute error (MAE) or L1 Loss Regression torch.nn.L1Loss
Mean squared error (MSE) or L2 Loss Regression torch.nn.MSELoss

PyTorch has two binary cross entropy implementations:

  1. torch.nn.BCELoss() - Creates a loss function that measures the binary cross entropy between the target (label) and input (features)
  2. torch.nn.BCEWithLogitsLoss() - This is the same as above except it has a sigmoid layer (nn.Sigmoid) built-in.

需要创建 loss function 和 optimizer

# 创建一个 loss function 
loss_fn = nn.BCEWithLogitsLoss()

# 创建一个 optimizer
optimizer = torch.optim.SGD(params = model_0.parameters(),
                            lr = 0.1)

def accuracy_fn(y_true, y_pred):
  correct = torch.eq(y_true, y_pred).sum().item()
  acc = (correct / len(y_pred)) * 100
  return acc 


都看到这了,给个赞呗~

相关推荐

  1. 机器学习 - 创建PyTorch classification model

    2024-03-30 16:38:02       52 阅读
  2. vue创建项目

    2024-03-30 16:38:02       38 阅读
  3. Bootstrap 创建网页

    2024-03-30 16:38:02       28 阅读
  4. 机器学习概述(

    2024-03-30 16:38:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 16:38:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 16:38:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 16:38:02       87 阅读
  4. Python语言-面向对象

    2024-03-30 16:38:02       96 阅读

热门阅读

  1. P1068 [NOIP2009 普及组] 分数线划定 Python

    2024-03-30 16:38:02       45 阅读
  2. 简述如何系统地学习Python

    2024-03-30 16:38:02       46 阅读
  3. SpringBoot -- 整合SpringMVC

    2024-03-30 16:38:02       46 阅读
  4. 用静态工厂方法代替构造器

    2024-03-30 16:38:02       35 阅读