机器学习中回归训练的示例

使用perming完成示例

python 复制代码
pip install perming>=1.9.2
pip install polars[pandas]

下载数据集

数据清洗和预处理

python 复制代码
import numpy
import pandas
df = pandas.read_csv('../data/uci_gbm_data.txt', sep='   ', engine='python')
df.head()
python 复制代码
Lever position (lp) [ ]	Ship speed (v) [knots]	Gas Turbine shaft torque (GTT) [kN m]	Gas Turbine rate of revolutions (GTn) [rpm]	Gas Generator rate of revolutions (GGn) [rpm]	Starboard Propeller Torque (Ts) [kN]	Port Propeller Torque (Tp) [kN]	HP Turbine exit temperature (T48) [C]	GT Compressor inlet air temperature (T1) [C]	GT Compressor outlet air temperature (T2) [C]	HP Turbine exit pressure (P48) [bar]	GT Compressor inlet air pressure (P1) [bar]	GT Compressor outlet air pressure (P2) [bar]	Gas Turbine exhaust gas pressure (Pexh) [bar]	Turbine Injecton Control (TIC) [%]	Fuel flow (mf) [kg/s]	GT Compressor decay state coefficient.	GT Turbine decay state coefficient.
0	1.138	3.0	289.964	1349.489	6677.380	7.584	7.584	464.006	288.0	550.563	1.096	0.998	5.947	1.019	7.137	0.082	0.95	0.975
1	2.088	6.0	6960.180	1376.166	6828.469	28.204	28.204	635.401	288.0	581.658	1.331	0.998	7.282	1.019	10.655	0.287	0.95	0.975
2	3.144	9.0	8379.229	1386.757	7111.811	60.358	60.358	606.002	288.0	587.587	1.389	0.998	7.574	1.020	13.086	0.259	0.95	0.975
3	4.161	12.0	14724.395	1547.465	7792.630	113.774	113.774	661.471	288.0	613.851	1.658	0.998	9.007	1.022	18.109	0.358	0.95	0.975
4	5.140	15.0	21636.432	1924.313	8494.777	175.306	175.306	731.494	288.0	645.642	2.078	0.998	11.197	1.026	26.373	0.522	0.95	0.975

转换数据形式到Numpy

python 复制代码
df = df.to_numpy()
values = df[:,-1]
features = df[:,:-1]
features.shape, values.shape
python 复制代码
((11934, 17), (11934,))

加载机器学习过程

python 复制代码
import perming
main = perming.Box(17, 1, (30,), criterion='MSELoss', batch_size=4, activation='relu', inplace_on=True, solver='adam', learning_rate_init=0.01)
# main = perming.Regressier(17, (30,), batch_size=4, activation='relu', solver='adam', learning_rate_init=0.01)
# main = perming.COMMON_MODELS['Regression'](17, (30,), batch_size=4, activation='relu', solver='adam', learning_rate_init=0.01)
main.print_config()
python 复制代码
MLP(
  (mlp): Sequential(
    (Linear0): Linear(in_features=17, out_features=30, bias=True)
    (Activation0): ReLU(inplace=True)
    (Linear1): Linear(in_features=30, out_features=1, bias=True)
  )
)
OrderedDict([('torch -v', '1.7.1+cu101'),
             ('criterion', MSELoss()),
             ('batch_size', 4),
             ('solver',
              Adam (
              Parameter Group 0
                  amsgrad: False
                  betas: (0.9, 0.99)
                  eps: 1e-08
                  lr: 0.01
                  weight_decay: 0
              )),
             ('lr_scheduler', None),
             ('device', device(type='cuda'))])

加载数据集到DataLoader

python 复制代码
main.data_loader(features, values, random_seed=0)

训练阶段和加速验证

