深度学习实践(二)

部署运行你感兴趣的模型镜像

最近准备做一个深度学习的项目,从零开始学习深度学习。在博客里记录学习的代码,方便回看。

配置:GPU Notebook Colab

深度学习框架:fastai/Pytorch

数据:Kaggle上Titanic数据集  Titanic - Machine Learning from Disaster | Kaggle

完整代码:https://www.kaggle.com/code/jhoward/why-you-should-use-a-framework


 在上一篇实践文章中,使用全部手写的方法将神经网络中初始化、计算预测值、更新参数、训练模型等函数均使用Pytorch内置函数写出。在本章中使用更加完善的fastai库,使得代码更加简洁

读取数据

from fastai.tabular.all import *

pd.options.display.float_format = '{:.2f}'.format
set_seed(42)

df = pd.read_csv(path/'train.csv')

splits = RandomSplitter(seed=42)(df)  # Use `splits` for indices of training and validation sets

dls = TabularPandas(
    df, splits=splits,
    # Turn strings into categories, fill missing values in numeric columns with the median, normalise all numeric columns
    procs = [Categorify, FillMissing, Normalize],  
    # These are the categorical independent variables
    cat_names=["Sex","Pclass","Embarked","Deck", "Title"],
    # These are the continuous independent variables
    cont_names=['Age', 'SibSp', 'Parch', 'LogFare', 'Alone', 'TicketFreq', 'Family'],
    # This is the dependent variable
    y_names="Survived", 
    # The dependent variable is categorical (so build a classification model, not a regression model
    y_block = CategoryBlock(),
).dataloaders(path=".")

训练模型

learn = tabular_learner(dls, metrics=accuracy, layers=[10,10])

learn.lr_find(suggest_funcs=(slide, valley))

fastai内置函数`lr_find`可以有效帮助快速确定learning_rate的取值区间

输出:

SuggestedLRs(slide=0.033113110810518265, valley=0.009120108559727669)

表示合适的learning_rate 范围在0.033和0.009之间

训练模型,epochs=16, learning_rate=0.03

learn.fit(16, lr=0.03)

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值