机器学习 - 创建一个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/OptimizerProblem typePyTorch Code
Stochastic Gradient Descent (SGD) optimizerClassification, regression, many others.torch.optim.SGD()
Adam OptimizerClassification regression, many others.torch.optim.Adam()
Binary cross entropy lossBinary classificationtorch.nn.BCELossWithLogits or torch.nn.BCELoss
Cross entropy lossMulti-class classificationtorch.nn.CrossEntropyLoss
Mean absolute error (MAE) or L1 LossRegressiontorch.nn.L1Loss
Mean squared error (MSE) or L2 LossRegressiontorch.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 


都看到这了,给个赞呗~

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值