python 复制代码
main.train_val(num_epochs=2, interval=100, early_stop=True)
python 复制代码
Epoch [1/2], Step [100/2387], Training Loss: 23.0912, Validation Loss: 24.5740
Epoch [1/2], Step [200/2387], Training Loss: 291.9099, Validation Loss: 6.7348
Epoch [1/2], Step [300/2387], Training Loss: 5637.1328, Validation Loss: 1480.3076
Epoch [1/2], Step [400/2387], Training Loss: 1211.0406, Validation Loss: 210.9741
Epoch [1/2], Step [500/2387], Training Loss: 90.4388, Validation Loss: 23.6573
Epoch [1/2], Step [600/2387], Training Loss: 67.0454, Validation Loss: 24.6701
Epoch [1/2], Step [700/2387], Training Loss: 1253.5343, Validation Loss: 1144.0096
Epoch [1/2], Step [800/2387], Training Loss: 39.3887, Validation Loss: 257.6939
Epoch [1/2], Step [900/2387], Training Loss: 0.9986, Validation Loss: 1.1887
Epoch [1/2], Step [1000/2387], Training Loss: 30.2453, Validation Loss: 9.7175
Epoch [1/2], Step [1100/2387], Training Loss: 264.4302, Validation Loss: 19.0528
Epoch [1/2], Step [1200/2387], Training Loss: 5.2984, Validation Loss: 8.8709
Epoch [1/2], Step [1300/2387], Training Loss: 0.0152, Validation Loss: 0.3077
Epoch [1/2], Step [1400/2387], Training Loss: 0.0118, Validation Loss: 0.0014
Epoch [1/2], Step [1500/2387], Training Loss: 0.3608, Validation Loss: 0.3265
Epoch [1/2], Step [1600/2387], Training Loss: 5616.9810, Validation Loss: 54.1350
Epoch [1/2], Step [1700/2387], Training Loss: 1.0014, Validation Loss: 0.3494
Epoch [1/2], Step [1800/2387], Training Loss: 0.0025, Validation Loss: 0.0249
Epoch [1/2], Step [1900/2387], Training Loss: 0.0008, Validation Loss: 0.0195
Epoch [1/2], Step [2000/2387], Training Loss: 0.0041, Validation Loss: 0.0234
Epoch [1/2], Step [2100/2387], Training Loss: 0.2388, Validation Loss: 0.0222
Process stop at epoch [1/2] with patience 10 within tolerance 0.001

已训练的参数测试

python 复制代码
main.test()
python 复制代码
loss of Box on the 1196 test dataset: 0.14259785413742065.
OrderedDict([('problem', 'regression'),
             ('loss',
              {'train': 0.18060052394866943,
               'val': 0.025247152894735336,
               'test': 0.14259785413742065})])

保存模型和导入模型参数

python 复制代码
main.save(False, '../models/ucigbm.ckpt')
python 复制代码
main.load(False, '../models/ucigbm.ckpt')
相关推荐
Elastic 中国社区官方博客6 小时前
搜索倍增器:推动收入、生产力和 AI 实现规模化
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
wjkjpcba6 小时前
机器人控制板PCBA怎么加工?从SMT贴片到BGA焊接解析机器人电子制造工艺
大数据·人工智能
青 春 记 忆6 小时前
Dify Docker Compose 通用无损升级指南:从备份、双版本预演到切换与回滚
运维·人工智能·python·docker·容器
小猪妈咪爱学法6 小时前
欧盟最新发布《AI数字综合法案(Digital Omnibus‑on‑AI,AI综合修订法案)
人工智能·网络安全
ZGIAI6 小时前
ZGI Workflow 变量池:接住节点输出
人工智能·架构
梵构广告6 小时前
提升品牌识别度有哪些方法?
人工智能
ZGIAI6 小时前
ZGI 记忆隔离:多人共用不串号
人工智能·架构
星栈独行7 小时前
决定 Agent 交付下限的「操作系统」:Harness 六层架构拆解
人工智能·架构
AKAMAI7 小时前
实时可观测性:Akamai Cloud Pulse 警报功能正式发布
人工智能·云计算
Capricorn19888 小时前
解决全网抄 Karpathy 导致的 LLM Wiki 污染?基于知芽 Notebook Skill 的 raw/ 笔记法排障指南
大数据·论文阅读·人工智能·笔记·论文笔记